From a519db7f383e4be875035abc3150a4721472708a Mon Sep 17 00:00:00 2001 From: lucasward Date: Fri, 29 Feb 2008 00:53:00 +0000 Subject: [PATCH] BATCH-378: Added ChunkListener, ItemReadListener, and ItemWriterListener. Also added them to the ListenerMulticaster. --- ...unkInterceptor.java => ChunkListener.java} | 18 ++--- .../batch/core/domain/ItemReadListener.java | 47 ++++++++++++ .../batch/core/domain/ItemWriteListener.java | 51 +++++++++++++ .../batch/core/domain/StepListener.java | 2 + .../interceptor/CompositeChunkListener.java | 72 ++++++++++++++++++ .../CompositeItemReadListener.java | 74 ++++++++++++++++++ .../CompositeItemWriteListener.java | 75 +++++++++++++++++++ .../interceptor/CompositeStepListener.java | 2 +- 8 files changed, 329 insertions(+), 12 deletions(-) rename spring-batch-core/src/main/java/org/springframework/batch/core/domain/{ChunkInterceptor.java => ChunkListener.java} (75%) create mode 100644 spring-batch-core/src/main/java/org/springframework/batch/core/domain/ItemReadListener.java create mode 100644 spring-batch-core/src/main/java/org/springframework/batch/core/domain/ItemWriteListener.java create mode 100644 spring-batch-core/src/main/java/org/springframework/batch/core/interceptor/CompositeChunkListener.java create mode 100644 spring-batch-core/src/main/java/org/springframework/batch/core/interceptor/CompositeItemReadListener.java create mode 100644 spring-batch-core/src/main/java/org/springframework/batch/core/interceptor/CompositeItemWriteListener.java diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/ChunkInterceptor.java b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/ChunkListener.java similarity index 75% rename from spring-batch-core/src/main/java/org/springframework/batch/core/domain/ChunkInterceptor.java rename to spring-batch-core/src/main/java/org/springframework/batch/core/domain/ChunkListener.java index 534d08829..5c8c25beb 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/ChunkInterceptor.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/ChunkListener.java @@ -16,20 +16,16 @@ package org.springframework.batch.core.domain; /** + * Listener interface for the lifecycle of a chunk. A chunk + * can be through of as a collection of items that will be + * committed together. + * * @author Lucas Ward * */ -public interface ChunkInterceptor { +public interface ChunkListener { - void beforeRead(); + void beforeChunk(); - void afterRead(Object item); - - void onReadError(Exception ex); - - void beforeWrite(Object item); - - void afterWrite(); - - void onWriterError(Exception ex, Object item); + void afterChunk(); } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/ItemReadListener.java b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/ItemReadListener.java new file mode 100644 index 000000000..8d489e955 --- /dev/null +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/ItemReadListener.java @@ -0,0 +1,47 @@ +/* + * 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.core.domain; + +import org.springframework.batch.item.ItemReader; +import org.springframework.batch.item.ItemWriter; + +/** + * Listener interface around the reading of an item. + * + * @author Lucas Ward + * + */ +public interface ItemReadListener { + + /** + * Called before {@link ItemReader#read()} + */ + void beforeRead(); + + /** + * Called after {@link ItemReader#read()} + * + * @param item returned from read() + */ + void afterRead(Object item); + + /** + * Called if an error occurs while trying to write. + * + * @param ex thrown from {@link ItemWriter} + */ + void onReadError(Exception ex); +} diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/ItemWriteListener.java b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/ItemWriteListener.java new file mode 100644 index 000000000..429786db8 --- /dev/null +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/ItemWriteListener.java @@ -0,0 +1,51 @@ +/* + * 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.core.domain; + +import org.springframework.batch.item.ItemWriter; + +/** + * Listener interface around the writing of an item. + * + * @author Lucas Ward + * + */ +public interface ItemWriteListener { + + /** + * Called before {@link ItemWriter#write(Object)} + * + * @param item to be written + */ + void beforeWrite(Object item); + + /** + * Called after {@link ItemWriter#write(Object) If the item is last in a + * chunk, this will be called before any transaction is committed, and + * before {@link ChunkListener#afterChunk()} + */ + void afterWrite(); + + /** + * Called if an error occurs while trying to write. + * + * @param ex + * thrown from {@link ItemWriter} + * @param item + * attempted to be written. + */ + void onWriteError(Exception ex, Object item); +} diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/StepListener.java b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/StepListener.java index d7ae6767e..8411e42a0 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/domain/StepListener.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/domain/StepListener.java @@ -18,6 +18,8 @@ package org.springframework.batch.core.domain; import org.springframework.batch.repeat.ExitStatus; /** + * Listener interface for the lifecycle of a {@link Step}. + * * @author Lucas Ward * @author Dave Syer * diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/interceptor/CompositeChunkListener.java b/spring-batch-core/src/main/java/org/springframework/batch/core/interceptor/CompositeChunkListener.java new file mode 100644 index 000000000..b6db011f4 --- /dev/null +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/interceptor/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.core.interceptor; + +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-core/src/main/java/org/springframework/batch/core/interceptor/CompositeItemReadListener.java b/spring-batch-core/src/main/java/org/springframework/batch/core/interceptor/CompositeItemReadListener.java new file mode 100644 index 000000000..d5f04db23 --- /dev/null +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/interceptor/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.core.interceptor; + +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-core/src/main/java/org/springframework/batch/core/interceptor/CompositeItemWriteListener.java b/spring-batch-core/src/main/java/org/springframework/batch/core/interceptor/CompositeItemWriteListener.java new file mode 100644 index 000000000..a4a4c0650 --- /dev/null +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/interceptor/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.core.interceptor; + +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-core/src/main/java/org/springframework/batch/core/interceptor/CompositeStepListener.java b/spring-batch-core/src/main/java/org/springframework/batch/core/interceptor/CompositeStepListener.java index e3ed24e2e..f93a7a868 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/interceptor/CompositeStepListener.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/interceptor/CompositeStepListener.java @@ -25,7 +25,7 @@ import org.springframework.batch.core.domain.StepListener; import org.springframework.batch.repeat.ExitStatus; /** - * @author Dave Syer + * @author Lucas Ward * */ public class CompositeStepListener implements StepListener {