review
We learned earlier
better java retry framework sisyphus
better java retry framework sisyphus configuration introduction of 2 ways
The story behind the better java retry framework sisyphus
java retry framework sisyphus open source address
In this section, let us learn 3 ways to use sisyphus together.
sisyphus proxy template
Purpose
In order to facilitate users to use annotations more conveniently, without relying on spring at the same time.
Provides implementation based on code mode + bytecode enhancement.
Use Cases
Introduced by maven
Introduce annotation related modules.
<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>sisyphus-annotation</artifactId>
<version>0.0.9</version>
</dependency>
Define the test method
The following test code can refer to the [spring-test]() module.
- MenuServiceImpl.java
public class MenuServiceImpl {
public void queryMenu(long id) {
System.out.println("查询菜单...");
throw new RuntimeException();
}
@Retry
public void queryMenuRetry(long id) {
System.out.println("查询菜单...");
throw new RuntimeException();
}
}
test
Use RetryTemplate for testing
Method without retry annotation
@Test(expected = RuntimeException.class)
public void templateTest() {
MenuServiceImpl menuService = RetryTemplate.getProxyObject(new MenuServiceImpl());
menuService.queryMenu(1);
}
- Log information
查询菜单...
Only requested once.
Annotated method
@Test(expected = RuntimeException.class)
public void templateRetryTest() {
MenuServiceImpl menuService = RetryTemplate.getProxyObject(new MenuServiceImpl());
menuService.queryMenuRetry(1);
}
- Log information
查询菜单...
查询菜单...
查询菜单...
sisyphus spring integration
Purpose
Similar to the spring-retry framework, if you use the spring framework, the integration of this project will be very simple.
The way of annotation and procedural programming, the two are as consistent as possible, and it is relatively simple for you to change from one way to the other.
It is also very convenient to switch from spring-retry to this framework.
Introduced by maven
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>sisyphus-spring</artifactId>
<version>${project.version}</version>
</dependency>
Spring and AOP related jars will be introduced by default.
Business code
You can refer to the sisyphus-test module.
Some very common business methods are simulated below.
Using the @Retry
identification method requires a retry.
- SpringService.java
public interface SpringService {
/**
* 查询示例代码
* @return 结果
*/
String query();
}
- SpringServiceImpl.java
import com.github.houbb.sisyphus.annotation.annotation.Retry;
import com.github.houbb.sisyphus.test.service.SpringService;
import org.springframework.stereotype.Service;
/**
* @author binbin.hou
* @since 0.0.4
*/
@Service
public class SpringServiceImpl implements SpringService {
@Override
@Retry
public String query() {
System.out.println("spring service query...");
throw new RuntimeException();
}
}
Turn on retry
Simply configure as follows based on the annotations.
Using the @EnableRetry
flag requires retrying.
@Configurable
@ComponentScan(basePackages = "com.github.houbb.sisyphus.test.service")
@EnableRetry
public class SpringConfig {
}
Test code
import com.github.houbb.sisyphus.test.config.SpringConfig;
import com.github.houbb.sisyphus.test.service.SpringService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author binbin.hou
* @since 0.0.4
*/
@ContextConfiguration(classes = SpringConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringServiceTest {
@Autowired
private SpringService springService;
@Test(expected = RuntimeException.class)
public void queryTest() {
springService.query();
}
}
- Log information
spring service query...
spring service query...
spring service query...
sisyphus springboot integration
Purpose
Similar to the spring-retry framework, if you use the springboot framework, the integration of this project will be very simple.
The way of annotation and procedural programming, the two are as consistent as possible, and it is relatively simple for you to change from one way to the other.
It is also very convenient to switch from spring-retry to this framework.
The overall integration is similar to spring, and it is simpler.
Introduced by maven
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>sisyphus-springboot-starter</artifactId>
<version>${project.version}</version>
</dependency>
Springboot integration related dependencies will be introduced by default.
Business code
You can refer to the sisyphus-test springboot test module.
Some very common business methods are simulated below.
Using the @Retry
identification method requires a retry.
- SpringService.java
public interface SpringService {
/**
* 查询示例代码
* @return 结果
*/
String query();
}
- SpringServiceImpl.java
import com.github.houbb.sisyphus.annotation.annotation.Retry;
import com.github.houbb.sisyphus.test.service.SpringService;
import org.springframework.stereotype.Service;
/**
* @author binbin.hou
* @since 0.0.4
*/
@Service
public class SpringServiceImpl implements SpringService {
@Override
@Retry
public String query() {
System.out.println("spring service query...");
throw new RuntimeException();
}
}
Test code
- SisyphusApplicationTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SisyphusApplication.class)
public class SisyphusApplicationTest {
@Autowired
private SpringService springService;
@Test(expected = RuntimeException.class)
public void queryTest() {
springService.query();
}
}
The code of SisyphusApplication.java is as follows:
It is the most basic springboot start entry.
@SpringBootApplication
@ComponentScan(basePackages = "com.github.houbb.sisyphus.test.service")
public class SisyphusApplication {
public static void main(String[] args) {
SpringApplication.run(SisyphusApplication.class, args);
}
}
- Log information
spring service query...
spring service query...
spring service query...
summary
The three usage methods can basically meet various scenarios in daily development.
java retry framework sisyphus open source address
I hope this article is helpful to you. If you like it, please like, collect and forward a wave.
I am an old horse, and I look forward to seeing you again next time.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。