Skip to content

Commit a39f3f9

Browse files
committed
sunnychan
1 parent 7b10dc2 commit a39f3f9

File tree

13 files changed

+229
-7
lines changed

13 files changed

+229
-7
lines changed

.idea/compiler.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/encodings.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pom.xml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
53
<modelVersion>4.0.0</modelVersion>
64

75
<parent>
@@ -23,6 +21,13 @@
2321
<version>2.1.5.RELEASE</version>
2422
</dependency>
2523

24+
<dependency>
25+
<groupId>org.springframework</groupId>
26+
<artifactId>spring-test</artifactId>
27+
<version>5.2.4.RELEASE</version>
28+
<scope>test</scope>
29+
</dependency>
30+
2631
<dependency>
2732
<groupId>com.alibaba</groupId>
2833
<artifactId>fastjson</artifactId>
@@ -46,5 +51,6 @@
4651
<module>spring-bean</module>
4752
<module>spring-cookbook</module>
4853
<module>spring-task</module>
49-
</modules>
50-
</project>
54+
<module>spring-integration</module>
55+
</modules>
56+
</project>

spring-integration/pom.xml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0"?>
2+
<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"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>me.sunny.demo.spring</groupId>
7+
<artifactId>spring-demo</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
11+
<groupId>me.sunny.demo.spring.integration</groupId>
12+
<artifactId>spring-integration</artifactId>
13+
<version>1.0-SNAPSHOT</version>
14+
<name>spring-integration</name>
15+
<url>http://maven.apache.org</url>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.springframework</groupId>
20+
<artifactId>spring-context</artifactId>
21+
</dependency>
22+
23+
<dependency>
24+
<groupId>org.springframework</groupId>
25+
<artifactId>spring-test</artifactId>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>org.springframework.retry</groupId>
30+
<artifactId>spring-retry</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.aspectj</groupId>
35+
<artifactId>aspectjweaver</artifactId>
36+
<version>1.9.4</version>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>junit</groupId>
41+
<artifactId>junit</artifactId>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-test</artifactId>
46+
<scope>test</scope>
47+
</dependency>
48+
49+
</dependencies>
50+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package me.sunny.demo.spring.integration.retry;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.retry.annotation.EnableRetry;
6+
7+
@Configuration
8+
@EnableRetry
9+
public class ApplicationDemo {
10+
11+
@Bean
12+
public ServiceDemo service() {
13+
return new ServiceDemo();
14+
}
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package me.sunny.demo.spring.integration.retry;
2+
3+
import org.springframework.remoting.RemoteAccessException;
4+
import org.springframework.retry.annotation.Recover;
5+
import org.springframework.retry.annotation.Retryable;
6+
import org.springframework.stereotype.Service;
7+
8+
@Service
9+
public class ServiceDemo {
10+
@Retryable(RemoteAccessException.class)
11+
public void service() {
12+
// ... do something
13+
}
14+
@Recover
15+
public void recover(RemoteAccessException e) {
16+
// ... panic
17+
}
18+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package me.sunny.demo.spring.integration.retry.demo;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.retry.support.RetryTemplate;
7+
import org.springframework.test.context.ContextConfiguration;
8+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
9+
10+
@RunWith(SpringJUnit4ClassRunner.class)
11+
@ContextConfiguration(classes = RetryTemplateDemo.class)
12+
public class RecoveryCallbackTest {
13+
@Autowired
14+
RetryTemplate retryTemplate;
15+
16+
@Test
17+
public void test() {
18+
Boolean execute = retryTemplate.execute(ctx -> testMethod("arg-1", 12), ctx -> callBack("arg-1", 12));
19+
System.out.println("result : " + execute);
20+
}
21+
22+
private Boolean testMethod(String arg1, Integer arg2) throws RuntimeException {
23+
System.out.println("调用 testMethod 方法:arg1:" + arg1 + ",arg2:" + arg2);
24+
throw new RuntimeException("出错啦!");
25+
}
26+
27+
private Boolean callBack(String arg1, Integer arg2) throws RuntimeException {
28+
System.out.println("调用 callBack 方法:arg1:" + arg1 + ",arg2:" + arg2);
29+
return false;
30+
}
31+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package me.sunny.demo.spring.integration.retry.demo;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
import org.springframework.test.context.junit4.SpringRunner;
8+
9+
@RunWith(SpringRunner.class)
10+
@SpringBootTest(classes={ServiceRetryDemo.class})
11+
public class RetryAnnotationBasedTest {
12+
13+
@Autowired
14+
private ServiceRetryDemo serviceRetryDemo;
15+
16+
@Test
17+
public void test() {
18+
serviceRetryDemo.execute();
19+
}
20+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package me.sunny.demo.spring.integration.retry.demo;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.retry.backoff.FixedBackOffPolicy;
5+
import org.springframework.retry.policy.SimpleRetryPolicy;
6+
import org.springframework.retry.policy.SoftReferenceMapRetryContextCache;
7+
import org.springframework.retry.support.RetryTemplate;
8+
9+
public class RetryTemplateDemo {
10+
11+
@Bean(name = "retryTemplate")
12+
public RetryTemplate getRetryTemplate() {
13+
RetryTemplate template = new RetryTemplate();
14+
15+
FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
16+
//设置2s重试一次
17+
backOffPolicy.setBackOffPeriod(2000);
18+
template.setBackOffPolicy(backOffPolicy);
19+
20+
SoftReferenceMapRetryContextCache contextCache = new SoftReferenceMapRetryContextCache();
21+
template.setRetryContextCache(contextCache);
22+
23+
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
24+
template.setRetryPolicy(retryPolicy);
25+
26+
template.setThrowLastExceptionOnExhausted(true);
27+
return template;
28+
}
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package me.sunny.demo.spring.integration.retry.demo;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.retry.support.RetryTemplate;
7+
import org.springframework.test.context.ContextConfiguration;
8+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
9+
10+
@RunWith(SpringJUnit4ClassRunner.class)
11+
@ContextConfiguration(classes=RetryTemplateDemo.class)
12+
public class RetryTest {
13+
@Autowired
14+
RetryTemplate retryTemplate;
15+
16+
@Test
17+
public void test() {
18+
Boolean execute = retryTemplate.execute(ctx -> testMethod("arg-1", 12));
19+
System.out.println("result : " + execute);
20+
}
21+
22+
private Boolean testMethod(String arg1, Integer arg2) throws RuntimeException {
23+
System.out.println("调用 testMethod 方法:arg1:" + arg1 + ",arg2:" + arg2);
24+
throw new RuntimeException("出错啦!");
25+
}
26+
27+
}

0 commit comments

Comments
 (0)