重试支持嵌套重试

This commit is contained in:
byteblogs168 2024-02-06 12:07:49 +08:00
parent 486f3166b6
commit d174cc65a4
6 changed files with 127 additions and 5 deletions

View File

@ -10,7 +10,7 @@
</parent>
<groupId>com.example</groupId>
<artifactId>example</artifactId>
<artifactId>example1</artifactId>
<version>1.0.0</version>
<name>example</name>
<description>Demo project for Spring Boot</description>
@ -38,17 +38,17 @@
<dependency>
<groupId>com.aizuda</groupId>
<artifactId>easy-retry-client-starter</artifactId>
<version>2.6.0</version>
<version>3.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.aizuda</groupId>
<artifactId>easy-retry-client-core</artifactId>
<version>2.6.0</version>
<version>3.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.aizuda</groupId>
<artifactId>easy-retry-client-job-core</artifactId>
<version>2.6.0</version>
<version>3.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>

View File

@ -111,4 +111,40 @@ public class LocalRetryController {
public boolean localRetryWithRetryMethod(@RequestBody OrderVo orderVo){
return localRetryService.localRetryWithRetryMethod(orderVo);
}
@GetMapping("/localRetryWithTwoRetryMethod")
/**
* 使用自定义的异常处理类 OrderRetryMethod
*/
@Operation(
description = "指定自定义的异常处理类",
summary ="🥇什么是自定义的异常处理类: https://www.easyretry.com/pages/540554/#%E8%87%AA%E5%AE%9A%E4%B9%89%E6%96%B9%E6%B3%95%E6%89%A7%E8%A1%8C%E5%99%A8"
)
public boolean localRetryWithTwoRetryMethod(@RequestParam("params") String params){
return localRetryService.localRetryWithTwoRetryMethod(params);
}
@GetMapping("/localRetryWithPropagationRequired")
/**
* 使用自定义的异常处理类 OrderRetryMethod
*/
@Operation(
description = "指定自定义的异常处理类",
summary ="🥇什么是自定义的异常处理类: https://www.easyretry.com/pages/540554/#%E8%87%AA%E5%AE%9A%E4%B9%89%E6%96%B9%E6%B3%95%E6%89%A7%E8%A1%8C%E5%99%A8"
)
public boolean localRetryWithPropagationRequired(@RequestParam("params") String params){
return localRetryService.localRetryWithPropagationRequired(params);
}
@GetMapping("/localRetryWithPropagationRequiredNew")
/**
* 使用自定义的异常处理类 OrderRetryMethod
*/
@Operation(
description = "指定自定义的异常处理类",
summary ="🥇什么是自定义的异常处理类: https://www.easyretry.com/pages/540554/#%E8%87%AA%E5%AE%9A%E4%B9%89%E6%96%B9%E6%B3%95%E6%89%A7%E8%A1%8C%E5%99%A8"
)
public boolean localRetryWithPropagationRequiredNew(@RequestParam("params") String params){
return localRetryService.localRetryWithPropagationRequiredNew(params);
}
}

View File

@ -1,7 +1,6 @@
package com.example.easy.retry.controller;
import com.aizuda.easy.retry.server.model.dto.CallbackParamsDTO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

View File

@ -0,0 +1,54 @@
package com.example.easy.retry.handler;
import com.aizuda.easy.retry.client.core.annotation.Propagation;
import com.aizuda.easy.retry.client.core.annotation.Retryable;
import com.aizuda.easy.retry.client.core.retryer.RetryType;
import com.example.easy.retry.vo.OrderVo;
import org.springframework.stereotype.Component;
import java.util.Random;
/**
* @author: xiaowoniu
* @date : 2024-02-05
* @since : 3.1.0
*/
@Component
public class RetryHandler {
@Retryable(scene = "localRetryWithTwoRetryMethod1", retryStrategy = RetryType.ONLY_LOCAL)
public void retryMethod1(String params) {
System.out.println("localRetryWithTwoRetryMethod1");
if (params.equals("1")) {
throw new RuntimeException("抛出异常");
}
if (params.equals("3")) {
int random = new Random().nextInt(10);
if (random % 3 == 0) {
System.out.println("localRetryWithTwoRetryMethod1 is success");
return;
}
throw new RuntimeException("抛出异常");
}
}
@Retryable(scene = "localRetryWithTwoRetryMethod2", retryStrategy = RetryType.ONLY_LOCAL)
public void retryMethod2(String params) {
System.out.println("localRetryWithTwoRetryMethod2");
if (params.equals("2")) {
throw new RuntimeException("抛出异常");
}
if (params.equals("3")) {
throw new RuntimeException("抛出异常");
}
}
@Retryable(scene = "localRetry", retryStrategy = RetryType.ONLY_LOCAL, propagation = Propagation.REQUIRES_NEW)
public void localRetry(String params) {
System.out.println("local retry 方法开始执行");
double i = 1 / 0;
}
}

View File

@ -12,6 +12,8 @@ public interface LocalRetryService {
void localRetry(String params);
boolean localRetryWithTwoRetryMethod(String params);
@Retryable(scene = "localRetryWithAnnoOnInterface", retryStrategy = RetryType.ONLY_LOCAL)
void localRetryWithAnnoOnInterface(String params);
@ -25,4 +27,8 @@ public interface LocalRetryService {
boolean localRetryWithRetryMethod(OrderVo orderVo);
boolean localRetryWithPropagationRequired(String params);
boolean localRetryWithPropagationRequiredNew(String params);
}

View File

@ -2,8 +2,10 @@ package com.example.easy.retry.service.impl;
import com.aizuda.easy.retry.client.core.retryer.RetryType;
import com.example.easy.retry.customized.OrderRetryMethod;
import com.example.easy.retry.handler.RetryHandler;
import com.example.easy.retry.service.LocalRetryService;
import com.example.easy.retry.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.aizuda.easy.retry.client.core.annotation.Retryable;
@ -15,6 +17,10 @@ import com.example.easy.retry.exception.ParamException;
@Component
public class LocalRetryServiceImpl implements LocalRetryService {
@Autowired
private RetryHandler retryHandler;
/**
* 入门案例
* 我们仅仅需要指定场景值scene就可以给方法赋予重试逻辑
@ -30,6 +36,13 @@ public class LocalRetryServiceImpl implements LocalRetryService {
double i = 1 / 0;
}
@Override
public boolean localRetryWithTwoRetryMethod(final String params) {
retryHandler.retryMethod1(params);
retryHandler.retryMethod2(params);
return true;
}
@Override
public void localRetryWithAnnoOnInterface(final String params) {
double i = 1 / 0;
@ -102,4 +115,18 @@ public class LocalRetryServiceImpl implements LocalRetryService {
throw new NullPointerException();
}
@Override
@Retryable(scene = "localRetryWithPropagationRequired", retryStrategy = RetryType.ONLY_LOCAL)
public boolean localRetryWithPropagationRequired(String params) {
retryHandler.localRetry(params);
return false;
}
@Override
@Retryable(scene = "localRetryWithPropagationRequiredNew", retryStrategy = RetryType.ONLY_LOCAL)
public boolean localRetryWithPropagationRequiredNew(final String params) {
retryHandler.localRetry(params);
return false;
}
}