SimpleRetryExceptionHandler exceptionHandler = new SimpleRetryExceptionHandler(retryPolicyWrapper, getExceptionHandler(), nonRetryableExceptionClasses); ((RepeatTemplate) stepOperations).setExceptionHandler(exceptionHandler);
/** * If retry is exhausted set up some state in the context that can be used * to signal that the exception should be handled. * * @see org.springframework.retry.RetryListener#close(org.springframework.retry.RetryContext, * org.springframework.retry.RetryCallback, java.lang.Throwable) */ @Override public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) { if (!retryPolicy.canRetry(context)) { if (logger.isDebugEnabled()) { logger.debug("Marking retry as exhausted: "+context); } getRepeatContext().setAttribute(EXHAUSTED, "true"); } }
/** * @param retryPolicy * @param ex * @return */ private SimpleRetryExceptionHandler getHandlerAfterRetry(RetryPolicy retryPolicy, RuntimeException ex, Collection<Class<? extends Throwable>> fatalExceptions) { // Always rethrow if the retry is exhausted SimpleRetryExceptionHandler handler = new SimpleRetryExceptionHandler(retryPolicy, new SimpleLimitExceptionHandler(0), fatalExceptions); // Simulate a failed retry... RetryContext retryContext = retryPolicy.open(null); retryPolicy.registerThrowable(retryContext, ex); handler.close(retryContext, null, ex); return handler; }
/** * Test method for * {@link org.springframework.batch.core.step.item.SimpleRetryExceptionHandler#handleException(org.springframework.batch.repeat.RepeatContext, java.lang.Throwable)} . */ public void testNoRethrowWhenRetryNotExhausted() throws Throwable { RetryPolicy retryPolicy = new AlwaysRetryPolicy(); RuntimeException ex = new RuntimeException("foo"); SimpleRetryExceptionHandler handler = getHandlerAfterRetry(retryPolicy, ex, Collections .<Class<? extends Throwable>> singleton(Error.class)); // Then pretend to handle the exception in the parent context... handler.handleException(context.getParent(), ex); assertEquals(0, context.attributeNames().length); assertEquals(0, context.getParent().attributeNames().length); }
/** * Test method for * {@link org.springframework.batch.core.step.item.SimpleRetryExceptionHandler#handleException(org.springframework.batch.repeat.RepeatContext, java.lang.Throwable)} . */ public void testRethrowWhenFatal() throws Throwable { RetryPolicy retryPolicy = new AlwaysRetryPolicy(); RuntimeException ex = new RuntimeException("foo"); SimpleRetryExceptionHandler handler = getHandlerAfterRetry(retryPolicy, ex, Collections .<Class<? extends Throwable>> singleton(RuntimeException.class)); // Then pretend to handle the exception in the parent context... try { handler.handleException(context.getParent(), ex); fail("Expected RuntimeException"); } catch (RuntimeException e) { assertEquals(ex, e); } assertEquals(0, context.attributeNames().length); // One for the counter in the delegate exception handler assertEquals(1, context.getParent().attributeNames().length); }
SimpleRetryExceptionHandler exceptionHandler = new SimpleRetryExceptionHandler(retryPolicyWrapper, getExceptionHandler(), nonRetryableExceptionClasses); ((RepeatTemplate) stepOperations).setExceptionHandler(exceptionHandler);
/** * If retry is exhausted set up some state in the context that can be used * to signal that the exception should be handled. * * @see org.springframework.retry.RetryListener#close(org.springframework.retry.RetryContext, * org.springframework.retry.RetryCallback, java.lang.Throwable) */ @Override public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) { if (!retryPolicy.canRetry(context)) { if (logger.isDebugEnabled()) { logger.debug("Marking retry as exhausted: "+context); } getRepeatContext().setAttribute(EXHAUSTED, "true"); } }
/** * Test method for * {@link org.springframework.batch.core.step.item.SimpleRetryExceptionHandler#handleException(org.springframework.batch.repeat.RepeatContext, java.lang.Throwable)} . */ public void testRethrowWhenRetryExhausted() throws Throwable { RetryPolicy retryPolicy = new NeverRetryPolicy(); RuntimeException ex = new RuntimeException("foo"); SimpleRetryExceptionHandler handler = getHandlerAfterRetry(retryPolicy, ex, Collections .<Class<? extends Throwable>> singleton(Error.class)); // Then pretend to handle the exception in the parent context... try { handler.handleException(context.getParent(), ex); fail("Expected RuntimeException"); } catch (RuntimeException e) { assertEquals(ex, e); } assertEquals(0, context.attributeNames().length); // One for the retry exhausted flag and one for the counter in the // delegate exception handler assertEquals(2, context.getParent().attributeNames().length); }
SimpleRetryExceptionHandler exceptionHandler = new SimpleRetryExceptionHandler(retryPolicyWrapper, getExceptionHandler(), nonRetryableExceptionClasses); ((RepeatTemplate) stepOperations).setExceptionHandler(exceptionHandler);
/** * If retry is exhausted set up some state in the context that can be used * to signal that the exception should be handled. * * @see org.springframework.batch.retry.RetryListener#close(org.springframework.batch.retry.RetryContext, * org.springframework.batch.retry.RetryCallback, java.lang.Throwable) */ public <T> void close(RetryContext context, RetryCallback<T> callback, Throwable throwable) { if (!retryPolicy.canRetry(context)) { logger.debug("Marking retry as exhausted: "+context); getRepeatContext().setAttribute(EXHAUSTED, "true"); } }
SimpleRetryExceptionHandler exceptionHandler = new SimpleRetryExceptionHandler(retryPolicyWrapper, getExceptionHandler(), nonRetryableExceptionClasses); ((RepeatTemplate) stepOperations).setExceptionHandler(exceptionHandler);
/** * If retry is exhausted set up some state in the context that can be used * to signal that the exception should be handled. * * @see org.springframework.retry.RetryListener#close(org.springframework.retry.RetryContext, * org.springframework.retry.RetryCallback, java.lang.Throwable) */ @Override public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) { if (!retryPolicy.canRetry(context)) { logger.debug("Marking retry as exhausted: "+context); getRepeatContext().setAttribute(EXHAUSTED, "true"); } }
SimpleRetryExceptionHandler exceptionHandler = new SimpleRetryExceptionHandler(retryPolicyWrapper, getExceptionHandler(), nonRetryableExceptionClasses); ((RepeatTemplate) stepOperations).setExceptionHandler(exceptionHandler);
/** * If retry is exhausted set up some state in the context that can be used * to signal that the exception should be handled. * * @see org.springframework.retry.RetryListener#close(org.springframework.retry.RetryContext, * org.springframework.retry.RetryCallback, java.lang.Throwable) */ @Override public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) { if (!retryPolicy.canRetry(context)) { if (logger.isDebugEnabled()) { logger.debug("Marking retry as exhausted: "+context); } getRepeatContext().setAttribute(EXHAUSTED, "true"); } }