spring retry-2 (RetryTemplate)
阿新 • • 發佈:2018-12-11
首先參照retry使用新增依賴
然後配置RetryTemplate:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.retry.RetryCallback; import org.springframework.retry.RetryContext; import org.springframework.retry.RetryListener; import org.springframework.retry.backoff.FixedBackOffPolicy; import org.springframework.retry.policy.SimpleRetryPolicy; import org.springframework.retry.policy.TimeoutRetryPolicy; import org.springframework.retry.support.RetryTemplate; import java.util.HashMap; import java.util.Map; /** * Author black * Date 2018/8/31 * Time 14:25 */ @Configuration public class RetryConfig { @Bean(name = "attemptsRetry") public RetryTemplate attemptsRetry() { RetryTemplate retryTemplate = new RetryTemplate(); retryTemplate.setListeners(getRetryListener()); return retryTemplate; } @Bean(name = "timeoutRetry") public RetryTemplate timeoutRetry() { RetryTemplate retryTemplate = new RetryTemplate(); TimeoutRetryPolicy policy = new TimeoutRetryPolicy(); policy.setTimeout(1000); retryTemplate.setRetryPolicy(policy); return retryTemplate; } public static RetryListener[] getRetryListener() { RetryListener[] listeners = { new RetryListener() { @Override public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) { System.err.println("start"); //todo 方法準備執行處理 return true; } @Override public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) { //todo 方法重試完畢處理 System.err.println("stop"); } @Override public <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) { //todo 方法執行異常處理 System.err.println("exception"); } } }; return listeners; } public static RetryTemplate setPolicy(RetryTemplate retryTemplate) { Map<Class<? extends Throwable>, Boolean> retryableExceptions = new HashMap<>(); //設定重試異常和是否重試 retryableExceptions.put(RetryService.RetryException.class, true); //設定重試次數和要重試的異常 SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(5,retryableExceptions); retryTemplate.setRetryPolicy(retryPolicy); FixedBackOffPolicy backoffPolicy = new FixedBackOffPolicy(); //設定重試間隔 mills backoffPolicy.setBackOffPeriod(5000); retryTemplate.setBackOffPolicy(backoffPolicy); return retryTemplate; } }
建立service使用retrytemplate:
import com.alibaba.dubbo.config.annotation.Service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.retry.RetryCallback; import org.springframework.retry.RetryContext; import org.springframework.retry.support.RetryTemplate; import static com.springboot.black.springboot_test.retry.RetryConfig.setPolicy; /** * Author zhangxiaopeng * Date 2018/8/31 * Time 14:31 */ @Service public class RetryTemplateService { @Autowired @Qualifier("attemptsRetry") private RetryTemplate attemptsRetry; public void testretry() throws Throwable { setPolicy(attemptsRetry); RetryCallback<Object, Throwable> objectThrowableRetryCallback = new RetryCallback<Object, Throwable>() { @Override public Object doWithRetry(RetryContext context) throws Throwable { System.err.println("1"); //業務邏輯 throw new RetryService.RetryException();//模擬異常場景 // throw new RuntimeException("1111"); // return null; } }; attemptsRetry.execute(objectThrowableRetryCallback); } }
輸出結果: