From 718137e7930206e68461365761f8167ee989aeb0 Mon Sep 17 00:00:00 2001 From: lucasward Date: Fri, 29 Feb 2008 06:41:21 +0000 Subject: [PATCH] BATCH-378:Moved and renamed packages for listeners, added support objects, and tests, and removed the ItemFailureHandler. --- .../batch/execution/job/SimpleJob.java | 2 +- .../listener/CompositeChunkListener.java | 72 ++++++++++++ .../listener/CompositeItemReadListener.java | 74 +++++++++++++ .../listener/CompositeItemWriteListener.java | 75 +++++++++++++ .../listener/CompositeJobListener.java | 74 +++++++++++++ .../listener/CompositeStepListener.java | 90 +++++++++++++++ .../repository/dao/JdbcJobInstanceDao.java | 6 +- .../batch/execution/step/AbstractStep.java | 8 -- .../execution/step/ItemOrientedStep.java | 1 + .../batch/execution/step/TaskletStep.java | 2 +- .../support/DefaultItemFailureHandler.java | 10 +- .../step/support/ListenerMulticaster.java | 15 +-- .../batch/execution/job/SimpleJobTests.java | 2 +- .../execution/launch/SimpleJobTests.java | 14 ++- .../listener/CompositeChunkListenerTests.java | 57 ++++++++++ .../CompositeItemReadListenerTests.java | 66 +++++++++++ .../CompositeItemWriteListenerTests.java | 65 +++++++++++ .../listener/CompositeJobListenerTests.java | 86 +++++++++++++++ .../listener/CompositeStepListenerTests.java | 103 ++++++++++++++++++ .../execution/step/ItemOrientedStepTests.java | 5 +- .../execution/step/TaskletStepTests.java | 2 +- 21 files changed, 795 insertions(+), 34 deletions(-) create mode 100644 spring-batch-execution/src/main/java/org/springframework/batch/execution/listener/CompositeChunkListener.java create mode 100644 spring-batch-execution/src/main/java/org/springframework/batch/execution/listener/CompositeItemReadListener.java create mode 100644 spring-batch-execution/src/main/java/org/springframework/batch/execution/listener/CompositeItemWriteListener.java create mode 100644 spring-batch-execution/src/main/java/org/springframework/batch/execution/listener/CompositeJobListener.java create mode 100644 spring-batch-execution/src/main/java/org/springframework/batch/execution/listener/CompositeStepListener.java create mode 100644 spring-batch-execution/src/test/java/org/springframework/batch/execution/listener/CompositeChunkListenerTests.java create mode 100644 spring-batch-execution/src/test/java/org/springframework/batch/execution/listener/CompositeItemReadListenerTests.java create mode 100644 spring-batch-execution/src/test/java/org/springframework/batch/execution/listener/CompositeItemWriteListenerTests.java create mode 100644 spring-batch-execution/src/test/java/org/springframework/batch/execution/listener/CompositeJobListenerTests.java create mode 100644 spring-batch-execution/src/test/java/org/springframework/batch/execution/listener/CompositeStepListenerTests.java diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/job/SimpleJob.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/job/SimpleJob.java index 5e17539ca..da1289d06 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/job/SimpleJob.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/job/SimpleJob.java @@ -28,9 +28,9 @@ import org.springframework.batch.core.domain.JobInterruptedException; import org.springframework.batch.core.domain.JobListener; import org.springframework.batch.core.domain.Step; import org.springframework.batch.core.domain.StepExecution; -import org.springframework.batch.core.interceptor.CompositeJobListener; import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.core.runtime.ExitStatusExceptionClassifier; +import org.springframework.batch.execution.listener.CompositeJobListener; import org.springframework.batch.execution.scope.SimpleStepContext; import org.springframework.batch.execution.scope.StepContext; import org.springframework.batch.execution.scope.StepSynchronizationManager; diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/listener/CompositeChunkListener.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/listener/CompositeChunkListener.java new file mode 100644 index 000000000..95642dfed --- /dev/null +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/listener/CompositeChunkListener.java @@ -0,0 +1,72 @@ +/* + * Copyright 2006-2007 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.execution.listener; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; + +import org.springframework.batch.core.domain.ChunkListener; + +/** + * @author Lucas Ward + * + */ +public class CompositeChunkListener implements ChunkListener { + + private List listeners = new ArrayList(); + + /** + * Public setter for the listeners. + * + * @param listeners + */ + public void setListeners(ChunkListener[] listeners) { + this.listeners = Arrays.asList(listeners); + } + + /** + * Register additional listener. + * + * @param stepListener + */ + public void register(ChunkListener chunkListener) { + if (!listeners.contains(chunkListener)) { + listeners.add(chunkListener); + } + } + + /* (non-Javadoc) + * @see org.springframework.batch.core.domain.ChunkListener#afterChunk() + */ + public void afterChunk() { + for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { + ChunkListener listener = (ChunkListener) iterator.next(); + listener.afterChunk(); + } + } + + /* (non-Javadoc) + * @see org.springframework.batch.core.domain.ChunkListener#beforeChunk() + */ + public void beforeChunk() { + for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { + ChunkListener listener = (ChunkListener) iterator.next(); + listener.beforeChunk(); + } + } +} diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/listener/CompositeItemReadListener.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/listener/CompositeItemReadListener.java new file mode 100644 index 000000000..215e3d297 --- /dev/null +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/listener/CompositeItemReadListener.java @@ -0,0 +1,74 @@ +/* + * Copyright 2006-2007 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.execution.listener; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; + +import org.springframework.batch.core.domain.ChunkListener; +import org.springframework.batch.core.domain.ItemReadListener; + +/** + * @author Lucas Ward + * + */ +public class CompositeItemReadListener implements ItemReadListener { + + private List listeners = new ArrayList(); + + /** + * Public setter for the listeners. + * + * @param listeners + */ + public void setListeners(ChunkListener[] listeners) { + this.listeners = Arrays.asList(listeners); + } + + /** + * Register additional listener. + * + * @param itemReaderListener + */ + public void register(ItemReadListener itemReaderListener) { + if (!listeners.contains(itemReaderListener)) { + listeners.add(itemReaderListener); + } + } + + public void afterRead(Object item) { + for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { + ItemReadListener listener = (ItemReadListener) iterator.next(); + listener.afterRead(item); + } + } + + public void beforeRead() { + for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { + ItemReadListener listener = (ItemReadListener) iterator.next(); + listener.beforeRead(); + } + } + + public void onReadError(Exception ex) { + for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { + ItemReadListener listener = (ItemReadListener) iterator.next(); + listener.onReadError(ex); + } + } +} diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/listener/CompositeItemWriteListener.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/listener/CompositeItemWriteListener.java new file mode 100644 index 000000000..e1a47af9d --- /dev/null +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/listener/CompositeItemWriteListener.java @@ -0,0 +1,75 @@ +/* + * Copyright 2006-2007 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.execution.listener; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; + +import org.springframework.batch.core.domain.ChunkListener; +import org.springframework.batch.core.domain.ItemReadListener; +import org.springframework.batch.core.domain.ItemWriteListener; + +/** + * @author Lucas Ward + * + */ +public class CompositeItemWriteListener implements ItemWriteListener { + + private List listeners = new ArrayList(); + + /** + * Public setter for the listeners. + * + * @param listeners + */ + public void setListeners(ChunkListener[] listeners) { + this.listeners = Arrays.asList(listeners); + } + + /** + * Register additional listener. + * + * @param itemReaderListener + */ + public void register(ItemWriteListener itemReaderListener) { + if (!listeners.contains(itemReaderListener)) { + listeners.add(itemReaderListener); + } + } + + public void afterWrite() { + for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { + ItemWriteListener listener = (ItemWriteListener) iterator.next(); + listener.afterWrite(); + } + } + + public void beforeWrite(Object item) { + for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { + ItemWriteListener listener = (ItemWriteListener) iterator.next(); + listener.beforeWrite(item); + } + } + + public void onWriteError(Exception ex, Object item) { + for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { + ItemWriteListener listener = (ItemWriteListener) iterator.next(); + listener.onWriteError(ex, item); + } + } +} diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/listener/CompositeJobListener.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/listener/CompositeJobListener.java new file mode 100644 index 000000000..36f081092 --- /dev/null +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/listener/CompositeJobListener.java @@ -0,0 +1,74 @@ +/* + * Copyright 2006-2007 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.execution.listener; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; + +import org.springframework.batch.core.domain.JobExecution; +import org.springframework.batch.core.domain.JobListener; + +/** + * @author Dave Syer + * + */ +public class CompositeJobListener implements JobListener { + + private List listeners = new ArrayList(); + + /** + * Public setter for the listeners. + * + * @param listeners + */ + public void setListeners(JobListener[] listeners) { + this.listeners = Arrays.asList(listeners); + } + + /** + * Register additional listener. + * + * @param stepListener + */ + public void register(JobListener stepListener) { + if (!listeners.contains(stepListener)) { + listeners.add(stepListener); + } + } + + /* (non-Javadoc) + * @see org.springframework.batch.core.domain.StepListener#close() + */ + public void afterJob() { + for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { + JobListener listener = (JobListener) iterator.next(); + listener.afterJob(); + } + } + + /* (non-Javadoc) + * @see org.springframework.batch.core.domain.StepListener#open(org.springframework.batch.core.domain.JobParameters) + */ + public void beforeJob(JobExecution jobExecution) { + for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { + JobListener listener = (JobListener) iterator.next(); + listener.beforeJob(jobExecution); + } + } + +} diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/listener/CompositeStepListener.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/listener/CompositeStepListener.java new file mode 100644 index 000000000..8ec5db2d3 --- /dev/null +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/listener/CompositeStepListener.java @@ -0,0 +1,90 @@ +/* + * Copyright 2006-2007 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.execution.listener; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; + +import org.springframework.batch.core.domain.StepExecution; +import org.springframework.batch.core.domain.StepListener; +import org.springframework.batch.repeat.ExitStatus; + +/** + * @author Lucas Ward + * + */ +public class CompositeStepListener implements StepListener { + + private List listeners = new ArrayList(); + + /** + * Public setter for the listeners. + * + * @param listeners + */ + public void setListeners(StepListener[] listeners) { + this.listeners = Arrays.asList(listeners); + } + + /** + * Register additional listener. + * + * @param stepListener + */ + public void register(StepListener stepListener) { + if (!listeners.contains(stepListener)) { + listeners.add(stepListener); + } + } + + /* (non-Javadoc) + * @see org.springframework.batch.core.domain.StepListener#close() + */ + public ExitStatus afterStep() { + ExitStatus status = null; + for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { + StepListener listener = (StepListener) iterator.next(); + ExitStatus close = listener.afterStep(); + status = status!=null ? status.and(close): close; + } + return status; + } + + /* (non-Javadoc) + * @see org.springframework.batch.core.domain.StepListener#open(org.springframework.batch.core.domain.JobParameters) + */ + public void beforeStep(StepExecution stepExecution) { + for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { + StepListener listener = (StepListener) iterator.next(); + listener.beforeStep(stepExecution); + } + } + + /* (non-Javadoc) + * @see org.springframework.batch.core.domain.StepListener#onError(java.lang.Throwable) + */ + public ExitStatus onErrorInStep(Throwable e) { + ExitStatus status = null; + for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { + StepListener listener = (StepListener) iterator.next(); + ExitStatus close = listener.onErrorInStep(e); + status = status!=null ? status.and(close): close; + } + return status; + } +} diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/JdbcJobInstanceDao.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/JdbcJobInstanceDao.java index 746aa56a6..d3e1c4551 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/JdbcJobInstanceDao.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/repository/dao/JdbcJobInstanceDao.java @@ -73,13 +73,13 @@ public class JdbcJobInstanceDao extends AbstractJdbcBatchMetadataDao implements private String createJobKey(JobParameters jobParameters) { Map props = jobParameters.getParameters(); - StringBuilder stringBuilder = new StringBuilder(); + StringBuffer stringBuffer = new StringBuffer(); for (Iterator it = props.entrySet().iterator(); it.hasNext();) { Entry entry = (Entry) it.next(); - stringBuilder.append(entry.toString() + ";"); + stringBuffer.append(entry.toString() + ";"); } - return stringBuilder.toString(); + return stringBuffer.toString(); } /** diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/AbstractStep.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/AbstractStep.java index 139054aeb..771011ee9 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/AbstractStep.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/AbstractStep.java @@ -15,13 +15,11 @@ */ package org.springframework.batch.execution.step; -import org.springframework.batch.core.domain.ItemFailureHandler; import org.springframework.batch.core.domain.ItemSkipPolicy; import org.springframework.batch.core.domain.JobInterruptedException; import org.springframework.batch.core.domain.Step; import org.springframework.batch.core.domain.StepExecution; import org.springframework.batch.core.repository.JobRepository; -import org.springframework.batch.execution.step.support.DefaultItemFailureHandler; import org.springframework.batch.execution.step.support.NeverSkipItemSkipPolicy; import org.springframework.batch.io.exception.BatchCriticalException; import org.springframework.batch.item.ItemReader; @@ -55,8 +53,6 @@ public abstract class AbstractStep implements Step, InitializingBean, BeanNameAw protected ItemSkipPolicy itemSkipPolicy = new NeverSkipItemSkipPolicy(); - protected ItemFailureHandler itemFailureHandler = new DefaultItemFailureHandler(); - protected String name; protected int startLimit = Integer.MAX_VALUE; @@ -182,10 +178,6 @@ public abstract class AbstractStep implements Step, InitializingBean, BeanNameAw this.itemSkipPolicy = itemSkipPolicy; } - public void setItemFailureHandler(ItemFailureHandler itemFailureHandler) { - this.itemFailureHandler = itemFailureHandler; - } - /** * Assert that all mandatory properties are set (the {@link JobRepository}). * diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/ItemOrientedStep.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/ItemOrientedStep.java index 0e7438688..23f9cf279 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/ItemOrientedStep.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/ItemOrientedStep.java @@ -19,6 +19,7 @@ import java.util.Date; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.batch.core.domain.BatchListener; import org.springframework.batch.core.domain.BatchStatus; import org.springframework.batch.core.domain.JobInstance; import org.springframework.batch.core.domain.JobInterruptedException; diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/TaskletStep.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/TaskletStep.java index b7fbf1952..58aeb5fff 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/TaskletStep.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/TaskletStep.java @@ -24,9 +24,9 @@ import org.springframework.batch.core.domain.JobInterruptedException; import org.springframework.batch.core.domain.Step; import org.springframework.batch.core.domain.StepExecution; import org.springframework.batch.core.domain.StepListener; -import org.springframework.batch.core.interceptor.CompositeStepListener; import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.core.tasklet.Tasklet; +import org.springframework.batch.execution.listener.CompositeStepListener; import org.springframework.batch.io.exception.BatchCriticalException; import org.springframework.batch.repeat.ExitStatus; import org.springframework.beans.factory.BeanNameAware; diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/support/DefaultItemFailureHandler.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/support/DefaultItemFailureHandler.java index 34780c3ae..61f095748 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/support/DefaultItemFailureHandler.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/support/DefaultItemFailureHandler.java @@ -17,10 +17,10 @@ package org.springframework.batch.execution.step.support; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.springframework.batch.core.domain.ItemFailureHandler; +import org.springframework.batch.core.listener.ItemListenerSupport; /** - * Default implementation of the {@link ItemFailureHandler} interface that + * Default implementation of the {@link ItemListenerSupport} class that * writes all exceptions via commons logging. Since generics can't be used to * ensure the list contains exceptions, any non exceptions will be logged out by * calling toString on the object. @@ -28,7 +28,7 @@ import org.springframework.batch.core.domain.ItemFailureHandler; * @author Lucas Ward * */ -public class DefaultItemFailureHandler implements ItemFailureHandler { +public class DefaultItemFailureHandler extends ItemListenerSupport { protected static final Log logger = LogFactory .getLog(DefaultItemFailureHandler.class); @@ -38,7 +38,7 @@ public class DefaultItemFailureHandler implements ItemFailureHandler { * * @see org.springframework.batch.core.domain.ItemFailureLog#log(java.util.List) */ - public void handleReadFailure(Exception ex) { + public void onReadError(Exception ex) { try { logger.error("Error encountered while reading", ex); } catch (Exception exception) { @@ -47,7 +47,7 @@ public class DefaultItemFailureHandler implements ItemFailureHandler { } } - public void handleWriteFailure(Object item, Exception ex) { + public void onWriteError(Exception ex, Object item) { try { logger.error("Error encountered while writing item: [ " + item + "]", ex); } catch (Exception exception) { diff --git a/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/support/ListenerMulticaster.java b/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/support/ListenerMulticaster.java index 4f2dc7d67..a5683dfe2 100644 --- a/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/support/ListenerMulticaster.java +++ b/spring-batch-execution/src/main/java/org/springframework/batch/execution/step/support/ListenerMulticaster.java @@ -15,15 +15,16 @@ */ package org.springframework.batch.execution.step.support; +import org.springframework.batch.core.domain.BatchListener; import org.springframework.batch.core.domain.ChunkListener; import org.springframework.batch.core.domain.ItemReadListener; import org.springframework.batch.core.domain.ItemWriteListener; import org.springframework.batch.core.domain.StepExecution; import org.springframework.batch.core.domain.StepListener; -import org.springframework.batch.core.interceptor.CompositeChunkListener; -import org.springframework.batch.core.interceptor.CompositeItemReadListener; -import org.springframework.batch.core.interceptor.CompositeItemWriteListener; -import org.springframework.batch.core.interceptor.CompositeStepListener; +import org.springframework.batch.execution.listener.CompositeChunkListener; +import org.springframework.batch.execution.listener.CompositeItemReadListener; +import org.springframework.batch.execution.listener.CompositeItemWriteListener; +import org.springframework.batch.execution.listener.CompositeStepListener; import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.item.ItemStream; import org.springframework.batch.item.exception.StreamException; @@ -83,7 +84,7 @@ public class ListenerMulticaster implements ItemStream, StepListener, ChunkListe /** * @return - * @see org.springframework.batch.core.interceptor.CompositeStepListener#afterStep() + * @see org.springframework.batch.execution.listener.CompositeStepListener#afterStep() */ public ExitStatus afterStep() { return stepListener.afterStep(); @@ -91,7 +92,7 @@ public class ListenerMulticaster implements ItemStream, StepListener, ChunkListe /** * @param stepExecution - * @see org.springframework.batch.core.interceptor.CompositeStepListener#beforeStep(org.springframework.batch.core.domain.StepExecution) + * @see org.springframework.batch.execution.listener.CompositeStepListener#beforeStep(org.springframework.batch.core.domain.StepExecution) */ public void beforeStep(StepExecution stepExecution) { stepListener.beforeStep(stepExecution); @@ -100,7 +101,7 @@ public class ListenerMulticaster implements ItemStream, StepListener, ChunkListe /** * @param e * @return - * @see org.springframework.batch.core.interceptor.CompositeStepListener#onErrorInStep(java.lang.Throwable) + * @see org.springframework.batch.execution.listener.CompositeStepListener#onErrorInStep(java.lang.Throwable) */ public ExitStatus onErrorInStep(Throwable e) { return stepListener.onErrorInStep(e); diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/job/SimpleJobTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/job/SimpleJobTests.java index 9f82b3b49..6024e1023 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/job/SimpleJobTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/job/SimpleJobTests.java @@ -28,7 +28,7 @@ import org.springframework.batch.core.domain.JobInterruptedException; import org.springframework.batch.core.domain.JobParameters; import org.springframework.batch.core.domain.Step; import org.springframework.batch.core.domain.StepExecution; -import org.springframework.batch.core.interceptor.JobListenerSupport; +import org.springframework.batch.core.listener.JobListenerSupport; import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.core.runtime.ExitStatusExceptionClassifier; import org.springframework.batch.execution.repository.SimpleJobRepository; diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/launch/SimpleJobTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/launch/SimpleJobTests.java index 50d7cbb60..aa1b3e2ec 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/launch/SimpleJobTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/launch/SimpleJobTests.java @@ -23,11 +23,12 @@ import java.util.List; import junit.framework.TestCase; +import org.springframework.batch.core.domain.BatchListener; import org.springframework.batch.core.domain.BatchStatus; -import org.springframework.batch.core.domain.ItemFailureHandler; import org.springframework.batch.core.domain.JobExecution; import org.springframework.batch.core.domain.JobInstance; import org.springframework.batch.core.domain.JobParameters; +import org.springframework.batch.core.listener.ItemListenerSupport; import org.springframework.batch.execution.job.SimpleJob; import org.springframework.batch.execution.repository.SimpleJobRepository; import org.springframework.batch.execution.repository.dao.MapJobExecutionDao; @@ -149,17 +150,19 @@ public class SimpleJobTests extends TestCase { throw new RuntimeException("Error!"); } }); - step.setItemFailureHandler(new ItemFailureHandler(){ + + step.setListeners(new BatchListener[]{new ItemListenerSupport(){ - public void handleReadFailure(Exception ex) { + public void onReadError(Exception ex) { recovered.add(ex); } - public void handleWriteFailure(Object item, Exception ex) { + public void onWriteError(Exception ex, Object item) { recovered.add(ex); } - }); + }}); + step.afterPropertiesSet(); job.setSteps(Collections.singletonList(step)); @@ -195,4 +198,5 @@ public class SimpleJobTests extends TestCase { } assertEquals(BatchStatus.FAILED, jobExecution.getStatus()); } + } diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/listener/CompositeChunkListenerTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/listener/CompositeChunkListenerTests.java new file mode 100644 index 000000000..affc6c6d6 --- /dev/null +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/listener/CompositeChunkListenerTests.java @@ -0,0 +1,57 @@ +/* + * Copyright 2006-2008 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.execution.listener; + +import org.easymock.MockControl; +import org.springframework.batch.core.domain.ChunkListener; + +import junit.framework.TestCase; + +/** + * @author Lucas Ward + * + */ +public class CompositeChunkListenerTests extends TestCase { + + MockControl listenerControl = MockControl.createControl(ChunkListener.class); + + ChunkListener listener; + CompositeChunkListener compositeListener; + + protected void setUp() throws Exception { + super.setUp(); + + listener = (ChunkListener)listenerControl.getMock(); + compositeListener = new CompositeChunkListener(); + compositeListener.register(listener); + } + + public void testBeforeChunk(){ + + listener.beforeChunk(); + listenerControl.replay(); + compositeListener.beforeChunk(); + listenerControl.verify(); + } + + public void testAfterChunk(){ + + listener.afterChunk(); + listenerControl.replay(); + compositeListener.afterChunk(); + listenerControl.verify(); + } +} diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/listener/CompositeItemReadListenerTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/listener/CompositeItemReadListenerTests.java new file mode 100644 index 000000000..4928eace0 --- /dev/null +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/listener/CompositeItemReadListenerTests.java @@ -0,0 +1,66 @@ +/* + * Copyright 2006-2008 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.execution.listener; + +import junit.framework.TestCase; + +import org.easymock.MockControl; +import org.springframework.batch.core.domain.ItemReadListener; + +/** + * @author Lucas Ward + * + */ +public class CompositeItemReadListenerTests extends TestCase { + + MockControl listenerControl = MockControl.createControl(ItemReadListener.class); + + ItemReadListener listener; + CompositeItemReadListener compositeListener; + + protected void setUp() throws Exception { + super.setUp(); + + listener = (ItemReadListener)listenerControl.getMock(); + compositeListener = new CompositeItemReadListener(); + compositeListener.register(listener); + } + + public void testBeforeRead(){ + + listener.beforeRead(); + listenerControl.replay(); + compositeListener.beforeRead(); + listenerControl.verify(); + } + + public void testAfterRead(){ + Object item = new Object(); + listener.afterRead(item); + listenerControl.replay(); + compositeListener.afterRead(item); + listenerControl.verify(); + } + + public void testOnReadError(){ + + Exception ex = new Exception(); + listener.onReadError(ex); + listenerControl.replay(); + compositeListener.onReadError(ex); + listenerControl.verify(); + } +} diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/listener/CompositeItemWriteListenerTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/listener/CompositeItemWriteListenerTests.java new file mode 100644 index 000000000..67b56073c --- /dev/null +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/listener/CompositeItemWriteListenerTests.java @@ -0,0 +1,65 @@ +/* + * Copyright 2006-2008 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.execution.listener; + +import junit.framework.TestCase; + +import org.easymock.MockControl; +import org.springframework.batch.core.domain.ItemWriteListener; + +/** + * @author Lucas Ward + * + */ +public class CompositeItemWriteListenerTests extends TestCase { + + MockControl listenerControl = MockControl.createControl(ItemWriteListener.class); + + ItemWriteListener listener; + CompositeItemWriteListener compositeListener; + + protected void setUp() throws Exception { + super.setUp(); + + listener = (ItemWriteListener)listenerControl.getMock(); + compositeListener = new CompositeItemWriteListener(); + compositeListener.register(listener); + } + + public void testBeforeWrite(){ + Object item = new Object(); + listener.beforeWrite(item); + listenerControl.replay(); + compositeListener.beforeWrite(item); + listenerControl.verify(); + } + + public void testAfterWrite(){ + listener.afterWrite(); + listenerControl.replay(); + compositeListener.afterWrite(); + listenerControl.verify(); + } + + public void testOnWriteError(){ + Object item = new Object(); + Exception ex = new Exception(); + listener.onWriteError(ex, item); + listenerControl.replay(); + compositeListener.onWriteError(ex, item); + listenerControl.verify(); + } +} diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/listener/CompositeJobListenerTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/listener/CompositeJobListenerTests.java new file mode 100644 index 000000000..cf027385d --- /dev/null +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/listener/CompositeJobListenerTests.java @@ -0,0 +1,86 @@ +/* + * Copyright 2006-2007 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.execution.listener; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.TestCase; + +import org.springframework.batch.core.domain.JobExecution; +import org.springframework.batch.core.domain.JobInstance; +import org.springframework.batch.core.domain.JobListener; +import org.springframework.batch.core.listener.JobListenerSupport; +import org.springframework.batch.execution.job.JobSupport; +import org.springframework.batch.execution.listener.CompositeJobListener; + +/** + * @author Dave Syer + * + */ +public class CompositeJobListenerTests extends TestCase { + + private CompositeJobListener listener = new CompositeJobListener(); + + private List list = new ArrayList(); + + /** + * Test method for + * {@link org.springframework.batch.execution.listener.CompositeJobListener#setListeners(org.springframework.batch.core.domain.JobListener[])}. + */ + public void testSetListeners() { + listener.setListeners(new JobListener[] { new JobListenerSupport() { + public void afterJob() { + list.add("fail"); + } + }, new JobListenerSupport() { + public void afterJob() { + list.add("continue"); + } + } }); + listener.afterJob(); + assertEquals(2, list.size()); + } + + /** + * Test method for + * {@link org.springframework.batch.execution.listener.CompositeJobListener#setListener(org.springframework.batch.core.domain.JobListener)}. + */ + public void testSetListener() { + listener.register(new JobListenerSupport() { + public void afterJob() { + list.add("fail"); + } + }); + listener.afterJob(); + assertEquals(1, list.size()); + } + + /** + * Test method for + * {@link org.springframework.batch.execution.listener.CompositeJobListener#beforeJob(JobExecution)}. + */ + public void testOpen() { + listener.register(new JobListenerSupport() { + public void beforeJob(JobExecution stepExecution) { + list.add("foo"); + } + }); + listener.beforeJob(new JobExecution(new JobInstance(new Long(11L), null, new JobSupport()))); + assertEquals(1, list.size()); + } + +} diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/listener/CompositeStepListenerTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/listener/CompositeStepListenerTests.java new file mode 100644 index 000000000..58b73a750 --- /dev/null +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/listener/CompositeStepListenerTests.java @@ -0,0 +1,103 @@ +/* + * Copyright 2006-2007 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.execution.listener; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.TestCase; + +import org.springframework.batch.core.domain.StepExecution; +import org.springframework.batch.core.domain.StepListener; +import org.springframework.batch.core.listener.StepListenerSupport; +import org.springframework.batch.execution.step.StepSupport; +import org.springframework.batch.repeat.ExitStatus; + +/** + * @author Dave Syer + * + */ +public class CompositeStepListenerTests extends TestCase { + + private CompositeStepListener listener = new CompositeStepListener(); + + private List list = new ArrayList(); + + /** + * Test method for + * {@link org.springframework.batch.execution.listener.CompositeStepListener#setListeners(org.springframework.batch.core.domain.StepListener[])}. + */ + public void testSetListeners() { + listener.setListeners(new StepListener[] { new StepListenerSupport() { + public ExitStatus afterStep() { + list.add("fail"); + return ExitStatus.FAILED; + } + }, new StepListenerSupport() { + public ExitStatus afterStep() { + list.add("continue"); + return ExitStatus.CONTINUABLE; + } + } }); + assertFalse(listener.afterStep().isContinuable()); + assertEquals(2, list.size()); + } + + /** + * Test method for + * {@link org.springframework.batch.execution.listener.CompositeStepListener#setListener(org.springframework.batch.core.domain.StepListener)}. + */ + public void testSetListener() { + listener.register(new StepListenerSupport() { + public ExitStatus afterStep() { + list.add("fail"); + return ExitStatus.FAILED; + } + }); + assertFalse(listener.afterStep().isContinuable()); + assertEquals(1, list.size()); + } + + /** + * Test method for + * {@link org.springframework.batch.execution.listener.CompositeStepListener#beforeStep(StepExecution)}. + */ + public void testOpen() { + listener.register(new StepListenerSupport() { + public void beforeStep(StepExecution stepExecution) { + list.add("foo"); + } + }); + listener.beforeStep(new StepExecution(new StepSupport("foo"), null)); + assertEquals(1, list.size()); + } + + /** + * Test method for + * {@link org.springframework.batch.execution.listener.CompositeStepListener#beforeStep(StepExecution)}. + */ + public void testOnError() { + listener.register(new StepListenerSupport() { + public ExitStatus onErrorInStep(Throwable e) { + list.add("foo"); + return null; + } + }); + listener.onErrorInStep(new RuntimeException()); + assertEquals(1, list.size()); + } + +} diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/ItemOrientedStepTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/ItemOrientedStepTests.java index ec4d72df6..31fb82a5a 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/ItemOrientedStepTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/ItemOrientedStepTests.java @@ -22,6 +22,7 @@ import java.util.List; import junit.framework.TestCase; +import org.springframework.batch.core.domain.BatchListener; import org.springframework.batch.core.domain.BatchStatus; import org.springframework.batch.core.domain.JobExecution; import org.springframework.batch.core.domain.JobInstance; @@ -29,7 +30,7 @@ import org.springframework.batch.core.domain.JobInterruptedException; import org.springframework.batch.core.domain.JobParameters; import org.springframework.batch.core.domain.StepContribution; import org.springframework.batch.core.domain.StepExecution; -import org.springframework.batch.core.interceptor.StepListenerSupport; +import org.springframework.batch.core.listener.StepListenerSupport; import org.springframework.batch.execution.job.JobSupport; import org.springframework.batch.execution.repository.SimpleJobRepository; import org.springframework.batch.execution.repository.dao.MapJobExecutionDao; @@ -371,7 +372,7 @@ public class ItemOrientedStepTests extends TestCase { } public void testDirectlyInjectedListener() throws Exception { - itemOrientedStep.setListeners(new Object[] {new StepListenerSupport() { + itemOrientedStep.setListeners(new BatchListener[] {new StepListenerSupport() { public void beforeStep(StepExecution stepExecution) { list.add("foo"); } diff --git a/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/TaskletStepTests.java b/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/TaskletStepTests.java index 88b98d39c..5163ccee7 100644 --- a/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/TaskletStepTests.java +++ b/spring-batch-execution/src/test/java/org/springframework/batch/execution/step/TaskletStepTests.java @@ -11,7 +11,7 @@ import org.springframework.batch.core.domain.JobInstance; import org.springframework.batch.core.domain.JobInterruptedException; import org.springframework.batch.core.domain.JobParameters; import org.springframework.batch.core.domain.StepExecution; -import org.springframework.batch.core.interceptor.StepListenerSupport; +import org.springframework.batch.core.listener.StepListenerSupport; import org.springframework.batch.core.tasklet.Tasklet; import org.springframework.batch.execution.job.JobSupport; import org.springframework.batch.execution.scope.StepSynchronizationManager;