/** * Get the parent context (the retry is in an inner "chunk" loop and we want * the exception to be handled at the outer "step" level). * @return the {@link RepeatContext} that should hold the exhausted flag. */ private RepeatContext getRepeatContext() { RepeatContext context = RepeatSynchronizationManager.getContext(); if (context.getParent() != null) { return context.getParent(); } return context; }
private boolean isMarkedComplete(RepeatContext context) { boolean complete = context.isCompleteOnly(); if (context.getParent() != null) { complete = complete || isMarkedComplete(context.getParent()); } if (complete) { logger.debug("Repeat is complete according to context alone."); } return complete; }
/** * Set current session and all ancestors (via parent) to complete., */ public static void setAncestorsCompleteOnly() { RepeatContext context = getContext(); while (context != null) { context.setCompleteOnly(); context = context.getParent(); } }
/** * Construct a new {@link RepeatContextCounter}. * * @param context the current context. * @param countKey the key to use to store the counter in the context. * @param useParent true if the counter is to be shared between siblings. * The state will be stored in the parent of the context (if it exists) * instead of the context itself. */ public RepeatContextCounter(RepeatContext context, String countKey, boolean useParent) { super(); Assert.notNull(context, "The context must be provided to initialize a counter"); this.countKey = countKey; this.useParent = useParent; RepeatContext parent = context.getParent(); if (this.useParent && parent != null) { this.context = parent; } else { this.context = context; } if (!this.context.hasAttribute(countKey)) { this.context.setAttribute(countKey, new AtomicInteger()); } }
/** * 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); }
@Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { count++; assertNotNull(context); assertNotSame("Nested batch should have new session", context, context.getParent()); assertSame(context, RepeatSynchronizationManager.getContext()); return RepeatStatus.FINISHED; } }) {
@Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { count++; assertNotNull(context); assertNotSame("Nested batch should have new session", context, context.getParent()); assertSame(context, RepeatSynchronizationManager.getContext()); return RepeatStatus.FINISHED; } }) {
@Override public RepeatStatus doInIteration(RepeatContext context) throws Exception { count++; assertNotNull(context); assertNotSame("Nested batch should have new session", context, context.getParent()); assertSame(context, RepeatSynchronizationManager.getContext()); return RepeatStatus.FINISHED; } }) {
/** * 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); }
/** * 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); }
/** * Get the parent context (the retry is in an inner "chunk" loop and we want * the exception to be handled at the outer "step" level). * @return the {@link RepeatContext} that should hold the exhausted flag. */ private RepeatContext getRepeatContext() { RepeatContext context = RepeatSynchronizationManager.getContext(); if (context.getParent() != null) { return context.getParent(); } return context; }
/** * Get the parent context (the retry is in an inner "chunk" loop and we want * the exception to be handled at the outer "step" level). * @return the {@link RepeatContext} that should hold the exhausted flag. */ private RepeatContext getRepeatContext() { RepeatContext context = RepeatSynchronizationManager.getContext(); if (context.getParent() != null) { return context.getParent(); } return context; }
/** * Get the parent context (the retry is in an inner "chunk" loop and we want * the exception to be handled at the outer "step" level). * @return the {@link RepeatContext} that should hold the exhausted flag. */ private RepeatContext getRepeatContext() { RepeatContext context = RepeatSynchronizationManager.getContext(); if (context.getParent() != null) { return context.getParent(); } return context; }
/** * Get the parent context (the retry is in an inner "chunk" loop and we want * the exception to be handled at the outer "step" level). * @return the {@link RepeatContext} that should hold the exhausted flag. */ private RepeatContext getRepeatContext() { RepeatContext context = RepeatSynchronizationManager.getContext(); if (context.getParent() != null) { return context.getParent(); } return context; }
private boolean isMarkedComplete(RepeatContext context) { boolean complete = context.isCompleteOnly(); if (context.getParent() != null) { complete = complete || isMarkedComplete(context.getParent()); } if (complete) { logger.debug("Repeat is complete according to context alone."); } return complete; }
/** * Set current session and all ancestors (via parent) to complete., */ public static void setAncestorsCompleteOnly() { RepeatContext context = getContext(); while (context != null) { context.setCompleteOnly(); context = context.getParent(); } }
/** * Construct a new {@link RepeatContextCounter}. * * @param context the current context. * @param countKey the key to use to store the counter in the context. * @param useParent true if the counter is to be shared between siblings. * The state will be stored in the parent of the context (if it exists) * instead of the context itself. */ public RepeatContextCounter(RepeatContext context, String countKey, boolean useParent) { super(); Assert.notNull(context, "The context must be provided to initialize a counter"); this.countKey = countKey; this.useParent = useParent; RepeatContext parent = context.getParent(); if (this.useParent && parent != null) { this.context = parent; } else { this.context = context; } if (!this.context.hasAttribute(countKey)) { this.context.setAttribute(countKey, new AtomicInteger()); } }