Skip to content

Commit

Permalink
sunnychan
Browse files Browse the repository at this point in the history
  • Loading branch information
SunnnyChan committed Nov 17, 2020
1 parent 7b10dc2 commit a39f3f9
Show file tree
Hide file tree
Showing 13 changed files with 229 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 12 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand All @@ -23,6 +21,13 @@
<version>2.1.5.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.4.RELEASE</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
Expand All @@ -46,5 +51,6 @@
<module>spring-bean</module>
<module>spring-cookbook</module>
<module>spring-task</module>
</modules>
</project>
<module>spring-integration</module>
</modules>
</project>
50 changes: 50 additions & 0 deletions spring-integration/pom.xml
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>
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();
}
}
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
}
}
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;
}
}
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();
}
}
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;
}
}
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("出错啦!");
}

}
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.
2 changes: 1 addition & 1 deletion spring-task/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<version>4.12</version>
</dependency>
</dependencies>

Expand Down

0 comments on commit a39f3f9

Please sign in to comment.