新增 try catch 测试用例

This commit is contained in:
byteblogs168 2024-02-22 22:56:08 +08:00
parent d174cc65a4
commit 3af97e6a7d
5 changed files with 99 additions and 24 deletions

View File

@ -3,8 +3,7 @@ CREATE DATABASE demo;
USE demo; USE demo;
CREATE TABLE fail_order CREATE TABLE fail_order
( (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
`order_id` varchar(255) NOT NULL COMMENT '订单Id', `order_id` varchar(255) NOT NULL COMMENT '订单Id',
`source_id` int NOT NULL COMMENT '来源Id', `source_id` int NOT NULL COMMENT '来源Id',
`scene_name` varchar(255) NOT NULL COMMENT '场景名称', `scene_name` varchar(255) NOT NULL COMMENT '场景名称',

View File

@ -106,45 +106,84 @@ public class LocalRetryController {
*/ */
@Operation( @Operation(
description = "指定自定义的异常处理类", 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" 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 localRetryWithRetryMethod(@RequestBody OrderVo orderVo){ public boolean localRetryWithRetryMethod(@RequestBody OrderVo orderVo) {
return localRetryService.localRetryWithRetryMethod(orderVo); return localRetryService.localRetryWithRetryMethod(orderVo);
} }
@GetMapping("/localRetryWithTwoRetryMethod") @GetMapping("/localRetryWithTwoRetryMethod")
/** /**
* 使用自定义的异常处理类 OrderRetryMethod *
* 方法内部存在两个串行的方法retryMethod1retryMethod1 如下所属
* public boolean localRetryWithTwoRetryMethod(final String params) {
* retryHandler.retryMethod1(params);
* retryHandler.retryMethod1(params);
* return true;
* }
* params: 1 => 则retryMethod1触发重试
* params: 2 => 则retryMethod2触发重试
* params: 3 => 则retryMethod1随机触发重试, 若retryMethod1重试成功则retryMethod2一定触发重试否则只触发retryMethod1重试
*
*/ */
@Operation( @Operation(
description = "指定自定义的异常处理类", summary = "N个串行执行的方法触发重试的场景",
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" description = "方法内部存在两个串行的方法retryMethod1、retryMethod1\n" +
"params: 1 => 则retryMethod1触发重试\n" +
"params: 2 => 则retryMethod2触发重试\n" +
"params: 3 => 则retryMethod1随机触发重试, 若retryMethod1重试成功则retryMethod2一定触发重试否则只触发retryMethod1重试"
) )
public boolean localRetryWithTwoRetryMethod(@RequestParam("params") String params){ public boolean localRetryWithTwoRetryMethod(@RequestParam("params") String params) {
return localRetryService.localRetryWithTwoRetryMethod(params); return localRetryService.localRetryWithTwoRetryMethod(params);
} }
@GetMapping("/localRetryWithPropagationRequired")
/** /**
* 使用自定义的异常处理类 OrderRetryMethod * 外部方法传播机制为REQUIRED内部方法传播机制为REQUIRED,
* 只执行入口方法重试
*/ */
@GetMapping("/localRetryWithPropagationRequired")
@Operation( @Operation(
description = "指定自定义的异常处理类", description = "外部方法传播机制为REQUIRED内部方法传播机制为REQUIRED, 只执行入口方法重试",
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" summary = "外部方法传播机制为REQUIRED内部方法传播机制为REQUIRED, 只执行入口方法重试"
) )
public boolean localRetryWithPropagationRequired(@RequestParam("params") String params){ public boolean localRetryWithPropagationRequired(@RequestParam("params") String params) {
return localRetryService.localRetryWithPropagationRequired(params); return localRetryService.localRetryWithPropagationRequired(params);
} }
@GetMapping("/localRetryWithPropagationRequiredNew")
/** /**
* 使用自定义的异常处理类 OrderRetryMethod * 外部方法传播机制为REQUIRED内部方法传播机制为REQUIRED_NEW,
* 外部和内部方法都触发重试
*/ */
@GetMapping("/localRetryWithPropagationRequiredNew")
@Operation( @Operation(
description = "指定自定义的异常处理类", description = "外部方法传播机制为REQUIRED内部方法传播机制为REQUIRED_NEW,外部和内部方法都触发重试",
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" summary = "外部方法传播机制为REQUIRED内部方法传播机制为REQUIRED_NEW,外部和内部方法都触发重试"
) )
public boolean localRetryWithPropagationRequiredNew(@RequestParam("params") String params){ public boolean localRetryWithPropagationRequiredNew(@RequestParam("params") String params) {
return localRetryService.localRetryWithPropagationRequiredNew(params); return localRetryService.localRetryWithPropagationRequiredNew(params);
} }
/**
* 内部方法传播机制为REQUIRED_NEW且异常被try catch捕获内部方法触发重试外部方法不会触发重试
*/
@GetMapping("/localRetryWithTryCatch1")
@Operation(
description = "",
summary = "内部方法传播机制为REQUIRED_NEW且异常被try catch捕获内部方法触发重试外部方法不会触发重试"
)
public boolean localRetryWithTryCatch1(@RequestParam("params") String params) {
return localRetryService.localRetryWithTryCatch1(params);
}
/**
* 内部方法传播机制为REQUIRED且异常被try catch捕获内部方法不会触发重试外部方法也不会触发重试
*/
@GetMapping("/localRetryWithTryCatch2")
@Operation(
description = "",
summary = "内部方法传播机制为REQUIRED且异常被try catch捕获内部方法不会触发重试外部方法也不会触发重试"
)
public boolean localRetryWithTryCatch2(@RequestParam("params") String params) {
return localRetryService.localRetryWithTryCatch2(params);
}
} }

View File

@ -46,9 +46,15 @@ public class RetryHandler {
} }
} }
@Retryable(scene = "localRetry", retryStrategy = RetryType.ONLY_LOCAL, propagation = Propagation.REQUIRES_NEW) @Retryable(scene = "localRetry", retryStrategy = RetryType.ONLY_LOCAL)
public void localRetry(String params) { public void localRetry(String params) {
System.out.println("local retry 方法开始执行"); System.out.println("local retry 方法开始执行");
double i = 1 / 0; double i = 1 / 0;
} }
@Retryable(scene = "localRetryWithRequiresNew", retryStrategy = RetryType.ONLY_LOCAL, propagation = Propagation.REQUIRES_NEW)
public void localRetryWithRequiresNew(String params) {
System.out.println("local retry 方法开始执行");
double i = 1 / 0;
}
} }

View File

@ -31,4 +31,8 @@ public interface LocalRetryService {
boolean localRetryWithPropagationRequiredNew(String params); boolean localRetryWithPropagationRequiredNew(String params);
boolean localRetryWithTryCatch1(String params);
boolean localRetryWithTryCatch2(String params);
} }

View File

@ -5,6 +5,7 @@ import com.example.easy.retry.customized.OrderRetryMethod;
import com.example.easy.retry.handler.RetryHandler; import com.example.easy.retry.handler.RetryHandler;
import com.example.easy.retry.service.LocalRetryService; import com.example.easy.retry.service.LocalRetryService;
import com.example.easy.retry.vo.OrderVo; import com.example.easy.retry.vo.OrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -16,6 +17,7 @@ import com.example.easy.retry.exception.ParamException;
*/ */
@Component @Component
@Slf4j
public class LocalRetryServiceImpl implements LocalRetryService { public class LocalRetryServiceImpl implements LocalRetryService {
@Autowired @Autowired
@ -125,7 +127,32 @@ public class LocalRetryServiceImpl implements LocalRetryService {
@Override @Override
@Retryable(scene = "localRetryWithPropagationRequiredNew", retryStrategy = RetryType.ONLY_LOCAL) @Retryable(scene = "localRetryWithPropagationRequiredNew", retryStrategy = RetryType.ONLY_LOCAL)
public boolean localRetryWithPropagationRequiredNew(final String params) { public boolean localRetryWithPropagationRequiredNew(final String params) {
retryHandler.localRetryWithRequiresNew(params);
return false;
}
@Override
@Retryable(scene = "localRetryWithTryCatch1", retryStrategy = RetryType.ONLY_LOCAL)
public boolean localRetryWithTryCatch1(String params) {
try {
// 内部方法需要触发重试
retryHandler.localRetryWithRequiresNew(params);
} catch (Exception e) {
log.error("inner method encountered an exception", e);
}
return false;
}
@Override
@Retryable(scene = "localRetryWithTryCatch2", retryStrategy = RetryType.ONLY_LOCAL)
public boolean localRetryWithTryCatch2(String params) {
// 由于传播机制为{REQUIRED}异常被捕获,所以内部不会触发重试
try {
retryHandler.localRetry(params); retryHandler.localRetry(params);
} catch (Exception e) {
log.error("inner method encountered an exception", e);
}
return false; return false;
} }