review

We learned that earlier

Introduction to Sisyphus, a better Java retry framework

Better Java Retry framework Sisyphus configuration in 2 ways introduced

The story behind Sisyphus, a better Java retry framework

Java Retry framework Sisyphus open source address

In this video, let’s look at three ways Sisyphus can be used.

Sisyphus proxy template

purpose

To make it easier for users to use annotations without relying on Spring.

Provides a way to enhance implementation based on code pattern + bytecode.

Use case

Maven is introduced into

Introduce annotation related modules.

<dependency>
    <groupId>com.github.houbb</groupId>
    <artifactId>sisyphus-annotation</artifactId>
    <version>0.0.9</version>
</dependency>
Copy the code

Define test methods

The following test code can be referenced in the Spring-test module.

  • MenuServiceImpl.java
public class MenuServiceImpl {

    public void queryMenu(long id) {
        System.out.println("Query menu...");
        throw new RuntimeException();
    }

    @Retry
    public void queryMenuRetry(long id) {
        System.out.println("Query menu...");
        throw newRuntimeException(); }}Copy the code

test

Use the RetryTemplate for the test

There is no way to retry annotations

@Test(expected = RuntimeException.class)
public void templateTest(a) {
    MenuServiceImpl menuService = RetryTemplate.getProxyObject(new MenuServiceImpl());
    menuService.queryMenu(1);
}
Copy the code
  • Log information
Query menu...Copy the code

It was only requested once.

Annotated methods

@Test(expected = RuntimeException.class)
public void templateRetryTest(a) {
    MenuServiceImpl menuService = RetryTemplate.getProxyObject(new MenuServiceImpl());
    menuService.queryMenuRetry(1);
}
Copy the code
  • Log information
Query menu... Query menu... Query menu...Copy the code

Sisyphus spring integration

purpose

Similar to the Spring-Retry framework, if you use the Spring framework, integrating the project will be easy.

Annotations and procedural programming are as consistent as possible, and it’s easy for you to go from one to the other.

It is also convenient to switch from Spring-Retry to this framework.

Maven is introduced into

<dependency>
    <groupId>${project.groupId}</groupId>
    <artifactId>sisyphus-spring</artifactId>
    <version>${project.version}</version>
</dependency>
Copy the code

Spring and AOP-related jars are introduced by default.

Business code

You can refer to the sisyphus-test module.

Let’s simulate some very common business methods.

Retry is required using the @retry flag method.

  • SpringService.java
public interface SpringService {

    /** * query sample code *@returnResults the * /
    String query(a);

}
Copy the code
  • 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
 * @since0.0.4 * /
@Service
public class SpringServiceImpl implements SpringService {

    @Override
    @Retry
    public String query(a) {
        System.out.println("spring service query...");
        throw newRuntimeException(); }}Copy the code

Open the retry

The following configuration can be done based on annotations.

Use @enableretry to EnableRetry.

@Configurable
@ComponentScan(basePackages = "com.github.houbb.sisyphus.test.service")
@EnableRetry
public class SpringConfig {}Copy the code

The 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
 * @since0.0.4 * /
@ContextConfiguration(classes = SpringConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringServiceTest {

    @Autowired
    private SpringService springService;

    @Test(expected = RuntimeException.class)
    public void queryTest(a) { springService.query(); }}Copy the code
  • Log information
spring service query...
spring service query...
spring service query...
Copy the code

Sisyphus springboot integration

purpose

Similar to the Spring-Retry framework, if you use the Springboot framework, integrating this project will be very simple.

Annotations and procedural programming are as consistent as possible, and it’s easy for you to go from one to the other.

It is also convenient to switch from Spring-Retry to this framework.

The overall integration is similar to Spring and much simpler.

Maven is introduced into

<dependency>
    <groupId>${project.groupId}</groupId>
    <artifactId>sisyphus-springboot-starter</artifactId>
    <version>${project.version}</version>
</dependency>
Copy the code

Springboot integration-related dependencies are introduced by default.

Business code

You can refer to the sisyphus-test Springboot test module.

Let’s simulate some very common business methods.

Retry is required using the @retry flag method.

  • SpringService.java
public interface SpringService {

    /** * query sample code *@returnResults the * /
    String query(a);

}
Copy the code
  • 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
 * @since0.0.4 * /
@Service
public class SpringServiceImpl implements SpringService {

    @Override
    @Retry
    public String query(a) {
        System.out.println("spring service query...");
        throw newRuntimeException(); }}Copy the code

The 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(a) { springService.query(); }}Copy the code

The sisyphusapplication. Java code is as follows:

Is the basic SpringBoot boot entry.

@SpringBootApplication
@ComponentScan(basePackages = "com.github.houbb.sisyphus.test.service")
public class SisyphusApplication {

    public static void main(String[] args) { SpringApplication.run(SisyphusApplication.class, args); }}Copy the code
  • Log information
spring service query...
spring service query...
spring service query...
Copy the code

summary

Three use methods can basically meet the daily development of various scenarios.

Java Retry framework Sisyphus open source address

I hope this article is helpful to you. If you like it, please click to collect and forward a wave.

I am old ma, looking forward to meeting with you next time.