首页 > 数据库 > 数据库开发技术 > Fast-Retry高性能百万级任务重试框架

https://github.com/burukeYou/fast-retry
https://mp.weixin.qq.com/s/Rr1oR4ieoYSoU_jLjxybow
https://blog.csdn.net/qq_34905631/article/details/131187901?spm=1001.2014.3001.5501
https://www.easyretry.com/pages/db78e2/
https://snailjob.opensnail.com/
<dependency>
<groupId>io.github.burukeyou</groupId>
<artifactId>fast-retry-all</artifactId>
<version>0.2.0</version>
</dependency>
ExecutorService executorService = Executors.newFixedThreadPool(8);RetryQueue queue = new FastRetryQueue(executorService);RetryTask<String> task = new RetryTask<String>() {int result = 0 ;public long waitRetryTime() {return 2000;}public boolean retry() {return ++result < 5;}public String getResult() {return result + "";}};CompletableFuture<String> future = queue.submit(task);log.info("任务结束 结果:{}",future.get());
RetryResultPolicy<String> resultPolicy = result -> result.equals("444");FastRetryer<String> retryer = FastRetryBuilder.<String>builder().attemptMaxTimes(3).waitRetryTime(3, TimeUnit.SECONDS).retryIfException(true).retryIfExceptionOfType(TimeoutException.class).exceptionRecover(true).resultPolicy(resultPolicy).build();CompletableFuture<String> future = retryer.submit(() -> {log.info("重试");//throw new Exception("test");//int i = 1/0;if (0 < 10){throw new TimeoutException("test");}return "444";});String o = future.get();log.info("结果{}", o);
依赖Spring环境,所以需要在配置上加上@EnableFastRetry注解启用配置才生效
如果将结果类型使用CompletableFuture包装,自动进行异步轮询返回,否则同步阻塞等待重试结果。
下面定义等价于 FastRetryer.execute方法
(retryWait = (delay = 2))public String retryTask(){return "success";}
下面定义等价于 FastRetryer.submit方法,支持异步轮询
(retryWait = (delay = 2))public CompletableFuture<String> retryTask(){return CompletableFuture.completedFuture("success");}
下图是三者的性能对比
测试线程池: 8个固定线程
单个任务逻辑: 轮询5次,隔2秒重试一次,总耗时10秒
未测预计公式:当我们使用线程池的时候, 一般线程池中 总任务处理耗时 = 任务数/并发度 x 单个任务重试耗时
原文作者:Java大飞哥;如有侵权,请联系管理员删除。