From c16f8e977f08769416b4fc8c997937878223e3af Mon Sep 17 00:00:00 2001 From: byteblogs168 <598092184@qq.com> Date: Tue, 16 May 2023 14:16:56 +0800 Subject: [PATCH] =?UTF-8?q?feat:=201.3.0=201.=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B=202.=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../client/core/{report => }/Report.java | 2 +- ...istener.java => EasyRetryEndListener.java} | 7 ++++--- ...tener.java => EasyRetryStartListener.java} | 6 +++--- .../client/core/report/AbstractReport.java | 14 +++++++------- .../retry/client/core/report/AsyncReport.java | 12 ++++-------- .../retry/client/core/report/SyncReport.java | 6 ++++-- .../strategy/AbstractRetryStrategies.java | 2 +- .../example/controller/SchoolController.java | 2 ++ .../example/demo/CustomAsyncCreateTask.java | 2 +- .../example/demo/CustomSyncCreateTask.java | 2 +- .../com/example/demo/NestMethodService.java | 19 ++++++++++++++++--- .../java/com/example/demo/RemoteService.java | 2 +- .../TestExistsTransactionalRetryService.java | 4 ++-- .../com/example/EasyRetryTemplateTest.java | 7 +++++++ .../ExistsTransactionalRetryServiceTest.java | 2 +- 15 files changed, 55 insertions(+), 34 deletions(-) rename easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/{report => }/Report.java (85%) rename easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/init/{EndListener.java => EasyRetryEndListener.java} (76%) rename easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/init/{StartListener.java => EasyRetryStartListener.java} (78%) diff --git a/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/report/Report.java b/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/Report.java similarity index 85% rename from easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/report/Report.java rename to easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/Report.java index aa97d16cf..68429acf4 100644 --- a/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/report/Report.java +++ b/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/Report.java @@ -1,4 +1,4 @@ -package com.aizuda.easy.retry.client.core.report; +package com.aizuda.easy.retry.client.core; import com.aizuda.easy.retry.client.core.retryer.RetryerInfo; diff --git a/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/init/EndListener.java b/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/init/EasyRetryEndListener.java similarity index 76% rename from easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/init/EndListener.java rename to easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/init/EasyRetryEndListener.java index 870d8305b..fd1f36dde 100644 --- a/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/init/EndListener.java +++ b/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/init/EasyRetryEndListener.java @@ -14,18 +14,19 @@ import java.util.List; * * @author: www.byteblogs.com * @date : 2021-11-19 19:00 + * @since 1.0.0 */ @Component @Slf4j -public class EndListener implements ApplicationListener { +public class EasyRetryEndListener implements ApplicationListener { @Autowired private List lifecycleList; @Override public void onApplicationEvent(ContextClosedEvent event) { - log.info("EASY-RETRY-CLIENT 关闭"); + log.info("Easy-Retry client about to shutdown"); lifecycleList.forEach(Lifecycle::close); - log.info("EASY-RETRY-CLIENT 关闭成功"); + log.info("Easy-Retry client closed successfully"); } } diff --git a/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/init/StartListener.java b/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/init/EasyRetryStartListener.java similarity index 78% rename from easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/init/StartListener.java rename to easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/init/EasyRetryStartListener.java index 96f071aed..40e670a73 100644 --- a/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/init/StartListener.java +++ b/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/init/EasyRetryStartListener.java @@ -17,15 +17,15 @@ import java.util.List; */ @Component @Slf4j -public class StartListener implements ApplicationRunner { +public class EasyRetryStartListener implements ApplicationRunner { @Autowired private List lifecycleList; @Override public void run(ApplicationArguments args) throws Exception { - log.info("EASY-RETRY-CLIENT-RETRY 启动"); + log.info("Easy-Retry client is preparing to start"); lifecycleList.forEach(Lifecycle::start); - log.info("EASY-RETRY-CLIENT-RETRY 启动成功"); + log.info("Easy-Retry client started successfully"); } } diff --git a/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/report/AbstractReport.java b/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/report/AbstractReport.java index 3003a4f2c..6211c520f 100644 --- a/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/report/AbstractReport.java +++ b/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/report/AbstractReport.java @@ -2,6 +2,7 @@ package com.aizuda.easy.retry.client.core.report; import cn.hutool.core.lang.Assert; import com.aizuda.easy.retry.client.core.IdempotentIdGenerate; +import com.aizuda.easy.retry.client.core.Report; import com.aizuda.easy.retry.client.core.RetryArgSerializer; import com.aizuda.easy.retry.client.core.cache.RetryerInfoCache; import com.aizuda.easy.retry.client.core.config.EasyRetryProperties; @@ -9,7 +10,6 @@ import com.aizuda.easy.retry.client.core.exception.EasyRetryClientException; import com.aizuda.easy.retry.client.core.intercepter.RetrySiteSnapshot; import com.aizuda.easy.retry.client.core.retryer.RetryerInfo; import com.aizuda.easy.retry.client.core.spel.SPELParamFunction; -import com.aizuda.easy.retry.client.core.window.RetryLeapArray; import com.aizuda.easy.retry.common.core.log.LogUtils; import com.aizuda.easy.retry.server.model.dto.RetryTaskDTO; import lombok.extern.slf4j.Slf4j; @@ -18,14 +18,14 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.util.ReflectionUtils; import java.lang.reflect.Method; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; import java.util.function.Function; /** + * 上报抽象类 + * * @author www.byteblogs.com * @date 2023-05-15 - * @since 2.0 + * @since 1.3.0 */ @Slf4j public abstract class AbstractReport implements Report { @@ -34,9 +34,9 @@ public abstract class AbstractReport implements Report { @Qualifier("easyRetryJacksonSerializer") private RetryArgSerializer retryArgSerializer; - public static final int SAMPLE_COUNT = 10; + protected static final int SAMPLE_COUNT = 10; - public static final int INTERVAL_IN_MS = 1000; + protected static final int INTERVAL_IN_MS = 1000; @Override public boolean report(String scene, final String targetClassName, final Object[] params) { @@ -44,7 +44,7 @@ public abstract class AbstractReport implements Report { Assert.notNull(retryerInfo, () -> new EasyRetryClientException("retryerInfo is null")); if (RetrySiteSnapshot.getStage().equals(RetrySiteSnapshot.EnumStage.REMOTE.getStage()) && !retryerInfo.isForceReport()) { - LogUtils.info(log, "已经上报成功,无需重复上报 scene:[{}] targetClassName:[{}] args:[{}]", + LogUtils.info(log, "Successfully reported, no need to repeat reporting. scene:[{}] targetClassName:[{}] args:[{}]", retryerInfo.getScene(), retryerInfo.getExecutorClassName(), params); return Boolean.TRUE; } diff --git a/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/report/AsyncReport.java b/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/report/AsyncReport.java index 83a12ff5d..f2d9b0f8d 100644 --- a/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/report/AsyncReport.java +++ b/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/report/AsyncReport.java @@ -1,23 +1,19 @@ package com.aizuda.easy.retry.client.core.report; import com.aizuda.easy.retry.client.core.Lifecycle; -import com.aizuda.easy.retry.client.core.client.NettyClient; -import com.aizuda.easy.retry.client.core.client.proxy.RequestBuilder; import com.aizuda.easy.retry.client.core.retryer.RetryerInfo; import com.aizuda.easy.retry.client.core.window.RetryLeapArray; -import com.aizuda.easy.retry.common.core.log.LogUtils; -import com.aizuda.easy.retry.common.core.model.NettyResult; -import com.aizuda.easy.retry.common.core.util.JsonUtil; import com.aizuda.easy.retry.server.model.dto.RetryTaskDTO; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; -import java.util.Collections; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; /** + * 异步上报数据 + * * @author www.byteblogs.com * @date 2023-05-15 * @since 1.3.0 @@ -62,8 +58,8 @@ public class AsyncReport extends AbstractReport implements Lifecycle { @Override public void close() { - log.info("reportHandler about to shutdown"); + log.info("AsyncReport about to shutdown"); slidingWindow.currentWindow(); - log.info("reportHandler has been shutdown"); + log.info("AsyncReport has been shutdown"); } } diff --git a/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/report/SyncReport.java b/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/report/SyncReport.java index afdf3d86d..5c0c0db4e 100644 --- a/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/report/SyncReport.java +++ b/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/report/SyncReport.java @@ -16,6 +16,8 @@ import java.util.Collections; import java.util.concurrent.TimeUnit; /** + * 同步上报数据 + * * @author www.byteblogs.com * @date 2023-05-15 * @since 1.3.0 @@ -42,14 +44,14 @@ public class SyncReport extends AbstractReport { RetryTaskDTO retryTaskDTO = buildRetryTaskDTO(scene, targetClassName, args); - NettyClient CLIENT = RequestBuilder.newBuilder() + NettyClient client = RequestBuilder.newBuilder() .client(NettyClient.class) .async(Boolean.FALSE) .timeout(timeout) .unit(unit) .build(); - NettyResult result = CLIENT.reportRetryInfo(Collections.singletonList(retryTaskDTO)); + NettyResult result = client.reportRetryInfo(Collections.singletonList(retryTaskDTO)); LogUtils.debug(log, "Data report result result:[{}]", JsonUtil.toJsonString(result)); return (Boolean) result.getData(); diff --git a/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/strategy/AbstractRetryStrategies.java b/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/strategy/AbstractRetryStrategies.java index 9a6870fb0..9f37ada74 100644 --- a/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/strategy/AbstractRetryStrategies.java +++ b/easy-retry-client-core/src/main/java/com/aizuda/easy/retry/client/core/strategy/AbstractRetryStrategies.java @@ -4,7 +4,7 @@ import com.aizuda.easy.retry.client.core.RetryExecutor; import com.aizuda.easy.retry.client.core.RetryExecutorParameter; import com.aizuda.easy.retry.client.core.event.EasyRetryListener; import com.aizuda.easy.retry.client.core.intercepter.RetrySiteSnapshot; -import com.aizuda.easy.retry.client.core.report.Report; +import com.aizuda.easy.retry.client.core.Report; import com.github.rholder.retry.Retryer; import com.github.rholder.retry.StopStrategy; import com.github.rholder.retry.WaitStrategy; diff --git a/example/src/main/java/com/example/controller/SchoolController.java b/example/src/main/java/com/example/controller/SchoolController.java index e53879205..5764a6db6 100644 --- a/example/src/main/java/com/example/controller/SchoolController.java +++ b/example/src/main/java/com/example/controller/SchoolController.java @@ -9,6 +9,8 @@ import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.util.HashMap; +import java.util.Map; /** *

diff --git a/example/src/main/java/com/example/demo/CustomAsyncCreateTask.java b/example/src/main/java/com/example/demo/CustomAsyncCreateTask.java index de3a0094c..755dd2e6a 100644 --- a/example/src/main/java/com/example/demo/CustomAsyncCreateTask.java +++ b/example/src/main/java/com/example/demo/CustomAsyncCreateTask.java @@ -12,7 +12,7 @@ import lombok.extern.slf4j.Slf4j; * @date : 2022-03-07 14:07 * @since 1.3.0 */ -@ExecutorMethodRegister(scene = CustomAsyncCreateTask.SCENE, async = true) +@ExecutorMethodRegister(scene = CustomAsyncCreateTask.SCENE, async = true, forceReport = true) @Slf4j public class CustomAsyncCreateTask implements ExecutorMethod { diff --git a/example/src/main/java/com/example/demo/CustomSyncCreateTask.java b/example/src/main/java/com/example/demo/CustomSyncCreateTask.java index 4ebdf8f2c..19a902d30 100644 --- a/example/src/main/java/com/example/demo/CustomSyncCreateTask.java +++ b/example/src/main/java/com/example/demo/CustomSyncCreateTask.java @@ -14,7 +14,7 @@ import java.util.concurrent.TimeUnit; * @date : 2022-03-07 14:07 * @since 1.3.0 */ -@ExecutorMethodRegister(scene = CustomSyncCreateTask.SCENE, async = false, timeout = 10000, unit = TimeUnit.MILLISECONDS, forceReport = false) +@ExecutorMethodRegister(scene = CustomSyncCreateTask.SCENE, async = false, timeout = 10000, unit = TimeUnit.MILLISECONDS, forceReport = true) @Slf4j public class CustomSyncCreateTask implements ExecutorMethod { diff --git a/example/src/main/java/com/example/demo/NestMethodService.java b/example/src/main/java/com/example/demo/NestMethodService.java index 7e84d0d0f..21f684647 100644 --- a/example/src/main/java/com/example/demo/NestMethodService.java +++ b/example/src/main/java/com/example/demo/NestMethodService.java @@ -2,8 +2,11 @@ package com.example.demo; import com.aizuda.easy.retry.client.core.annotation.Retryable; import com.aizuda.easy.retry.client.core.exception.EasyRetryClientException; +import com.aizuda.easy.retry.client.core.intercepter.RetrySiteSnapshot; +import com.aizuda.easy.retry.client.core.intercepter.RetrySiteSnapshot.EnumStage; import com.aizuda.easy.retry.client.core.retryer.EasyRetryTemplate; import com.aizuda.easy.retry.client.core.retryer.RetryTaskTemplateBuilder; +import com.aizuda.easy.retry.common.core.model.Result; import com.example.model.Zoo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -22,6 +25,8 @@ public class NestMethodService { @Autowired private TestExistsTransactionalRetryService testExistsTransactionalRetryService; + @Autowired + private RemoteService remoteService; @Retryable(scene = "testNestMethod" , isThrowException = false) @Transactional @@ -33,12 +38,11 @@ public class NestMethodService { @Transactional public void testNestMethodForCustomSyncCreateTask() { - Random random = new Random(); - int i = random.nextInt(5); - if (i <= 2) { + if (RetrySiteSnapshot.getStage() == null || RetrySiteSnapshot.getStage() == EnumStage.LOCAL.getStage()) { throw new EasyRetryClientException("测试注解重试和手动重试"); } + // 同步强制上报 Zoo zoo = new Zoo(); zoo.setNow(LocalDateTime.now()); EasyRetryTemplate retryTemplate = RetryTaskTemplateBuilder.newBuilder() @@ -48,5 +52,14 @@ public class NestMethodService { .build(); retryTemplate.executeRetry(); + + // 异步强制上报 + zoo.setNow(LocalDateTime.now()); + retryTemplate = RetryTaskTemplateBuilder.newBuilder() + .withExecutorMethod(CustomAsyncCreateTask.class) + .withParam(zoo) + .withScene(CustomAsyncCreateTask.SCENE) + .build(); + retryTemplate.executeRetry(); } } diff --git a/example/src/main/java/com/example/demo/RemoteService.java b/example/src/main/java/com/example/demo/RemoteService.java index aabd430b7..88c54dae8 100644 --- a/example/src/main/java/com/example/demo/RemoteService.java +++ b/example/src/main/java/com/example/demo/RemoteService.java @@ -16,7 +16,7 @@ public class RemoteService { private RestTemplate restTemplate; public Result call() { - return restTemplate.getForObject("http://127.0.0.1:8088/school/id", Result.class); + return restTemplate.getForObject("http://127.0.0.1:8089/school/id", Result.class); // return new Result(); } } diff --git a/example/src/main/java/com/example/demo/TestExistsTransactionalRetryService.java b/example/src/main/java/com/example/demo/TestExistsTransactionalRetryService.java index 1c38f5be2..ebb455190 100644 --- a/example/src/main/java/com/example/demo/TestExistsTransactionalRetryService.java +++ b/example/src/main/java/com/example/demo/TestExistsTransactionalRetryService.java @@ -55,8 +55,8 @@ public class TestExistsTransactionalRetryService { throw new UnsupportedOperationException("调用远程失败" + school.getAddress()); } - TransactionalEvent event = new TransactionalEvent<>("123"); - SpringContext.CONTEXT.publishEvent(event); +// TransactionalEvent event = new TransactionalEvent<>("123"); +// SpringContext.CONTEXT.publishEvent(event); return "testSimpleInsert"+school.getAddress(); } diff --git a/example/src/test/java/com/example/EasyRetryTemplateTest.java b/example/src/test/java/com/example/EasyRetryTemplateTest.java index ae43bcc12..ecea92437 100644 --- a/example/src/test/java/com/example/EasyRetryTemplateTest.java +++ b/example/src/test/java/com/example/EasyRetryTemplateTest.java @@ -2,17 +2,22 @@ package com.example; import com.aizuda.easy.retry.client.core.retryer.EasyRetryTemplate; import com.aizuda.easy.retry.client.core.retryer.RetryTaskTemplateBuilder; +import com.aizuda.easy.retry.common.core.model.Result; import com.example.demo.CustomAsyncCreateTask; import com.example.demo.CustomSyncCreateTask; import com.example.demo.NestMethodService; +import com.example.demo.RemoteService; import com.example.model.Cat; import com.example.model.Zoo; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; import java.time.LocalDateTime; +import java.util.UUID; /** * @author: www.byteblogs.com @@ -24,6 +29,8 @@ public class EasyRetryTemplateTest { @Autowired private NestMethodService nestMethodService; + @MockBean + private RemoteService remoteService; @Test public void generateAsyncTaskTest() throws InterruptedException { diff --git a/example/src/test/java/com/example/ExistsTransactionalRetryServiceTest.java b/example/src/test/java/com/example/ExistsTransactionalRetryServiceTest.java index 4f3b5ef27..e13b50099 100644 --- a/example/src/test/java/com/example/ExistsTransactionalRetryServiceTest.java +++ b/example/src/test/java/com/example/ExistsTransactionalRetryServiceTest.java @@ -39,7 +39,7 @@ public class ExistsTransactionalRetryServiceTest { Mockito.when(remoteService.call()) .thenReturn(new Result(0, "1")) .thenReturn(new Result(0, "2")) - .thenReturn(new Result(3, "3")) + .thenReturn(new Result(0, "3")) .thenReturn(new Result(0, "4")) .thenReturn(new Result(0, "5")) ;