-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b10dc2
commit a39f3f9
Showing
13 changed files
with
229 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?xml version="1.0"?> | ||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>me.sunny.demo.spring</groupId> | ||
<artifactId>spring-demo</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<groupId>me.sunny.demo.spring.integration</groupId> | ||
<artifactId>spring-integration</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<name>spring-integration</name> | ||
<url>http://maven.apache.org</url> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-context</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-test</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.retry</groupId> | ||
<artifactId>spring-retry</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.aspectj</groupId> | ||
<artifactId>aspectjweaver</artifactId> | ||
<version>1.9.4</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
</project> |
15 changes: 15 additions & 0 deletions
15
spring-integration/src/main/java/me/sunny/demo/spring/integration/retry/ApplicationDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package me.sunny.demo.spring.integration.retry; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.retry.annotation.EnableRetry; | ||
|
||
@Configuration | ||
@EnableRetry | ||
public class ApplicationDemo { | ||
|
||
@Bean | ||
public ServiceDemo service() { | ||
return new ServiceDemo(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
spring-integration/src/main/java/me/sunny/demo/spring/integration/retry/ServiceDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package me.sunny.demo.spring.integration.retry; | ||
|
||
import org.springframework.remoting.RemoteAccessException; | ||
import org.springframework.retry.annotation.Recover; | ||
import org.springframework.retry.annotation.Retryable; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class ServiceDemo { | ||
@Retryable(RemoteAccessException.class) | ||
public void service() { | ||
// ... do something | ||
} | ||
@Recover | ||
public void recover(RemoteAccessException e) { | ||
// ... panic | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...ation/src/test/java/me/sunny/demo/spring/integration/retry/demo/RecoveryCallbackTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package me.sunny.demo.spring.integration.retry.demo; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.retry.support.RetryTemplate; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@ContextConfiguration(classes = RetryTemplateDemo.class) | ||
public class RecoveryCallbackTest { | ||
@Autowired | ||
RetryTemplate retryTemplate; | ||
|
||
@Test | ||
public void test() { | ||
Boolean execute = retryTemplate.execute(ctx -> testMethod("arg-1", 12), ctx -> callBack("arg-1", 12)); | ||
System.out.println("result : " + execute); | ||
} | ||
|
||
private Boolean testMethod(String arg1, Integer arg2) throws RuntimeException { | ||
System.out.println("调用 testMethod 方法:arg1:" + arg1 + ",arg2:" + arg2); | ||
throw new RuntimeException("出错啦!"); | ||
} | ||
|
||
private Boolean callBack(String arg1, Integer arg2) throws RuntimeException { | ||
System.out.println("调用 callBack 方法:arg1:" + arg1 + ",arg2:" + arg2); | ||
return false; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...n/src/test/java/me/sunny/demo/spring/integration/retry/demo/RetryAnnotationBasedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package me.sunny.demo.spring.integration.retry.demo; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(classes={ServiceRetryDemo.class}) | ||
public class RetryAnnotationBasedTest { | ||
|
||
@Autowired | ||
private ServiceRetryDemo serviceRetryDemo; | ||
|
||
@Test | ||
public void test() { | ||
serviceRetryDemo.execute(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...egration/src/test/java/me/sunny/demo/spring/integration/retry/demo/RetryTemplateDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package me.sunny.demo.spring.integration.retry.demo; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.retry.backoff.FixedBackOffPolicy; | ||
import org.springframework.retry.policy.SimpleRetryPolicy; | ||
import org.springframework.retry.policy.SoftReferenceMapRetryContextCache; | ||
import org.springframework.retry.support.RetryTemplate; | ||
|
||
public class RetryTemplateDemo { | ||
|
||
@Bean(name = "retryTemplate") | ||
public RetryTemplate getRetryTemplate() { | ||
RetryTemplate template = new RetryTemplate(); | ||
|
||
FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy(); | ||
//设置2s重试一次 | ||
backOffPolicy.setBackOffPeriod(2000); | ||
template.setBackOffPolicy(backOffPolicy); | ||
|
||
SoftReferenceMapRetryContextCache contextCache = new SoftReferenceMapRetryContextCache(); | ||
template.setRetryContextCache(contextCache); | ||
|
||
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(); | ||
template.setRetryPolicy(retryPolicy); | ||
|
||
template.setThrowLastExceptionOnExhausted(true); | ||
return template; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
spring-integration/src/test/java/me/sunny/demo/spring/integration/retry/demo/RetryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package me.sunny.demo.spring.integration.retry.demo; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.retry.support.RetryTemplate; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@ContextConfiguration(classes=RetryTemplateDemo.class) | ||
public class RetryTest { | ||
@Autowired | ||
RetryTemplate retryTemplate; | ||
|
||
@Test | ||
public void test() { | ||
Boolean execute = retryTemplate.execute(ctx -> testMethod("arg-1", 12)); | ||
System.out.println("result : " + execute); | ||
} | ||
|
||
private Boolean testMethod(String arg1, Integer arg2) throws RuntimeException { | ||
System.out.println("调用 testMethod 方法:arg1:" + arg1 + ",arg2:" + arg2); | ||
throw new RuntimeException("出错啦!"); | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
...tegration/src/test/java/me/sunny/demo/spring/integration/retry/demo/ServiceRetryDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package me.sunny.demo.spring.integration.retry.demo; | ||
|
||
import org.springframework.retry.annotation.Backoff; | ||
import org.springframework.retry.annotation.EnableRetry; | ||
import org.springframework.retry.annotation.Recover; | ||
import org.springframework.retry.annotation.Retryable; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@EnableRetry | ||
public class ServiceRetryDemo { | ||
|
||
@Retryable(value = {Exception.class, RuntimeException.class}, maxAttempts = 3, backoff = @Backoff(delay = 2000L, multiplier = 2)) | ||
public void execute() { | ||
System.out.println("调用 execute 方法"); | ||
throw new RuntimeException("出错啦!"); | ||
} | ||
|
||
@Recover | ||
public void recover(Throwable throwable) { | ||
System.out.println("调用 recover 方法"); | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters