public CircuitBreakerRetryPolicy() { this(new SimpleRetryPolicy()); }
SimpleRetryPolicy simple = hasExpression ? new ExpressionRetryPolicy(resolve(exceptionExpression)) .withBeanFactory(this.beanFactory) : new SimpleRetryPolicy(); simple.setMaxAttempts(maxAttempts); return simple; return new SimpleRetryPolicy(maxAttempts, policyMap, true, retryNotExcluded);
/** * Apply the max attempts - a SimpleRetryPolicy will be used. Cannot be used if a custom retry operations * or retry policy has been set. * @param maxAttempts the max attempts (including the initial attempt). * @return this. */ public RetryInterceptorBuilder<T> maxAttempts(int maxAttempts) { Assert.isNull(this.retryOperations, "cannot alter the retry policy when a custom retryOperations has been set"); Assert.isTrue(!this.retryPolicySet, "cannot alter the retry policy when a custom retryPolicy has been set"); this.simpleRetryPolicy.setMaxAttempts(maxAttempts); this.retryTemplate.setRetryPolicy(this.simpleRetryPolicy); this.templateAltered = true; return this; }
/** * Apply the max attempts - a SimpleRetryPolicy will be used. Cannot be used if a custom retry operations * or retry policy has been set. * @param maxAttempts the max attempts (including the initial attempt). * @return this. */ public RetryInterceptorBuilder<T> maxAttempts(int maxAttempts) { Assert.isNull(this.retryOperations, "cannot alter the retry policy when a custom retryOperations has been set"); Assert.isTrue(!this.retryPolicySet, "cannot alter the retry policy when a custom retryPolicy has been set"); this.simpleRetryPolicy.setMaxAttempts(maxAttempts); this.retryTemplate.setRetryPolicy(this.simpleRetryPolicy); this.templateAltered = true; return this; }
@Bean @ConditionalOnMissingBean(name = "statsRetryTemplate") public RetryTemplate statsRetryTemplate() { RetryTemplate retryTemplate = new RetryTemplate(); ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy(); backOffPolicy.setInitialInterval(3000L); backOffPolicy.setMultiplier(3); SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy( this.statsProperties.getElastic().getMaxAttempts(), Collections.singletonMap(Exception.class, true)); retryTemplate.setBackOffPolicy(backOffPolicy); retryTemplate.setRetryPolicy(retryPolicy); return retryTemplate; }
@Test // BATCH-2663 public void testFilterCountOnSkipInWriteWithRetry() throws Exception { SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(); retryPolicy.setMaxAttempts(3); batchRetryTemplate.setRetryPolicy(retryPolicy); processor.setWriteSkipPolicy(new AlwaysSkipItemSkipPolicy()); processor.setItemProcessor(new ItemProcessor<String, String>() { @Override public String process(String item) throws Exception { if (item.equals("1")) { return null; } return item; } }); Chunk<String> inputs = new Chunk<>(Arrays.asList("fail", "1", "2")); processAndExpectPlannedRuntimeException(inputs); // (first attempt) Process fail, 1, 2 // item 1 is filtered out so it is removed from the chunk => now inputs = [fail, 2] processAndExpectPlannedRuntimeException(inputs); // (first retry) Process fail, 2 processAndExpectPlannedRuntimeException(inputs); // (second retry) Process fail, 2 // retry exhausted (maxAttempts = 3) => now scanning processAndExpectPlannedRuntimeException(inputs); // (scanning) Process fail processor.process(contribution, inputs); // (scanning) Process 2 assertEquals(1, list.size()); assertEquals("[2]", list.toString()); assertEquals(1, contribution.getWriteSkipCount()); assertEquals(3, contribution.getFilterCount()); }
/** * Apply the max attempts - a SimpleRetryPolicy will be used. Cannot be used if a custom retry operations * or retry policy has been set. * @param maxAttempts the max attempts. * @return this. */ public B maxAttempts(int maxAttempts) { Assert.isNull(this.retryOperations, "cannot alter the retry policy when a custom retryOperations has been set"); Assert.isTrue(!this.retryPolicySet, "cannot alter the retry policy when a custom retryPolicy has been set"); this.simpleRetryPolicy.setMaxAttempts(maxAttempts); this.retryTemplate.setRetryPolicy(this.simpleRetryPolicy); this.templateAltered = true; return _this(); }
retryableExceptionClasses); map.put(ForceRollbackForWriteSkipException.class, true); simpleRetryPolicy = new SimpleRetryPolicy(retryLimit, map);
@Test SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(); retryPolicy.setMaxAttempts(2); batchRetryTemplate.setRetryPolicy(retryPolicy); processor.setWriteSkipPolicy(new LimitCheckingItemSkipPolicy(1,
/** * Apply the max attempts - a SimpleRetryPolicy will be used. Cannot be used if a custom retry operations * or retry policy has been set. * @param maxAttempts the max attempts. * @return this. */ public B maxAttempts(int maxAttempts) { Assert.isNull(this.retryOperations, "cannot alter the retry policy when a custom retryOperations has been set"); Assert.isTrue(!this.retryPolicySet, "cannot alter the retry policy when a custom retryPolicy has been set"); this.simpleRetryPolicy.setMaxAttempts(maxAttempts); this.retryTemplate.setRetryPolicy(this.simpleRetryPolicy); this.templateAltered = true; return _this(); }
@Override public boolean canRetry(RetryContext context) { Throwable t = context.getLastThrowable(); if (t instanceof ResponseMessageException) { int maxRetryCount = ((ResponseMessageException) t).getMaxRetryCount(); return context.getRetryCount() < maxRetryCount; } log.warn("Policy is used for a wrong exception: {}. Falling back to SimpleRetryPolicy.", (t == null ? null : t.getClass().getSimpleName())); return super.canRetry(context); } }
assertInitialState(); retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1, Collections .<Class<? extends Throwable>, Boolean> singletonMap(Exception.class, true)));
@Test public void testWriteRetryOnException() throws Exception { SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(); retryPolicy.setMaxAttempts(2); batchRetryTemplate.setRetryPolicy(retryPolicy); processor.setWriteSkipPolicy(new AlwaysSkipItemSkipPolicy());
@Override public boolean canRetry(RetryContext context) { Throwable t = context.getLastThrowable(); if (t instanceof ResponseMessageException) { int maxRetryCount = ((ResponseMessageException) t).getMaxRetryCount(); return context.getRetryCount() < maxRetryCount; } log.warn("Policy is used for a wrong exception: {}. Falling back to SimpleRetryPolicy.", (t == null ? null : t.getClass().getSimpleName())); return super.canRetry(context); } }
template.setRetryPolicy(new SimpleRetryPolicy(1, Collections .<Class<? extends Throwable>, Boolean> singletonMap(Exception.class, true)));
@Test public void testWriteRetryOnTwoExceptions() throws Exception { SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(); retryPolicy.setMaxAttempts(2); batchRetryTemplate.setRetryPolicy(retryPolicy); processor.setWriteSkipPolicy(new AlwaysSkipItemSkipPolicy());
@Test(expected = ExhaustedRetryException.class) public void testExhaustedRetry() throws Exception { BatchRetryTemplate template = new BatchRetryTemplate(); template.setRetryPolicy(new SimpleRetryPolicy(1, Collections .<Class<? extends Throwable>, Boolean> singletonMap(Exception.class, true))); RetryCallback<String[], Exception> retryCallback = new RetryCallback<String[], Exception>() { @Override public String[] doWithRetry(RetryContext context) throws Exception { if (count++ < 2) { throw new RecoverableException("Recoverable"); } return outputs.toArray(new String[0]); } }; outputs = Arrays.asList("a", "b"); try { template.execute(retryCallback, BatchRetryTemplate.createState(outputs)); fail("Expected RecoverableException"); } catch (RecoverableException e) { assertEquals("Recoverable", e.getMessage()); } outputs = Arrays.asList("a", "c"); template.execute(retryCallback, BatchRetryTemplate.createState(outputs)); }
@Component("yourRetryPolicy") public class YourRetryPolicy extends ExceptionClassifierRetryPolicy { @PostConstruct public void init() { final SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy(); simpleRetryPolicy.setMaxAttempts( 3 ); this.setExceptionClassifier( new Classifier<Throwable, RetryPolicy>() { @Override public RetryPolicy classify( Throwable classifiable ) { if ( classifiable instanceof YourException ) { return new NeverRetryPolicy(); } // etc... return simpleRetryPolicy; } } ); }