当前位置: 首页 > news >正文

自己做的网站怎么发布win7除了红动中国还有哪些设计网站

自己做的网站怎么发布win7,除了红动中国还有哪些设计网站,设计教程网站有哪些,上海市崇明县建设中学网站文章目录 简要1.为什么需要重试#xff1f;2.添加maven依赖3.使用Retryable注解实现重试4.基于RetryTemplate模板实现重试 简要 Spring实现了一套重试机制#xff0c;功能简单实用。Spring Retry是从Spring Batch独立出来的一个功能#xff0c;已经广泛应用于Spring Batch,… 文章目录 简要1.为什么需要重试2.添加maven依赖3.使用Retryable注解实现重试4.基于RetryTemplate模板实现重试 简要 Spring实现了一套重试机制功能简单实用。Spring Retry是从Spring Batch独立出来的一个功能已经广泛应用于Spring Batch,Spring Integration, Spring for Apache Hadoop等Spring项目。 本文将讲述如何使用Spring Retry及其实现原理。 1.为什么需要重试 在调用一些第三方接口时候可能会因为网络或者服务方异常导致请求失败这个时候可以通过重试解决这种问题。 以下面的简单的例子来了解 Retry的功能下面有个doTask函数执行该函数的时候如果出现异常则需要重试任务 1.CountDownLatch 用于在主线程用于等待线程池中的任务完成2.AtomicInteger 类型用于计算重试次数3.ScheduledExecutorService用于定时执行需要重试的任务如果没有异常则第一次执行完任务则会关闭线程池4.doTask函数中通过 1/0故意造成异常 public static boolean doTask() {try {System.out.println(1/0);return true;} catch (Exception e) {return false;}}/*** 通过ScheduledExecutor定时器实现低配版本的重试机制* param args* throws Exception*/public static void main(String[] args) throws Exception {CountDownLatch countDownLatch new CountDownLatch(1);AtomicInteger count new AtomicInteger(0);//设置重试的次数int retryNumber 3;//创建单线程的定时任务处理器ScheduledExecutorService scheduledThreadPool Executors.newSingleThreadScheduledExecutor();//(参数1执行内容,参数2初始延迟时间,参数3任务间隔时间,参数4时间单位)scheduledThreadPool.scheduleAtFixedRate(() - {boolean flag doTask();//业务是否处理成功成功则关闭线程池if (flag || count.get() retryNumber) {//执行成功或者已达到执行次数则关闭线程池scheduledThreadPool.shutdownNow();countDownLatch.countDown();;}else{log.info(第{}次重试任务, count.get()1);count.getAndIncrement();}}, 0, 1, TimeUnit.SECONDS);//等待线程池中的任务完成countDownLatch.await();}把doTask函数中的导致异常的代码注释再运行可以看到控制台没有打印重试的信息 从上面代码可以看出写一个任务重试的工具不难感兴趣的同学可以通过AOP代理的方式自己实现基于注解的重试功能Spring官方的Retry模块里有通过Aop加注解的方式实现重试功能Aop玩腻了我就不造轮子了。。 2.添加maven依赖 由于retry依赖中没有包含aspectj相关依赖所以需要单独引用aspectj !-- https://mvnrepository.com/artifact/org.springframework.retry/spring-retry --dependencygroupIdorg.springframework.retry/groupIdartifactIdspring-retry/artifactIdversion1.3.4/version/dependency!--aop切面--dependencygroupIdorg.aspectj/groupIdartifactIdaspectjweaver/artifactIdversion1.9.4/version/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/dependency3.使用Retryable注解实现重试 3.1.需要在springboot启动添加EnableRetry注解开启对Retry的支持 EnableRetry SpringBootApplication public class RetryApplication { }3.2.定义需要重试任务的异常类型 public class CustomRecoveryException extends Exception{public CustomRecoveryException(String error){super(error);} }3.3.在需要任务重试的函数上面添加注解 value属性表示在哪些异常的情况下才会去重试多个类型用,隔开。maxAttempts属性设置执行次数默认值为3则表示异常后只会重试两次backoff属性设置下次重试的延迟时间默认值为1000ms(1秒)。 Slf4j Service public class RetryServer {Retryable(value {CustomRecoveryException.class, IOException.class}, maxAttempts 3, backoff Backoff(delay 1000))public void retryTest() throws CustomRecoveryException {log.info(retryTest,当前时间{},LocalDateTime.now());throw new CustomRecoveryException(test retry error);}3.4.通过Recover定义降级处理的函数 返回值需要和重试的任务一致要不然会抛出异常。 Recoverpublic void fallback(Throwable throwable) {// 降级处理逻辑log.error(fallback,Error msg:{},throwable.getMessage());return fallback;}} 3.5.使用junit进行测试 SpringBootTest class RetryApplicationTests {Autowiredprivate RetryServer retryServer;Testvoid contextLoads() throws CustomRecoveryException {retryServer.retryTest();} } 可以看到控制台只打印了三次日志,从这能确认任务共执行了三次只重试了两次。 4.基于RetryTemplate模板实现重试 4.1.配置RetryTemplate重试的策略 EnableRetry SpringBootApplication public class RetryApplication {public static void main(String[] args) {SpringApplication.run(RetryApplication.class, args);}Beanpublic RetryTemplate retryTemplate() {final SimpleRetryPolicy simpleRetryPolicy new SimpleRetryPolicy();//设置最多执行次数, 包含第一次执行,下面配置成3则第一次执行出现异常后最多会再重试2次simpleRetryPolicy.setMaxAttempts(3);final FixedBackOffPolicy fixedBackOffPolicy new FixedBackOffPolicy();//设置重试间隔时间 单位 msfixedBackOffPolicy.setBackOffPeriod(1000L);return RetryTemplate.builder().customPolicy(simpleRetryPolicy).customBackoff(fixedBackOffPolicy).build();} }4.2.添加任务重试失败之后的降级处理回调函数 Slf4j Component public class CustomRecoveryCallback implements RecoveryCallbackString {Overridepublic String recover(RetryContext retryContext) {log.error(fallback,retryCount:{},error msg:{},retryContext.getRetryCount(),retryContext.getLastThrowable().getMessage());return fallback;} }4.3.通过retryTemplate.execute执行需要重试的任务 Slf4j Service public class RetryServer {Autowiredprivate RetryTemplate retryTemplate;Autowiredprivate CustomRecoveryCallback customRecoveryCallback;public void retryTemplateTest() {//第一个参数是只需要执行的方法第二个参数是降级处理方法retryTemplate.execute(f-function(),customRecoveryCallback);}/*** 具体的执行任务*/public String function(){log.info(retryTemplateTest,当前时间{},LocalDateTime.now());throw new RuntimeException(test retry error);}}4.4.使用junit进行测试 SpringBootTest class RetryApplicationTests {Autowiredprivate RetryServer retryServer;Testvoid contextLoads() {retryServer.retryTemplateTest();}}可以看到测试得到的结果和注解的方式是一样的都只执行了三次任务。
http://www.pierceye.com/news/21182/

相关文章:

  • 建设职业技术学院网站做赚钱的网站有哪些
  • 上海建设工程造价信息网站网店无货源怎么做
  • 网站添加锚点青岛网站排名公司
  • thinkphp企业网站开发西安市环评建设备案网站
  • 网站建设柒首先金手指2wordpress萨龙怎么使用
  • 福建省网站备案用户注销珠海 网站开发
  • wordpress全站采集网站到底备案好不好
  • 网站开发环境各大网站的名字
  • 福州晋安区建设局网站动漫设计专修学校
  • 网站数据库查询怎么做奎屯市住房和城乡建设局网站
  • 请人做网站收费多少钱投资网站哪个好
  • 科技公司网站设计做网站应该用什么语言来开发
  • 佛山网站快照优化公司吾道ppt模板免费下载
  • 一般网站的架构做企业网站用什么软件
  • 网站正在建设中色综合企业网站设计需求文档
  • 开网站建设公司心得常州平台网站建设
  • 复刻手表网站上海静安网站制作
  • 东莞网站快速排名深圳广告宣传片拍摄
  • 纯文本网站建设智慧团建注册登录入口电脑版
  • 个人怎么注册自己的网站做网站还需要买服务器么
  • 建设网站市场规模专业网站定制团队
  • 安阳企业建网站免费空间申请2018
  • 做ppt赚钱的网站十款app软件下载入口
  • 买个网站需要多少钱开发app订制软件
  • 做窗帘网站图片公司免费网站注册
  • asp网站内容优化方案
  • 上海嘉定区网站建设公司美食网站的建设
  • 网站购物功能如何做seo外贸公司推广
  • 网站怎么在工信部备案信息网站建设与管理实践
  • 洛阳网站建设设计公司哪家好wordpress是英文的怎么办