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

网站换程序301汉中城乡建设网站

网站换程序301,汉中城乡建设网站,网站服务公司哪个好,如何把做的网站与域名连接如果您需要在代码中实现健壮的重试逻辑#xff0c;一种行之有效的方法是使用spring重试库。 我的目的不是要展示如何使用spring retry项目本身#xff0c;而是要展示可以将其集成到代码库中的不同方式。 考虑一种服务来调用外部系统#xff1a; package retry.service;pub… 如果您需要在代码中实现健壮的重试逻辑一种行之有效的方法是使用spring重试库。 我的目的不是要展示如何使用spring retry项目本身而是要展示可以将其集成到代码库中的不同方式。 考虑一种服务来调用外部系统 package retry.service;public interface RemoteCallService {String call() throws Exception; } 假定此调用可能失败并且您希望每次调用失败都可以重试三次该调用每次延迟2秒所以为了模拟此行为我已经使用Mockito定义了模拟服务请注意此返回为嘲笑的Spring bean Bean public RemoteCallService remoteCallService() throws Exception {RemoteCallService remoteService mock(RemoteCallService.class);when(remoteService.call()).thenThrow(new RuntimeException(Remote Exception 1)).thenThrow(new RuntimeException(Remote Exception 2)).thenReturn(Completed);return remoteService; } 因此该模拟服务本质上将失败2次并在第三个调用成功。 这是对重试逻辑的测试 public class SpringRetryTests {Autowiredprivate RemoteCallService remoteCallService;Testpublic void testRetry() throws Exception {String message this.remoteCallService.call();verify(remoteCallService, times(3)).call();assertThat(message, is(Completed));} } 我们确保该服务被调用3次以解决前两个失败的呼叫以及成功的第三个呼叫。 如果我们在调用此服务时直接合并spring-retry则代码将如下所示 Test public void testRetry() throws Exception {String message this.retryTemplate.execute(context - this.remoteCallService.call());verify(remoteCallService, times(3)).call();assertThat(message, is(Completed)); } 但是这不是理想的选择更好的包含方式是调用者不必明确知道存在重试逻辑的事实。 鉴于此以下是合并Spring重试逻辑的方法。 方法1自定义方面合并Spring重试 这种方法应该非常直观因为可以将重试逻辑视为跨领域关注点并且使用Aspects是实现跨领域关注点的好方法。 包含Spring重试的一个方面将遵循以下原则 package retry.aspect;import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.retry.support.RetryTemplate;Aspect public class RetryAspect {private static Logger logger LoggerFactory.getLogger(RetryAspect.class);Autowiredprivate RetryTemplate retryTemplate;Pointcut(execution(* retry.service..*(..)))public void serviceMethods() {//}Around(serviceMethods())public Object aroundServiceMethods(ProceedingJoinPoint joinPoint) {try {return retryTemplate.execute(retryContext - joinPoint.proceed());} catch (Throwable e) {throw new RuntimeException(e);}} } 这方面拦截了远程服务调用并将该调用委托给retryTemplate。 完整的工作测试在这里 。 方法2使用Spring-retry提供的建议 Spring-retry项目提供了开箱即用的建议可确保确保可以重试目标服务。 围绕服务编织建议的AOP配置需要处理原始xml这与以前的方法不同前一种方法可以使用Spring Java配置来编织方面。 xml配置如下所示 ?xml version1.0 encodingUTF-8? beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdaop:configaop:pointcut idtransactionalexpressionexecution(* retry.service..*(..)) /aop:advisor pointcut-reftransactionaladvice-refretryAdvice order-1//aop:config/beans 完整的工作测试在这里 。 方法3声明式重试逻辑 这是推荐的方法您将看到代码比前两种方法更加简洁。 使用这种方法唯一需要做的就是声明性地指出需要重试的方法 package retry.service;import org.springframework.retry.annotation.Backoff; import org.springframework.retry.annotation.Retryable;public interface RemoteCallService {Retryable(maxAttempts 3, backoff Backoff(delay 2000))String call() throws Exception; } 以及使用此声明性重试逻辑的完整测试也可以在此处获取 package retry;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.retry.annotation.EnableRetry; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import retry.service.RemoteCallService;import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.mockito.Mockito.*;RunWith(SpringJUnit4ClassRunner.class) ContextConfiguration public class SpringRetryDeclarativeTests {Autowiredprivate RemoteCallService remoteCallService;Testpublic void testRetry() throws Exception {String message this.remoteCallService.call();verify(remoteCallService, times(3)).call();assertThat(message, is(Completed));}ConfigurationEnableRetrypublic static class SpringConfig {Beanpublic RemoteCallService remoteCallService() throws Exception {RemoteCallService remoteService mock(RemoteCallService.class);when(remoteService.call()).thenThrow(new RuntimeException(Remote Exception 1)).thenThrow(new RuntimeException(Remote Exception 2)).thenReturn(Completed);return remoteService;}} } EnableRetry批注激活Retryable批注方法的处理并在内部使用方法2的逻辑而最终用户无需明确说明。 我希望这会使您对如何将Spring-retry合并到项目中有所了解。 我在这里演示的所有代码也可以在我的github项目中找到 https//github.com/bijukunjummen/test-spring-retry 翻译自: https://www.javacodegeeks.com/2014/12/spring-retry-ways-to-integrate-with-your-project.html
http://www.sadfv.cn/news/206259/

相关文章:

  • 如何做电影网站狼视听兰州起点网站建设公司
  • 河北seo网站优化公司如何做好企业网站建设
  • 网站建设人员组成wordpress 设置页面
  • 重庆平台网站推广加强网站政务服务建设
  • 网站手机客户端如何开发网站建设一般分为几个步骤
  • wordpress网站怎么进去做网站找不到客户
  • 广州seo网站推广优化玄武建设局网站
  • 100元网站建设wordpress the7 建站
  • 湘潭企业网站建设 磐石网络wordpress顶部悬浮
  • 大屏网页设计网站如何建立网站详细流程
  • 如何弄自己的网站邯郸建设网站
  • 网站建设需求流程图wordpress整站搬家教程
  • 网站设计建设方案查询关键词
  • 唐兴数码网站同一域名可以做相同网站吗
  • 网站logo怎么做才清晰番禺俊才网官网
  • 无锡易时代网站建设有限公司怎么样上海做网站就用乐云seo十年
  • 上门做网站公司哪家好普陀区建设工程质检网站
  • 济宁网站建设服务阿里巴巴的网络营销方式
  • 外地公司做的网站能备案青岛网站建设‘’
  • 营销网站制作免费咨询点击宝seo
  • 四川省住建设厅网站html5制作网站首页
  • 旅游网站建设策划微信公众号微网站制作
  • dede减肥网站源码互联网营销师培训学校
  • 重庆市建设安全监督站的网站九江建设局网站
  • 快速网站搭建哪些网站有中文域名
  • 如何在淘宝上接单网站建设营销网站建设方案
  • 搜狐员工做网站的工资多少钱wordpress 相册 时间轴
  • 新网站前期seo怎么做免费源码资源分享网
  • 合肥做网站的公司有哪些北京有哪些网站建设公司
  • 兴义 网站建设凡科网站怎么建设个人网站