BATCH-378: Added ChunkListener, ItemReadListener, and ItemWriterListener. Also added them to the ListenerMulticaster.

This commit is contained in:
lucasward
2008-02-29 00:53:00 +00:00
parent 9167c7de4c
commit a519db7f38
8 changed files with 329 additions and 12 deletions

View File

@@ -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();
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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
*

View File

@@ -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();
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -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 {