本节我们一起来学习sisyphus的函数式配置和注解式配置。功能配置概述为了满足更方便的配置,Retryer类提供了很多可配置的信息。默认配置/***默认配置测试*/publicvoiddefaultConfigTest(){Retryer.newInstance().condition(RetryConditions.hasExceptionCause()).retryWaitContext(RetryWaiter.retryWait(NoRetryWait.class).context()).maxAttempt(3).listen(RetryListens.noListen()).recover(Recovers.noRecover()).callable(newCallable(){@OverridepublicStringcall()throwsException{System.out.println("调用。..");thrownewRuntimeException();}}).retryCall();}相当于下面的代码:publicvoidhelloTest(){Retryer.newInstance().callable(newCallable(){@OverridepublicStringcall()throwsException{System.out.println("called...");thrownewRuntimeException();}}).retryCall();}方法描述了条件重试触发的条件,可以指定多个条件。默认是抛出异常。retryWaitContext重试等待策略,可以指定多个。默认是不等待。maxAttempt指定最大重试次数,包括第一次执行。默认值:3次。listen指定重试的监听实现,默认不监听。recover重试完成后,如果仍然满足重试条件,则可以指定恢复策略。默认情况下不进行任何恢复。要重试的可调用方法。retryCall触发重试执行。接口详细介绍Interface及其实现所有的接口都可以直接在对应的子类实例中查看。用户自定义基于替换的灵活性,用户可以自己实现接口,定义更符合自己业务的实现。sisyphus注解配置灵活度高,但对于开发者来说,却不如注解简单灵活。所以这个框架也实现了基于注解的重试。设计规范保证了接口和注解的统一。Maven引入了${project.groupId}sisyphus-annotation${project.version}注解主要是核心注解包括两个。Retry用于指定retry的相关配置。/***重试注释*1。只有在实际需要时才允许将其放置在方法上。*2.如果放在接口上,是不是所有的子类都会生效?为了简单明了,没有提供这样的实现。*3.保持注释和接口一致。{@linkcom.github.houbb.sisyphus.api.core.Retry}接口*@authorbinbin.hou*@since0.0.3*/@Documented@Inherited@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@RetryAble(DefaultRetryAbleHandler.class)public@interfaceRetry{/***重试类实现*@returnretry*@since0.0.5*/Classretry()defaultDefaultRetry。class;/***最大尝试次数*1。包含方法第一次正常执行次数*@returntimes*/intmaxAttempt()default3;/***重试触发场景*@return重试触发场景*/Classcondition()defaultExceptionCauseRetryCondition.class;/***监听器*1。默认不监听*@returnlistener*/Classlisten()defaultNoRetryListen.class;/***恢复操作*1。默认不进行恢复操作*@return恢复操作对应的class*/Classrecover()defaultNoRecover.class;/***等待策略*1。支持指定多个,不指定则不执行等待,*@return等待策略*/RetryWait[]waits()default{};}RetryWait用于指定重试的等待策略。packagecom.github.houbb.sisyphus.annotation.annotation;importcom.github.houbb.sisyphus.annotation.annotation.metadata.RetryWaitAble;importcom.github.houbb.sisyphus.annotation.handler.impl.DefaultRetryWaitAbleHandler;importcom.github.houbb。sisyphus.core.constant.RetryWaitConst;importcom.github.houbb.sisyphus.core.support.wait.NoRetryWait;importjava.lang.annotation.*;/***重试等待策略*1。为了对应重试策略,所有的内置注解都应该实现当前的注解。*2.是否允许自定义注释?**当注解+对象同时出现时,视为组合。**@authorbinbin.hou*@since0.0.3*/@Retention(RetentionPolicy.RUNTIME)@Inherited@Documented@Target(ElementType.ANNOTATION_TYPE)@RetryWaitAble(DefaultRetryWaitAbleHandler.class)public@interfaceRetryWait{/***默认值*1.fixed模式,对应固定等待时间*2.Incremental*@returndefaultvalue*/longvalue()defaultRetryWaitConst.VALUE_MILLS;/***最小值*@returnMinimumvalue*/longmin()defaultRetryWaitConst.MIN_MILLS;/***最大值*@return最大值*/longmax()defaultRetryWaitConst.MAX_MILLS;/***影响因子*1。增量重试,默认为{@linkRetryWaitConst#INCREASE_MILLS_FACTOR}*2。指数模式。默认为{@linkRetryWaitConst#MULTIPLY_FACTOR}*@return影响因子*/doublefactor()defaultDouble.MIN_VALUE;/***指定重试等待时间类信息*@return重试等待时间类*/ClassretryWait()defaultNoRetryWait.class;}注解的使用定义了注解之后,必然会有注解的相关使用。使用注解有两种主要方式。Proxy+CGLIB基于代理模式和字节码增强。如果项目中没有使用spring,直接使用这个方法比较方便。Spring-AOP可以直接与spring集成。用法和spring-retry一样。这些将在下一节中详细解释。总结灵活的配置更能满足实际生产和使用中的各种需求。总的来说,实际使用时推荐使用注解配置方式,非常简单方便。