From 5205c43e7d624c7320768ad2c40ac77348537fbe Mon Sep 17 00:00:00 2001 From: Michael Minella Date: Wed, 9 Jan 2013 09:29:05 -0600 Subject: [PATCH] BATCH-1943: Added ChunkContext to all ChunkListener calls --- .../batch/core/ChunkListener.java | 8 ++++-- .../batch/core/annotation/AfterChunk.java | 7 +++--- .../core/annotation/AfterChunkError.java | 2 +- .../batch/core/annotation/BeforeChunk.java | 9 ++++--- .../core/listener/ChunkListenerSupport.java | 5 ++-- .../core/listener/CompositeChunkListener.java | 14 +++++------ .../listener/MulticasterBatchListener.java | 12 ++++----- .../core/listener/StepListenerMetaData.java | 4 +-- .../core/listener/StepListenerSupport.java | 8 +++--- .../builder/FaultTolerantStepBuilder.java | 8 +++--- .../batch/core/step/tasklet/TaskletStep.java | 4 +-- .../listener/CompositeChunkListenerTests.java | 11 ++++---- .../MulticasterBatchListenerTests.java | 25 ++++++++++--------- .../StepListenerFactoryBeanTests.java | 4 +-- ...tTolerantStepFactoryBeanRollbackTests.java | 4 +-- .../FaultTolerantStepFactoryBeanTests.java | 4 +-- .../step/item/SimpleStepFactoryBeanTests.java | 8 +++--- 17 files changed, 73 insertions(+), 64 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/ChunkListener.java b/spring-batch-core/src/main/java/org/springframework/batch/core/ChunkListener.java index c314d22a1..8596bd4d3 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/ChunkListener.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/ChunkListener.java @@ -32,13 +32,17 @@ public interface ChunkListener extends StepListener { /** * Callback before the chunk is executed, but inside the transaction. + * + * @param context The current {@link ChunkContext} */ - void beforeChunk(); + void beforeChunk(ChunkContext context); /** * Callback after the chunk is executed, outside the transaction. + * + * @param context The current {@link ChunkContext} */ - void afterChunk(); + void afterChunk(ChunkContext context); /** * Callback after a chunk has been marked for rollback. It is invoked diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/annotation/AfterChunk.java b/spring-batch-core/src/main/java/org/springframework/batch/core/annotation/AfterChunk.java index 3d5ab26cd..2a0366f59 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/annotation/AfterChunk.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/annotation/AfterChunk.java @@ -21,15 +21,16 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.batch.core.ChunkListener; +import org.springframework.batch.core.scope.context.ChunkContext; /** * Marks a method to be called after a chunk is executed.
*
- * Expected signature: void afterChunk() - * + * Expected signature: void afterChunk(ChunkContext context) + * * @author Lucas Ward * @since 2.0 - * @see ChunkListener#afterChunk() + * @see ChunkListener#afterChunk(ChunkContext context) */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/annotation/AfterChunkError.java b/spring-batch-core/src/main/java/org/springframework/batch/core/annotation/AfterChunkError.java index 65c92159e..eba8a66a2 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/annotation/AfterChunkError.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/annotation/AfterChunkError.java @@ -31,7 +31,7 @@ import org.springframework.batch.core.scope.context.ChunkContext; * * @author Michael Minella * @since 2.2 - * @see ChunkListener#afterChunkError(ChunkContext) + * @see ChunkListener#afterChunkError(ChunkContext context) */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/annotation/BeforeChunk.java b/spring-batch-core/src/main/java/org/springframework/batch/core/annotation/BeforeChunk.java index 00beb461b..fa86e9ab0 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/annotation/BeforeChunk.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/annotation/BeforeChunk.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2008 the original author or authors. + * Copyright 2002-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,15 +21,16 @@ import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.batch.core.ChunkListener; +import org.springframework.batch.core.scope.context.ChunkContext; /** * Marks a method to be called before a chunk is executed.
*
- * Expected signature: void beforeChunk() - * + * Expected signature: void beforeChunk(ChunkContext context) + * * @author Lucas Ward * @since 2.0 - * @see ChunkListener#beforeChunk() + * @see ChunkListener#beforeChunk(ChunkContext context) */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/listener/ChunkListenerSupport.java b/spring-batch-core/src/main/java/org/springframework/batch/core/listener/ChunkListenerSupport.java index e36fef4e0..65d00cc1f 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/listener/ChunkListenerSupport.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/listener/ChunkListenerSupport.java @@ -22,6 +22,7 @@ import org.springframework.batch.core.scope.context.ChunkContext; * Basic support implementation of {@link ChunkListener} * * @author Lucas Ward + * @author Michael Minella * */ public class ChunkListenerSupport implements ChunkListener { @@ -30,14 +31,14 @@ public class ChunkListenerSupport implements ChunkListener { * @see org.springframework.batch.core.domain.ChunkListener#afterChunk() */ @Override - public void afterChunk() { + public void afterChunk(ChunkContext context) { } /* (non-Javadoc) * @see org.springframework.batch.core.domain.ChunkListener#beforeChunk() */ @Override - public void beforeChunk() { + public void beforeChunk(ChunkContext context) { } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/listener/CompositeChunkListener.java b/spring-batch-core/src/main/java/org/springframework/batch/core/listener/CompositeChunkListener.java index 0207b717c..f6982aa4c 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/listener/CompositeChunkListener.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/listener/CompositeChunkListener.java @@ -49,29 +49,29 @@ public class CompositeChunkListener implements ChunkListener { } /** - * Call the registered listeners in order, respecting and prioritising those + * Call the registered listeners in order, respecting and prioritizing those * that implement {@link Ordered}. * - * @see org.springframework.batch.core.ChunkListener#afterChunk() + * @see org.springframework.batch.core.ChunkListener#afterChunk(ChunkContext context) */ @Override - public void afterChunk() { + public void afterChunk(ChunkContext context) { for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { ChunkListener listener = iterator.next(); - listener.afterChunk(); + listener.afterChunk(context); } } /** * Call the registered listeners in reverse order. * - * @see org.springframework.batch.core.ChunkListener#beforeChunk() + * @see org.springframework.batch.core.ChunkListener#beforeChunk(ChunkContext context) */ @Override - public void beforeChunk() { + public void beforeChunk(ChunkContext context) { for (Iterator iterator = listeners.reverse(); iterator.hasNext();) { ChunkListener listener = iterator.next(); - listener.beforeChunk(); + listener.beforeChunk(context); } } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/listener/MulticasterBatchListener.java b/spring-batch-core/src/main/java/org/springframework/batch/core/listener/MulticasterBatchListener.java index 24b496ee5..c9dde8432 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/listener/MulticasterBatchListener.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/listener/MulticasterBatchListener.java @@ -176,12 +176,12 @@ ItemProcessListener, ItemWriteListener, SkipListener { /** * - * @see org.springframework.batch.core.listener.CompositeChunkListener#afterChunk() + * @see org.springframework.batch.core.listener.CompositeChunkListener#afterChunk(ChunkContext context) */ @Override - public void afterChunk() { + public void afterChunk(ChunkContext context) { try { - chunkListener.afterChunk(); + chunkListener.afterChunk(context); } catch (RuntimeException e) { throw new StepListenerFailedException("Error in afterChunk.", e); @@ -190,12 +190,12 @@ ItemProcessListener, ItemWriteListener, SkipListener { /** * - * @see org.springframework.batch.core.listener.CompositeChunkListener#beforeChunk() + * @see org.springframework.batch.core.listener.CompositeChunkListener#beforeChunk(ChunkContext context) */ @Override - public void beforeChunk() { + public void beforeChunk(ChunkContext context) { try { - chunkListener.beforeChunk(); + chunkListener.beforeChunk(context); } catch (RuntimeException e) { throw new StepListenerFailedException("Error in beforeChunk.", e); diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/listener/StepListenerMetaData.java b/spring-batch-core/src/main/java/org/springframework/batch/core/listener/StepListenerMetaData.java index 21f85eaff..31d46e66e 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/listener/StepListenerMetaData.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/listener/StepListenerMetaData.java @@ -59,8 +59,8 @@ public enum StepListenerMetaData implements ListenerMetaData { BEFORE_STEP("beforeStep", "before-step-method", BeforeStep.class, StepExecutionListener.class, StepExecution.class), AFTER_STEP("afterStep", "after-step-method", AfterStep.class, StepExecutionListener.class, StepExecution.class), - BEFORE_CHUNK("beforeChunk", "before-chunk-method", BeforeChunk.class, ChunkListener.class), - AFTER_CHUNK("afterChunk", "after-chunk-method", AfterChunk.class, ChunkListener.class), + BEFORE_CHUNK("beforeChunk", "before-chunk-method", BeforeChunk.class, ChunkListener.class, ChunkContext.class), + AFTER_CHUNK("afterChunk", "after-chunk-method", AfterChunk.class, ChunkListener.class, ChunkContext.class), AFTER_CHUNK_ERROR("afterChunkError", "after-chunk-error-method", AfterChunkError.class, ChunkListener.class, ChunkContext.class), BEFORE_READ("beforeRead", "before-read-method", BeforeRead.class, ItemReadListener.class), AFTER_READ("afterRead", "after-read-method", AfterRead.class, ItemReadListener.class, Object.class), diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/listener/StepListenerSupport.java b/spring-batch-core/src/main/java/org/springframework/batch/core/listener/StepListenerSupport.java index e401b7fb9..567ce3075 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/listener/StepListenerSupport.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/listener/StepListenerSupport.java @@ -53,17 +53,17 @@ ItemReadListener, ItemProcessListener, ItemWriteListener, SkipListene } /* (non-Javadoc) - * @see org.springframework.batch.core.domain.ChunkListener#afterChunk() + * @see org.springframework.batch.core.domain.ChunkListener#afterChunk(ChunkContext context) */ @Override - public void afterChunk() { + public void afterChunk(ChunkContext context) { } /* (non-Javadoc) - * @see org.springframework.batch.core.domain.ChunkListener#beforeChunk() + * @see org.springframework.batch.core.domain.ChunkListener#beforeChunk(ChunkContext context) */ @Override - public void beforeChunk() { + public void beforeChunk(ChunkContext context) { } /* (non-Javadoc) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/FaultTolerantStepBuilder.java b/spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/FaultTolerantStepBuilder.java index 1d1bad01f..05450eccf 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/FaultTolerantStepBuilder.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/FaultTolerantStepBuilder.java @@ -649,9 +649,9 @@ public class FaultTolerantStepBuilder extends SimpleStepBuilder { } @Override - public void beforeChunk() { + public void beforeChunk(ChunkContext context) { try { - chunkListener.beforeChunk(); + chunkListener.beforeChunk(context); } catch (Throwable t) { throw new FatalStepExecutionException("ChunkListener threw exception, rethrowing as fatal", t); @@ -659,9 +659,9 @@ public class FaultTolerantStepBuilder extends SimpleStepBuilder { } @Override - public void afterChunk() { + public void afterChunk(ChunkContext context) { try { - chunkListener.afterChunk(); + chunkListener.afterChunk(context); } catch (Throwable t) { throw new FatalStepExecutionException("ChunkListener threw exception, rethrowing as fatal", t); diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/TaskletStep.java b/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/TaskletStep.java index 9d9c07b42..c661cdd1a 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/TaskletStep.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/TaskletStep.java @@ -272,7 +272,7 @@ public class TaskletStep extends AbstractStep { throw (Exception) e.getCause(); } - chunkListener.afterChunk(); + chunkListener.afterChunk(chunkContext); // Check for interruption after transaction as well, so that // the interrupted exception is correctly propagated up to @@ -380,7 +380,7 @@ public class TaskletStep extends AbstractStep { StepContribution contribution = stepExecution.createStepContribution(); - chunkListener.beforeChunk(); + chunkListener.beforeChunk(chunkContext); // In case we need to push it back to its old value // after a commit fails... diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/listener/CompositeChunkListenerTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/listener/CompositeChunkListenerTests.java index 19ba25d07..3cda9691f 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/listener/CompositeChunkListenerTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/listener/CompositeChunkListenerTests.java @@ -33,9 +33,11 @@ public class CompositeChunkListenerTests { ChunkListener listener; CompositeChunkListener compositeListener; + ChunkContext chunkContext; @Before public void setUp() throws Exception { + chunkContext = new ChunkContext(null); listener = createMock(ChunkListener.class); compositeListener = new CompositeChunkListener(); compositeListener.register(listener); @@ -43,19 +45,18 @@ public class CompositeChunkListenerTests { @Test public void testBeforeChunk(){ - - listener.beforeChunk(); + listener.beforeChunk(chunkContext); replay(listener); - compositeListener.beforeChunk(); + compositeListener.beforeChunk(chunkContext); verify(listener); } @Test public void testAfterChunk(){ - listener.afterChunk(); + listener.afterChunk(chunkContext); replay(listener); - compositeListener.afterChunk(); + compositeListener.afterChunk(chunkContext); verify(listener); } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/listener/MulticasterBatchListenerTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/listener/MulticasterBatchListenerTests.java index c02d9d108..3e41beccf 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/listener/MulticasterBatchListenerTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/listener/MulticasterBatchListenerTests.java @@ -25,6 +25,7 @@ import org.junit.Before; import org.junit.Test; import org.springframework.batch.core.ExitStatus; import org.springframework.batch.core.StepExecution; +import org.springframework.batch.core.scope.context.ChunkContext; /** * @author Dave Syer @@ -128,25 +129,25 @@ public class MulticasterBatchListenerTests { /** * Test method for - * {@link org.springframework.batch.core.listener.MulticasterBatchListener#afterChunk()} + * {@link org.springframework.batch.core.listener.MulticasterBatchListener#afterChunk(ChunkContext context)} * . */ @Test public void testAfterChunk() { - multicast.afterChunk(); + multicast.afterChunk(null); assertEquals(1, count); } /** * Test method for - * {@link org.springframework.batch.core.listener.MulticasterBatchListener#afterChunk()} + * {@link org.springframework.batch.core.listener.MulticasterBatchListener#afterChunk(ChunkContext context)} * . */ @Test public void testAfterChunkFails() { error = true; try { - multicast.afterChunk(); + multicast.afterChunk(null); fail("Expected StepListenerFailedException"); } catch (StepListenerFailedException e) { @@ -159,25 +160,25 @@ public class MulticasterBatchListenerTests { /** * Test method for - * {@link org.springframework.batch.core.listener.MulticasterBatchListener#beforeChunk()} + * {@link org.springframework.batch.core.listener.MulticasterBatchListener#beforeChunk(ChunkContext context)} * . */ @Test public void testBeforeChunk() { - multicast.beforeChunk(); + multicast.beforeChunk(null); assertEquals(1, count); } /** * Test method for - * {@link org.springframework.batch.core.listener.MulticasterBatchListener#beforeChunk()} + * {@link org.springframework.batch.core.listener.MulticasterBatchListener#beforeChunk(ChunkContext context)} * . */ @Test public void testBeforeChunkFails() { error = true; try { - multicast.beforeChunk(); + multicast.beforeChunk(null); fail("Expected StepListenerFailedException"); } catch (StepListenerFailedException e) { @@ -528,12 +529,12 @@ public class MulticasterBatchListenerTests { * () */ @Override - public void afterChunk() { + public void afterChunk(ChunkContext context) { count++; if (error) { throw new RuntimeException("listener error"); } - super.afterChunk(); + super.afterChunk(context); } /* @@ -576,12 +577,12 @@ public class MulticasterBatchListenerTests { * () */ @Override - public void beforeChunk() { + public void beforeChunk(ChunkContext context) { count++; if (error) { throw new RuntimeException("listener error"); } - super.beforeChunk(); + super.beforeChunk(context); } /* diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/listener/StepListenerFactoryBeanTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/listener/StepListenerFactoryBeanTests.java index 7b118078a..a29654f8c 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/listener/StepListenerFactoryBeanTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/listener/StepListenerFactoryBeanTests.java @@ -95,8 +95,8 @@ public class StepListenerFactoryBeanTests { StepListener listener = (StepListener) factoryBean.getObject(); ((StepExecutionListener) listener).beforeStep(stepExecution); ((StepExecutionListener) listener).afterStep(stepExecution); - ((ChunkListener) listener).beforeChunk(); - ((ChunkListener) listener).afterChunk(); + ((ChunkListener) listener).beforeChunk(null); + ((ChunkListener) listener).afterChunk(null); ((ChunkListener) listener).afterChunkError(new ChunkContext(null)); ((ItemReadListener) listener).beforeRead(); ((ItemReadListener) listener).afterRead(readItem); diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanRollbackTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanRollbackTests.java index ae2e803d7..3c72c745a 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanRollbackTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanRollbackTests.java @@ -598,14 +598,14 @@ public class FaultTolerantStepFactoryBeanRollbackTests { } @Override - public void beforeChunk() { + public void beforeChunk(ChunkContext context) { if(phase == 1){ throw new IllegalArgumentException("Planned exception"); } } @Override - public void afterChunk() { + public void afterChunk(ChunkContext context) { if(phase == 2) { throw new IllegalArgumentException("Planned exception"); } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanTests.java index 8a2484751..3bdef5d4b 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/FaultTolerantStepFactoryBeanTests.java @@ -785,12 +785,12 @@ public class FaultTolerantStepFactoryBeanTests { } @Override - public void afterChunk() { + public void afterChunk(ChunkContext context) { listenerCalls.add(4); } @Override - public void beforeChunk() { + public void beforeChunk(ChunkContext context) { } @Override diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/SimpleStepFactoryBeanTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/SimpleStepFactoryBeanTests.java index 2e6e4fd99..b78d6b48b 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/SimpleStepFactoryBeanTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/step/item/SimpleStepFactoryBeanTests.java @@ -256,13 +256,13 @@ public class SimpleStepFactoryBeanTests { } @Override - public void afterChunk() { + public void afterChunk(ChunkContext context) { writeListener.trail = writeListener.trail + "4"; afterCount++; } @Override - public void beforeChunk() { + public void beforeChunk(ChunkContext context) { writeListener.trail = writeListener.trail + "1"; beforeCount++; } @@ -399,12 +399,12 @@ public class SimpleStepFactoryBeanTests { } @Override - public void afterChunk() { + public void afterChunk(ChunkContext context) { listenerCalls.add("chunk"); } @Override - public void beforeChunk() { + public void beforeChunk(ChunkContext context) { } @Override