From d174cc65a48f9432946da596d4e2c693e47ec8ac Mon Sep 17 00:00:00 2001 From: byteblogs168 <598092184@qq.com> Date: Tue, 6 Feb 2024 12:07:49 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E8=AF=95=E6=94=AF=E6=8C=81=E5=B5=8C?= =?UTF-8?q?=E5=A5=97=E9=87=8D=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 8 +-- .../controller/LocalRetryController.java | 36 +++++++++++++ .../WorkflowCallbackController.java | 1 - .../easy/retry/handler/RetryHandler.java | 54 +++++++++++++++++++ .../easy/retry/service/LocalRetryService.java | 6 +++ .../service/impl/LocalRetryServiceImpl.java | 27 ++++++++++ 6 files changed, 127 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/example/easy/retry/handler/RetryHandler.java diff --git a/pom.xml b/pom.xml index c65eb97..1385a65 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ com.example - example + example1 1.0.0 example Demo project for Spring Boot @@ -38,17 +38,17 @@ com.aizuda easy-retry-client-starter - 2.6.0 + 3.1.0-SNAPSHOT com.aizuda easy-retry-client-core - 2.6.0 + 3.1.0-SNAPSHOT com.aizuda easy-retry-client-job-core - 2.6.0 + 3.1.0-SNAPSHOT org.freemarker diff --git a/src/main/java/com/example/easy/retry/controller/LocalRetryController.java b/src/main/java/com/example/easy/retry/controller/LocalRetryController.java index 4f383b1..f980e12 100644 --- a/src/main/java/com/example/easy/retry/controller/LocalRetryController.java +++ b/src/main/java/com/example/easy/retry/controller/LocalRetryController.java @@ -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); + } } diff --git a/src/main/java/com/example/easy/retry/controller/WorkflowCallbackController.java b/src/main/java/com/example/easy/retry/controller/WorkflowCallbackController.java index 92cbbbe..4a290da 100644 --- a/src/main/java/com/example/easy/retry/controller/WorkflowCallbackController.java +++ b/src/main/java/com/example/easy/retry/controller/WorkflowCallbackController.java @@ -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.*; diff --git a/src/main/java/com/example/easy/retry/handler/RetryHandler.java b/src/main/java/com/example/easy/retry/handler/RetryHandler.java new file mode 100644 index 0000000..bc012ab --- /dev/null +++ b/src/main/java/com/example/easy/retry/handler/RetryHandler.java @@ -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; + } +} diff --git a/src/main/java/com/example/easy/retry/service/LocalRetryService.java b/src/main/java/com/example/easy/retry/service/LocalRetryService.java index d36c166..af70aa5 100644 --- a/src/main/java/com/example/easy/retry/service/LocalRetryService.java +++ b/src/main/java/com/example/easy/retry/service/LocalRetryService.java @@ -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); + } diff --git a/src/main/java/com/example/easy/retry/service/impl/LocalRetryServiceImpl.java b/src/main/java/com/example/easy/retry/service/impl/LocalRetryServiceImpl.java index 87b88e8..c58ed59 100644 --- a/src/main/java/com/example/easy/retry/service/impl/LocalRetryServiceImpl.java +++ b/src/main/java/com/example/easy/retry/service/impl/LocalRetryServiceImpl.java @@ -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; + } + }