OPEN - issue BATCH-320: Refactor ItemWriter as primary collaborator (with wrapper for processor)
http://jira.springframework.org/browse/BATCH-320 Half way there - tests OK, but some naming and tidying up to do.
This commit is contained in:
@@ -114,10 +114,11 @@ public class HibernateAwareItemWriter implements ItemWriter, RepeatInterceptor,
|
||||
/**
|
||||
* Use the delegate to actually do the writing, but flush aggressively if
|
||||
* the item was previously part of a failed chunk.
|
||||
* @throws Exception
|
||||
*
|
||||
* @see org.springframework.batch.io.OutputSource#write(java.lang.Object)
|
||||
*/
|
||||
public void write(Object output) {
|
||||
public void write(Object output) throws Exception {
|
||||
getProcessed().add(output);
|
||||
delegate.write(output);
|
||||
flushIfNecessary(output);
|
||||
|
||||
@@ -31,6 +31,7 @@ public interface ItemWriter {
|
||||
*
|
||||
* @param item
|
||||
* the object to write.
|
||||
* @throws Exception if something goes wrong
|
||||
*/
|
||||
public void write(Object item);
|
||||
public void write(Object item) throws Exception;
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.util.Properties;
|
||||
|
||||
import org.springframework.batch.item.ItemProcessor;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.restart.GenericRestartData;
|
||||
import org.springframework.batch.restart.RestartData;
|
||||
import org.springframework.batch.restart.Restartable;
|
||||
@@ -17,18 +18,18 @@ import org.springframework.batch.restart.Restartable;
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class CompositeItemProcessor implements ItemProcessor, Restartable {
|
||||
public class CompositeItemWriter implements ItemWriter, Restartable {
|
||||
|
||||
private static final String SEPARATOR = "#";
|
||||
|
||||
private List itemProcessors;
|
||||
private List delegates;
|
||||
|
||||
/**
|
||||
* Calls injected ItemProcessors in order.
|
||||
*/
|
||||
public void process(Object data) throws Exception {
|
||||
for (Iterator iterator = itemProcessors.listIterator(); iterator.hasNext();) {
|
||||
((ItemProcessor) iterator.next()).process(data);
|
||||
public void write(Object data) throws Exception {
|
||||
for (Iterator iterator = delegates.listIterator(); iterator.hasNext();) {
|
||||
((ItemWriter) iterator.next()).write(data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,24 +64,24 @@ public class CompositeItemProcessor implements ItemProcessor, Restartable {
|
||||
List restartDataList = parseProperties(data.getProperties());
|
||||
|
||||
// iterators would make the loop below less readable
|
||||
for (int i = 0; i < itemProcessors.size(); i++) {
|
||||
if (itemProcessors.get(i) instanceof Restartable) {
|
||||
((Restartable) itemProcessors.get(i)).restoreFrom((RestartData) restartDataList.get(i));
|
||||
for (int i = 0; i < delegates.size(); i++) {
|
||||
if (delegates.get(i) instanceof Restartable) {
|
||||
((Restartable) delegates.get(i)).restoreFrom((RestartData) restartDataList.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void setItemProcessors(List itemProcessors) {
|
||||
this.itemProcessors = itemProcessors;
|
||||
public void setItemWriters(List itemProcessors) {
|
||||
this.delegates = itemProcessors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses compound properties into a list of RestartData.
|
||||
*/
|
||||
private List parseProperties(Properties props) {
|
||||
List restartDataList = new ArrayList(itemProcessors.size());
|
||||
for (int i = 0; i < itemProcessors.size(); i++) {
|
||||
List restartDataList = new ArrayList(delegates.size());
|
||||
for (int i = 0; i < delegates.size(); i++) {
|
||||
restartDataList.add(new GenericRestartData(new Properties()));
|
||||
}
|
||||
|
||||
@@ -104,7 +105,7 @@ public class CompositeItemProcessor implements ItemProcessor, Restartable {
|
||||
private Properties createCompoundProperties(PropertiesExtractor extractor) {
|
||||
Properties stats = new Properties();
|
||||
int index = 0;
|
||||
for (Iterator iterator = itemProcessors.listIterator(); iterator.hasNext();) {
|
||||
for (Iterator iterator = delegates.listIterator(); iterator.hasNext();) {
|
||||
ItemProcessor processor = (ItemProcessor) iterator.next();
|
||||
Properties processorStats = extractor.extractProperties(processor);
|
||||
if (processorStats != null) {
|
||||
@@ -3,7 +3,6 @@ package org.springframework.batch.item.processor;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.springframework.batch.io.Skippable;
|
||||
import org.springframework.batch.item.ItemProcessor;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.restart.GenericRestartData;
|
||||
import org.springframework.batch.restart.RestartData;
|
||||
@@ -18,17 +17,18 @@ import org.springframework.util.Assert;
|
||||
* @author Dave Syer
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class ItemWriterItemProcessor implements ItemProcessor, Restartable, Skippable, InitializingBean {
|
||||
public class DelegatingItemWriter implements ItemWriter, Restartable, Skippable, InitializingBean {
|
||||
|
||||
private ItemWriter writer;
|
||||
|
||||
/**
|
||||
* Calls {@link #doProcess(Object)} and then writes the result to the
|
||||
* {@link ItemWriter}.
|
||||
* delegate {@link ItemWriter}.
|
||||
* @throws Exception
|
||||
*
|
||||
* @see org.springframework.batch.item.ItemProcessor#process(java.lang.Object)
|
||||
*/
|
||||
final public void process(Object item) throws Exception {
|
||||
final public void write(Object item) throws Exception {
|
||||
Object result = doProcess(item);
|
||||
writer.write(result);
|
||||
}
|
||||
@@ -36,6 +36,7 @@ public class ItemWriterItemProcessor implements ItemProcessor, Restartable, Skip
|
||||
/**
|
||||
* By default returns the argument. This method is an extension point meant
|
||||
* to be overridden by subclasses that implement processing logic.
|
||||
* @throws Exception
|
||||
*/
|
||||
protected Object doProcess(Object item) throws Exception {
|
||||
return item;
|
||||
@@ -44,7 +45,7 @@ public class ItemWriterItemProcessor implements ItemProcessor, Restartable, Skip
|
||||
/**
|
||||
* Setter for {@link ItemWriter}.
|
||||
*/
|
||||
public void setItemWriter(ItemWriter writer) {
|
||||
public void setDelegate(ItemWriter writer) {
|
||||
this.writer = writer;
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ import org.springframework.batch.support.AbstractMethodInvokingDelegator;
|
||||
* Delegates item processing to a custom method -
|
||||
* passes the item as an argument for the delegate method.
|
||||
*
|
||||
* @see PropertyExtractingDelegatingItemProcessor
|
||||
* @see PropertyExtractingDelegatingItemWriter
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
|
||||
@@ -9,7 +9,7 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class TransformerWriterItemProcessor extends ItemWriterItemProcessor {
|
||||
public class ItemTransformerItemWriterr extends DelegatingItemWriter {
|
||||
|
||||
private ItemTransformer itemTransformer;
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
package org.springframework.batch.item.processor;
|
||||
|
||||
import org.springframework.batch.item.ItemProcessor;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.support.AbstractMethodInvokingDelegator;
|
||||
import org.springframework.beans.BeanWrapper;
|
||||
import org.springframework.beans.BeanWrapperImpl;
|
||||
@@ -30,7 +30,7 @@ import org.springframework.util.Assert;
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class PropertyExtractingDelegatingItemProcessor extends AbstractMethodInvokingDelegator implements ItemProcessor {
|
||||
public class PropertyExtractingDelegatingItemWriter extends AbstractMethodInvokingDelegator implements ItemWriter {
|
||||
|
||||
private String[] fieldsUsedAsTargetMethodArguments;
|
||||
|
||||
@@ -38,7 +38,7 @@ public class PropertyExtractingDelegatingItemProcessor extends AbstractMethodInv
|
||||
* Extracts values from item's fields named in fieldsUsedAsTargetMethodArguments
|
||||
* and passes them as arguments to the delegate method.
|
||||
*/
|
||||
public void process(Object item) throws Exception {
|
||||
public void write(Object item) throws Exception {
|
||||
// helper for extracting property values from a bean
|
||||
BeanWrapper beanWrapper = new BeanWrapperImpl();
|
||||
beanWrapper.setWrappedInstance(item);
|
||||
@@ -16,15 +16,15 @@
|
||||
|
||||
package org.springframework.batch.repeat.callback;
|
||||
|
||||
import org.springframework.batch.item.ItemProcessor;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.repeat.ExitStatus;
|
||||
import org.springframework.batch.repeat.RepeatCallback;
|
||||
import org.springframework.batch.repeat.RepeatContext;
|
||||
import org.springframework.batch.repeat.ExitStatus;
|
||||
|
||||
/**
|
||||
* Simple wrapper for two business interfaces: get the next item from a
|
||||
* ItemProvider and apply the given processor to the result (if not null).
|
||||
* reader and apply the given writer to the result (if not null).
|
||||
*
|
||||
* @author Dave Syer
|
||||
*
|
||||
@@ -33,12 +33,12 @@ public class ItemReaderRepeatCallback implements RepeatCallback {
|
||||
|
||||
ItemReader provider;
|
||||
|
||||
ItemProcessor processor;
|
||||
ItemWriter writer;
|
||||
|
||||
public ItemReaderRepeatCallback(ItemReader provider, ItemProcessor processor) {
|
||||
public ItemReaderRepeatCallback(ItemReader provider, ItemWriter writer) {
|
||||
super();
|
||||
this.provider = provider;
|
||||
this.processor = processor;
|
||||
this.writer = writer;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,7 +52,7 @@ public class ItemReaderRepeatCallback implements RepeatCallback {
|
||||
}
|
||||
|
||||
/**
|
||||
* Use the processor to process the next item if there is one. Return the
|
||||
* Use the writer to process the next item if there is one. Return the
|
||||
* item processed, or null if nothing was available.
|
||||
* @see org.springframework.batch.repeat.RepeatCallback#doInIteration(org.springframework.batch.item.BatchContextAdapter)
|
||||
* @param context the current context.
|
||||
@@ -63,9 +63,9 @@ public class ItemReaderRepeatCallback implements RepeatCallback {
|
||||
ExitStatus result = ExitStatus.FINISHED;
|
||||
Object item = provider.read();
|
||||
|
||||
if (processor != null) {
|
||||
if (writer != null) {
|
||||
if (item != null) {
|
||||
processor.process(item);
|
||||
writer.write(item);
|
||||
result = ExitStatus.CONTINUABLE;
|
||||
}
|
||||
item = null;
|
||||
|
||||
@@ -18,9 +18,9 @@ package org.springframework.batch.retry.callback;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.batch.item.ItemProcessor;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.ItemRecoverer;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.batch.retry.RetryCallback;
|
||||
import org.springframework.batch.retry.RetryContext;
|
||||
import org.springframework.batch.retry.RetryPolicy;
|
||||
@@ -49,15 +49,15 @@ public class ItemReaderRetryCallback implements RetryCallback {
|
||||
|
||||
private ItemReader provider;
|
||||
|
||||
private ItemProcessor processor;
|
||||
private ItemWriter writer;
|
||||
|
||||
private ItemRecoverer recoverer;
|
||||
|
||||
public ItemReaderRetryCallback(ItemReader provider,
|
||||
ItemProcessor processor) {
|
||||
ItemWriter writer) {
|
||||
super();
|
||||
this.provider = provider;
|
||||
this.processor = processor;
|
||||
this.writer = writer;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,7 +99,7 @@ public class ItemReaderRetryCallback implements RetryCallback {
|
||||
private Object process(RetryContext context) throws Exception {
|
||||
Object item = next(context);
|
||||
if (item != null) {
|
||||
processor.process(item);
|
||||
writer.write(item);
|
||||
}
|
||||
context.removeAttribute(ITEM); // if successful
|
||||
return item;
|
||||
|
||||
Reference in New Issue
Block a user