IN PROGRESS - issue BATCH-7: Remove transaction synchronization and state management from input/output sources (formerly buffering)

http://jira.springframework.org/browse/BATCH-7

Added ItemStream as base interface for ItemReader/Writer.
This commit is contained in:
dsyer
2008-01-30 18:42:48 +00:00
parent 11708012b5
commit bebab9c8d2
42 changed files with 340 additions and 235 deletions

View File

@@ -47,15 +47,15 @@ public class RestartableItemOrientedTasklet extends ItemOrientedTasklet implemen
StreamContext itemProviderRestartData = null;
StreamContext itemProcessorRestartData = null;
if (itemProvider instanceof ItemStream) {
itemProviderRestartData = ((ItemStream) itemProvider).getRestartData();
if (itemProvider != null) {
itemProviderRestartData = itemProvider.getRestartData();
}
if (itemWriter != null) {
itemProcessorRestartData = itemWriter.getRestartData();
}
if (itemWriter instanceof ItemStream) {
itemProcessorRestartData = ((ItemStream) itemWriter).getRestartData();
}
RestartableItemOrientedTaskletRestartData restartData = new RestartableItemOrientedTaskletRestartData(itemProviderRestartData, itemProcessorRestartData);
RestartableItemOrientedTaskletRestartData restartData = new RestartableItemOrientedTaskletRestartData(
itemProviderRestartData, itemProcessorRestartData);
return restartData;
}
@@ -76,11 +76,11 @@ public class RestartableItemOrientedTasklet extends ItemOrientedTasklet implemen
moduleRestartData = new RestartableItemOrientedTaskletRestartData(data.getProperties());
}
if (itemProvider instanceof ItemStream) {
((ItemStream) itemProvider).restoreFrom(moduleRestartData.readerData);
if (itemProvider != null) {
itemProvider.restoreFrom(moduleRestartData.readerData);
}
if (itemWriter instanceof ItemStream) {
((ItemStream) itemWriter).restoreFrom(moduleRestartData.writerData);
if (itemWriter != null) {
itemWriter.restoreFrom(moduleRestartData.writerData);
}
}
@@ -100,10 +100,8 @@ public class RestartableItemOrientedTasklet extends ItemOrientedTasklet implemen
}
public RestartableItemOrientedTaskletRestartData(Properties data) {
readerData = new GenericStreamContext(PropertiesConverter
.stringToProperties(data.getProperty(READER_KEY)));
writerData = new GenericStreamContext(PropertiesConverter.stringToProperties(data
.getProperty(WRITER_KEY)));
readerData = new GenericStreamContext(PropertiesConverter.stringToProperties(data.getProperty(READER_KEY)));
writerData = new GenericStreamContext(PropertiesConverter.stringToProperties(data.getProperty(WRITER_KEY)));
}
public Properties getProperties() {
@@ -117,4 +115,20 @@ public class RestartableItemOrientedTasklet extends ItemOrientedTasklet implemen
return props;
}
}
/*
* (non-Javadoc)
* @see org.springframework.batch.item.ItemStream#close()
*/
public void close() throws Exception {
// no-op
}
/*
* (non-Javadoc)
* @see org.springframework.batch.item.ItemStream#open()
*/
public void open() throws Exception {
// no-op
}
}

View File

@@ -21,11 +21,11 @@ import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.writer.AbstractItemWriter;
import org.springframework.batch.support.transaction.TransactionAwareProxyFactory;
import org.springframework.beans.factory.InitializingBean;
public class EmptyItemWriter implements ItemWriter, InitializingBean {
public class EmptyItemWriter extends AbstractItemWriter implements InitializingBean {
private boolean failed = false;

View File

@@ -25,10 +25,10 @@ import java.util.Properties;
import junit.framework.TestCase;
import org.springframework.batch.core.domain.JobSupport;
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.domain.JobSupport;
import org.springframework.batch.core.domain.StepContribution;
import org.springframework.batch.core.domain.StepExecution;
import org.springframework.batch.core.domain.StepInstance;
@@ -40,10 +40,10 @@ import org.springframework.batch.execution.scope.StepScope;
import org.springframework.batch.execution.scope.StepSynchronizationManager;
import org.springframework.batch.execution.tasklet.ItemOrientedTasklet;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.StreamContext;
import org.springframework.batch.item.reader.ListItemReader;
import org.springframework.batch.item.stream.ItemStreamAdapter;
import org.springframework.batch.item.writer.AbstractItemWriter;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.batch.repeat.RepeatContext;
@@ -455,7 +455,7 @@ public class SimpleStepExecutorTests extends TestCase {
assertEquals(0, map.size());
}
private class MockRestartableTasklet implements Tasklet, ItemStream {
private class MockRestartableTasklet extends ItemStreamAdapter implements Tasklet {
private boolean getRestartDataCalled = false;

View File

@@ -273,7 +273,7 @@ public class ItemOrientedTaskletTests extends TestCase {
}
}
private class SkippableItemReader implements KeyedItemReader,
private class SkippableItemReader extends AbstractItemReader implements KeyedItemReader,
Skippable, StatisticsProvider {
public Object read() throws Exception {
return itemProvider.read();
@@ -287,11 +287,9 @@ public class ItemOrientedTaskletTests extends TestCase {
public Properties getStatistics() {
return PropertiesConverter.stringToProperties("foo=bar");
}
public void close() throws Exception {
}
}
private class SkippableItemWriter implements ItemWriter, Skippable,
private class SkippableItemWriter extends AbstractItemWriter implements ItemWriter, Skippable,
StatisticsProvider {
String props = "foo=bar";

View File

@@ -21,10 +21,11 @@ import java.util.Properties;
import junit.framework.TestCase;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.StreamContext;
import org.springframework.batch.item.reader.AbstractItemReader;
import org.springframework.batch.item.stream.GenericStreamContext;
import org.springframework.batch.item.writer.AbstractItemWriter;
import org.springframework.batch.support.PropertiesConverter;
/**
@@ -32,16 +33,16 @@ import org.springframework.batch.support.PropertiesConverter;
*/
public class RestartableItemOrientedTaskletTests extends TestCase {
private static class MockProvider implements ItemReader, ItemStream {
private static class MockProvider extends AbstractItemReader {
StreamContext data = new StreamContext() {
public Properties getProperties() {
return PropertiesConverter.stringToProperties("a=b");
}
};
public Object read() {
return null;
}
@@ -55,16 +56,9 @@ public class RestartableItemOrientedTaskletTests extends TestCase {
assertEquals(this.data.getProperties(), data.getProperties());
}
public boolean recover(Object data, Throwable cause) {
return false;
}
public void close() throws Exception {
}
}
private static class MockWriter implements ItemWriter, ItemStream {
private static class MockWriter extends AbstractItemWriter {
StreamContext data = new StreamContext() {
public Properties getProperties() {
@@ -131,7 +125,7 @@ public class RestartableItemOrientedTaskletTests extends TestCase {
// restore from restart data (see asserts in mock classes)
module.restoreFrom(data);
}
public void testRestartFromNotRestartable() {
// create and set up module
@@ -144,7 +138,7 @@ public class RestartableItemOrientedTaskletTests extends TestCase {
assertNotNull(data);
// restore from restart data (see asserts in mock classes)
module.restoreFrom(data);
//System.err.println(data.getProperties());
// System.err.println(data.getProperties());
}
}

View File

@@ -26,7 +26,6 @@ import org.hibernate.StatelessSession;
import org.springframework.batch.io.Skippable;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ResourceLifecycle;
import org.springframework.batch.item.StreamContext;
import org.springframework.batch.item.reader.AbstractItemReader;
import org.springframework.batch.item.stream.GenericStreamContext;
@@ -58,7 +57,7 @@ import org.springframework.util.StringUtils;
* @author Dave Syer
*/
public class HibernateCursorItemReader extends AbstractItemReader implements ItemReader, ItemStream,
Skippable, InitializingBean, DisposableBean, ResourceLifecycle {
Skippable, InitializingBean, DisposableBean {
private static final String RESTART_DATA_ROW_NUMBER_KEY = ClassUtils
.getShortName(HibernateCursorItemReader.class)

View File

@@ -33,7 +33,6 @@ import org.springframework.batch.io.Skippable;
import org.springframework.batch.io.support.AbstractTransactionalIoSource;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.KeyedItemReader;
import org.springframework.batch.item.ResourceLifecycle;
import org.springframework.batch.item.StreamContext;
import org.springframework.batch.item.stream.GenericStreamContext;
import org.springframework.batch.statistics.StatisticsProvider;
@@ -117,7 +116,7 @@ import org.springframework.util.StringUtils;
* @author Peter Zozom
*/
public class JdbcCursorItemReader extends AbstractTransactionalIoSource
implements KeyedItemReader, ResourceLifecycle, DisposableBean,
implements KeyedItemReader, DisposableBean,
InitializingBean, ItemStream, StatisticsProvider, Skippable {
private static Log log = LogFactory.getLog(JdbcCursorItemReader.class);

View File

@@ -21,7 +21,6 @@ import java.util.List;
import org.springframework.batch.io.support.AbstractTransactionalIoSource;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.KeyedItemReader;
import org.springframework.batch.item.ResourceLifecycle;
import org.springframework.batch.item.StreamContext;
import org.springframework.batch.repeat.synch.BatchTransactionSynchronizationManager;
import org.springframework.beans.factory.DisposableBean;
@@ -51,7 +50,7 @@ import org.springframework.util.Assert;
* @since 1.0
*/
public class DrivingQueryItemReader extends AbstractTransactionalIoSource
implements KeyedItemReader, ResourceLifecycle, InitializingBean,
implements KeyedItemReader, InitializingBean,
DisposableBean, ItemStream {
private boolean initialized = false;

View File

@@ -33,7 +33,6 @@ import org.springframework.batch.io.exception.BatchEnvironmentException;
import org.springframework.batch.io.support.AbstractTransactionalIoSource;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.ResourceLifecycle;
import org.springframework.batch.item.StreamContext;
import org.springframework.batch.item.stream.GenericStreamContext;
import org.springframework.batch.item.writer.ItemTransformer;
@@ -61,7 +60,7 @@ import org.springframework.util.Assert;
* @author Dave Syer
*/
public class FlatFileItemWriter extends AbstractTransactionalIoSource implements
ItemWriter, ResourceLifecycle, ItemStream, StatisticsProvider, InitializingBean,
ItemWriter, ItemStream, StatisticsProvider, InitializingBean,
DisposableBean {
private static final String LINE_SEPARATOR = System.getProperty("line.separator");

View File

@@ -31,7 +31,6 @@ import org.springframework.batch.io.file.transform.AbstractLineTokenizer;
import org.springframework.batch.io.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.io.file.transform.LineTokenizer;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ResourceLifecycle;
import org.springframework.batch.item.reader.AbstractItemReader;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
@@ -171,11 +170,10 @@ public class SimpleFlatFileItemReader extends AbstractItemReader implements Item
}
/**
* Close and null out the reader.
*
* @see ResourceLifecycle
* Close and null out the delegate line reader.
* @throws Exception
*/
public void close() {
public void close() throws Exception {
try {
if (reader != null) {
log.debug("Closing flat file for reading: "+resource);

View File

@@ -16,13 +16,12 @@
package org.springframework.batch.io.file.separator;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ResourceLifecycle;
/**
* @author Dave Syer
*
*/
public interface LineReader extends ResourceLifecycle, ItemReader {
public interface LineReader extends ItemReader {
/**
* @return

View File

@@ -27,7 +27,6 @@ import java.util.Iterator;
import org.springframework.batch.io.exception.BatchEnvironmentException;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ResourceLifecycle;
import org.springframework.batch.item.reader.AbstractItemReader;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.core.io.Resource;
@@ -56,7 +55,7 @@ import org.springframework.util.Assert;
* @author Dave Syer
* @author Rob Harrop
*/
public class ResourceLineReader extends AbstractItemReader implements LineReader, ResourceLifecycle, ItemReader,
public class ResourceLineReader extends AbstractItemReader implements LineReader, ItemReader,
DisposableBean {
private static final Collection DEFAULT_COMMENTS = Collections.singleton("#");

View File

@@ -20,6 +20,7 @@ import java.util.Set;
import org.hibernate.SessionFactory;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.stream.ItemStreamAdapter;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.batch.repeat.RepeatContext;
import org.springframework.batch.repeat.RepeatInterceptor;
@@ -44,7 +45,7 @@ import org.springframework.util.Assert;
* @author Dave Syer
*
*/
public class HibernateAwareItemWriter implements ItemWriter, RepeatInterceptor,
public class HibernateAwareItemWriter extends ItemStreamAdapter implements ItemWriter, RepeatInterceptor,
InitializingBean {
/**

View File

@@ -19,7 +19,6 @@ import org.springframework.batch.io.xml.stax.FragmentEventReader;
import org.springframework.batch.io.xml.stax.TransactionalEventReader;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ResourceLifecycle;
import org.springframework.batch.item.StreamContext;
import org.springframework.batch.item.reader.AbstractItemReader;
import org.springframework.batch.item.stream.GenericStreamContext;
@@ -43,7 +42,7 @@ import org.springframework.util.Assert;
*
* @author Robert Kasanicky
*/
public class StaxEventItemReader extends AbstractItemReader implements ItemReader, ResourceLifecycle,
public class StaxEventItemReader extends AbstractItemReader implements ItemReader,
Skippable, ItemStream, StatisticsProvider, InitializingBean, DisposableBean {
public static final String READ_COUNT_STATISTICS_NAME = "StaxEventReaderItemReader.readCount";

View File

@@ -18,7 +18,6 @@ import org.springframework.batch.io.support.FileUtils;
import org.springframework.batch.io.xml.stax.NoStartEndDocumentStreamWriter;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.ResourceLifecycle;
import org.springframework.batch.item.StreamContext;
import org.springframework.batch.item.stream.GenericStreamContext;
import org.springframework.batch.repeat.synch.BatchTransactionSynchronizationManager;
@@ -42,7 +41,7 @@ import org.springframework.util.CollectionUtils;
* @author Peter Zozom
*
*/
public class StaxEventItemWriter implements ItemWriter, ResourceLifecycle, ItemStream,
public class StaxEventItemWriter implements ItemWriter, ItemStream,
StatisticsProvider, InitializingBean, DisposableBean {
// default encoding

View File

@@ -38,7 +38,7 @@ import org.springframework.batch.item.reader.AbstractItemReader;
* @author Lucas Ward
* @since 1.0
*/
public interface ItemReader {
public interface ItemReader extends ItemStream {
/**
* Reads a piece of input data and advance to the next one. Implementations
@@ -51,9 +51,4 @@ public interface ItemReader {
*/
Object read() throws Exception;
/**
* Close the reader, freeing any resources that may have been allocated
* since the first call to read().
*/
void close() throws Exception;
}

View File

@@ -35,6 +35,16 @@ package org.springframework.batch.item;
*
*/
public interface ItemStream {
void open() throws Exception;
/**
* Close the reader, freeing any resources that may have been allocated
* since the first call to read().
*
* @throws Exception if an underlying resource is unavailable
*/
void close() throws Exception;
/**
* Get {@link StreamContext} representing this object's current state.

View File

@@ -25,7 +25,7 @@ package org.springframework.batch.item;
* @author Dave Syer
* @author Lucas Ward
*/
public interface ItemWriter {
public interface ItemWriter extends ItemStream {
/**
* Process the supplied data element. Will be called multiple times during a
@@ -38,10 +38,4 @@ public interface ItemWriter {
*/
public void write(Object item) throws Exception;
/**
* Close the writer, allowing all allocated resources to be cleaned up.
*
* @throws Exception
*/
void close() throws Exception;
}

View File

@@ -1,37 +0,0 @@
/*
* 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.item;
/**
* Common interface for classes that require initialisation before they can be
* used and need to free resources after they are no longer used.
*/
public interface ResourceLifecycle {
/**
* This method should be invoked by clients at the start of processing to
* allow initialisation of resources.
*
*/
public void open();
/**
* This method should be invoked by clients after the completion of each
* step and the implementing class should close all managed resources.
*
*/
public void close();
}

View File

@@ -17,19 +17,13 @@
package org.springframework.batch.item.reader;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.stream.ItemStreamAdapter;
/**
* Base class for {@link ItemReader} implementations.
* @author Dave Syer
*
*/
public abstract class AbstractItemReader implements ItemReader {
/**
* Do nothing.
* @see org.springframework.batch.item.ItemReader#close()
*/
public void close() throws Exception {
}
public abstract class AbstractItemReader extends ItemStreamAdapter implements ItemReader {
}

View File

@@ -16,7 +16,11 @@
package org.springframework.batch.item.reader;
import java.util.Properties;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.StreamContext;
import org.springframework.batch.item.stream.GenericStreamContext;
import org.springframework.batch.support.AbstractMethodInvokingDelegator;
/**
@@ -35,10 +39,33 @@ public class ItemReaderAdapter extends AbstractMethodInvokingDelegator implement
/**
* Do nothing.
*
* @see org.springframework.batch.item.ItemReader#close()
* @see org.springframework.batch.item.ItemStream#open()
*/
public void open() throws Exception {
// no-op
}
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#close()
*/
public void close() throws Exception {
// no-op
}
/**
* Return empty {@link StreamContext}.
* @see org.springframework.batch.item.ItemStream#getRestartData()
*/
public StreamContext getRestartData() {
return new GenericStreamContext(new Properties());
}
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#restoreFrom(org.springframework.batch.item.StreamContext)
*/
public void restoreFrom(StreamContext data) {
}

View File

@@ -1,31 +0,0 @@
/*
* 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.item.writer;
import org.springframework.batch.item.ItemWriter;
/**
* Abstract {@link ItemWriter} that allows for base classes to only
* implement the close method if they need it.
*
* @author Lucas Ward
*
*/
public abstract class AbstractItemWriter implements ItemWriter{
public void close() throws Exception {
}
}

View File

@@ -11,13 +11,14 @@ import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.StreamContext;
import org.springframework.batch.item.stream.GenericStreamContext;
import org.springframework.batch.item.stream.ItemStreamAdapter;
/**
* Runs a collection of ItemProcessors in fixed-order sequence.
*
* @author Robert Kasanicky
*/
public class CompositeItemWriter implements ItemWriter, ItemStream {
public class CompositeItemWriter extends ItemStreamAdapter implements ItemWriter, ItemStream {
private static final String SEPARATOR = "#";
@@ -38,13 +39,8 @@ public class CompositeItemWriter implements ItemWriter, ItemStream {
*/
public StreamContext getRestartData() {
Properties props = createCompoundProperties(new PropertiesExtractor() {
public Properties extractProperties(Object o) {
if (o instanceof ItemStream) {
return ((ItemStream) o).getRestartData().getProperties();
}
else {
return null;
}
public Properties extractProperties(ItemStream o) {
return o.getRestartData().getProperties();
}
});
return new GenericStreamContext(props);
@@ -105,7 +101,7 @@ public class CompositeItemWriter implements ItemWriter, ItemStream {
Properties stats = new Properties();
int index = 0;
for (Iterator iterator = delegates.listIterator(); iterator.hasNext();) {
Properties writerStats = extractor.extractProperties(iterator.next());
Properties writerStats = extractor.extractProperties((ItemStream) iterator.next());
if (writerStats != null) {
for (Iterator iterator2 = writerStats.entrySet().iterator(); iterator2.hasNext();) {
Map.Entry entry = (Map.Entry) iterator2.next();
@@ -123,7 +119,7 @@ public class CompositeItemWriter implements ItemWriter, ItemStream {
* null.
*/
private interface PropertiesExtractor {
Properties extractProperties(Object o);
Properties extractProperties(ItemStream o);
}
public void close() throws Exception {

View File

@@ -7,6 +7,7 @@ import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.StreamContext;
import org.springframework.batch.item.stream.GenericStreamContext;
import org.springframework.batch.item.stream.ItemStreamAdapter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
@@ -17,7 +18,7 @@ import org.springframework.util.Assert;
* @author Dave Syer
* @author Robert Kasanicky
*/
public class DelegatingItemWriter implements ItemWriter, ItemStream, Skippable, InitializingBean {
public class DelegatingItemWriter extends ItemStreamAdapter implements ItemWriter, Skippable, InitializingBean {
private ItemWriter writer;

View File

@@ -16,7 +16,11 @@
package org.springframework.batch.item.writer;
import java.util.Properties;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.StreamContext;
import org.springframework.batch.item.stream.GenericStreamContext;
import org.springframework.batch.support.AbstractMethodInvokingDelegator;
@@ -34,13 +38,35 @@ public class ItemWriterAdapter extends AbstractMethodInvokingDelegator implement
invokeDelegateMethodWithArgument(item);
}
/*
* No-op, can't call more than one method.
*
* (non-Javadoc)
* @see org.springframework.batch.item.ItemWriter#close()
*/
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#open()
*/
public void open() throws Exception {
// no-op
}
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#close()
*/
public void close() throws Exception {
// no-op
}
/**
* Return empty {@link StreamContext}.
* @see org.springframework.batch.item.ItemStream#getRestartData()
*/
public StreamContext getRestartData() {
return new GenericStreamContext(new Properties());
}
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#restoreFrom(org.springframework.batch.item.StreamContext)
*/
public void restoreFrom(StreamContext data) {
}

View File

@@ -16,7 +16,11 @@
package org.springframework.batch.item.writer;
import java.util.Properties;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.StreamContext;
import org.springframework.batch.item.stream.GenericStreamContext;
import org.springframework.batch.support.AbstractMethodInvokingDelegator;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
@@ -66,7 +70,35 @@ public class PropertyExtractingDelegatingItemWriter extends AbstractMethodInvoki
this.fieldsUsedAsTargetMethodArguments = fieldsUsedAsMethodArguments;
}
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#open()
*/
public void open() throws Exception {
// no-op
}
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#close()
*/
public void close() throws Exception {
// no-op
}
/**
* Return empty {@link StreamContext}.
* @see org.springframework.batch.item.ItemStream#getRestartData()
*/
public StreamContext getRestartData() {
return new GenericStreamContext(new Properties());
}
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#restoreFrom(org.springframework.batch.item.StreamContext)
*/
public void restoreFrom(StreamContext data) {
}
}

View File

@@ -22,7 +22,7 @@ import java.util.Map;
import junit.framework.TestCase;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.writer.AbstractItemWriter;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.batch.repeat.RepeatContext;
import org.springframework.batch.repeat.RepeatInterceptor;
@@ -41,12 +41,13 @@ public class HibernateAwareItemWriterTests extends TestCase {
public void flush() throws DataAccessException {
list.add("flush");
}
public void clear() {
list.add("clear");
list.add("clear");
};
}
private class StubItemWriter implements ItemWriter, RepeatInterceptor {
private class StubItemWriter extends AbstractItemWriter implements RepeatInterceptor {
public void write(Object item) {
list.add(item);
}
@@ -70,13 +71,10 @@ public class HibernateAwareItemWriterTests extends TestCase {
public void open(RepeatContext context) {
list.add(context);
}
public void close() throws Exception {
}
}
HibernateAwareItemWriter writer = new HibernateAwareItemWriter();
final List list = new ArrayList();
private RepeatContextSupport context;
@@ -93,15 +91,16 @@ public class HibernateAwareItemWriterTests extends TestCase {
writer.setHibernateTemplate(new HibernateTemplateWrapper());
list.clear();
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see junit.framework.TestCase#tearDown()
*/
protected void tearDown() throws Exception {
Map map = TransactionSynchronizationManager.getResourceMap();
for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
String key = (String) iterator.next();
TransactionSynchronizationManager.unbindResource(key);
TransactionSynchronizationManager.unbindResource(key);
}
}
@@ -116,10 +115,10 @@ public class HibernateAwareItemWriterTests extends TestCase {
try {
writer.afterPropertiesSet();
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
catch (IllegalArgumentException e) {
// expected
assertTrue("Wrong message for exception: " + e.getMessage(), e
.getMessage().indexOf("delegate") >= 0);
assertTrue("Wrong message for exception: " + e.getMessage(), e.getMessage().indexOf("delegate") >= 0);
}
}
@@ -136,7 +135,7 @@ public class HibernateAwareItemWriterTests extends TestCase {
/**
* Test method for
* {@link org.springframework.batch.io.support.HibernateAwareItemWriter#write(java.lang.Object)}.
* @throws Exception
* @throws Exception
*/
public void testWrite() throws Exception {
writer.write("foo");
@@ -158,7 +157,8 @@ public class HibernateAwareItemWriterTests extends TestCase {
try {
writer.close(context);
fail("Expected RuntimeException");
} catch (RuntimeException e) {
}
catch (RuntimeException e) {
assertEquals("bar", e.getMessage());
}
assertEquals(2, list.size());
@@ -169,7 +169,7 @@ public class HibernateAwareItemWriterTests extends TestCase {
/**
* Test method for
* {@link org.springframework.batch.io.support.HibernateAwareItemWriter#write(java.lang.Object)}.
* @throws Exception
* @throws Exception
*/
public void testWriteAndCloseWithFailure() throws Exception {
final RuntimeException ex = new RuntimeException("bar");
@@ -182,7 +182,8 @@ public class HibernateAwareItemWriterTests extends TestCase {
try {
writer.close(context);
fail("Expected RuntimeException");
} catch (RuntimeException e) {
}
catch (RuntimeException e) {
assertEquals("bar", e.getMessage());
}
assertEquals(3, list.size());

View File

@@ -8,11 +8,10 @@ import java.util.Properties;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.batch.item.ItemStream;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.StreamContext;
import org.springframework.batch.item.stream.GenericStreamContext;
import org.springframework.batch.item.writer.CompositeItemWriter;
import org.springframework.batch.item.stream.ItemStreamAdapter;
import org.springframework.batch.statistics.StatisticsProvider;
/**
@@ -62,14 +61,9 @@ public class CompositeItemWriterTests extends TestCase {
* All Restartable processors should be restarted, not-Restartable processors should be ignored.
*/
public void testRestart() {
//this mock with undefined behavior makes sure not-Restartable processor is ignored
MockControl p1c = MockControl.createStrictControl(ItemWriter.class);
final ItemWriter p1 = (ItemWriter) p1c.getMock();
final ItemWriter p2 = new ItemWriterStub();
final ItemWriter p3 = new ItemWriterStub();
List itemProcessors = new ArrayList(){{
add(p1);
add(p2);
add(p3);
}};
@@ -120,7 +114,7 @@ public class CompositeItemWriterTests extends TestCase {
* Stub for testing restart. Checks the restart data received is the same that was returned by
* <code>getRestartData()</code>
*/
private static class ItemWriterStub implements ItemWriter, ItemStream, StatisticsProvider {
private static class ItemWriterStub extends ItemStreamAdapter implements ItemWriter, StatisticsProvider {
private static final String RESTART_KEY = "restartData";
private static final String STATS_KEY = "stats";

View File

@@ -19,9 +19,9 @@ package org.springframework.batch.repeat.support;
import java.util.HashSet;
import java.util.Set;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.batch.repeat.RepeatCallback;
import org.springframework.batch.repeat.RepeatContext;
import org.springframework.batch.repeat.ExitStatus;
import org.springframework.batch.repeat.callback.ItemReaderRepeatCallback;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

View File

@@ -17,7 +17,6 @@
package org.springframework.batch.sample.dao;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.ResourceLifecycle;
import org.springframework.batch.sample.domain.CustomerCredit;
import org.springframework.beans.factory.DisposableBean;
@@ -56,17 +55,13 @@ public class FlatFileCustomerCreditWriter implements CustomerCreditDao,
this.outputSource = outputSource;
}
public void open() {
if (outputSource instanceof ResourceLifecycle) {
((ResourceLifecycle) outputSource).open();
}
public void open() throws Exception {
outputSource.open();
opened = true;
}
public void close() {
if (outputSource instanceof ResourceLifecycle) {
((ResourceLifecycle) outputSource).close();
}
public void close() throws Exception {
outputSource.close();
}
/*

View File

@@ -1,6 +1,10 @@
package org.springframework.batch.sample.dao;
import java.util.Properties;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.StreamContext;
import org.springframework.batch.item.stream.GenericStreamContext;
import org.springframework.batch.sample.domain.Game;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.util.Assert;
@@ -34,7 +38,36 @@ public class JdbcGameDao extends JdbcDaoSupport implements ItemWriter {
this.getJdbcTemplate().update(INSERT_GAME, args);
}
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#open()
*/
public void open() throws Exception {
// no-op
}
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#close()
*/
public void close() throws Exception {
// no-op
}
/**
* Return empty {@link StreamContext}.
* @see org.springframework.batch.item.ItemStream#getRestartData()
*/
public StreamContext getRestartData() {
return new GenericStreamContext(new Properties());
}
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#restoreFrom(org.springframework.batch.item.StreamContext)
*/
public void restoreFrom(StreamContext data) {
}
}

View File

@@ -1,6 +1,10 @@
package org.springframework.batch.sample.dao;
import java.util.Properties;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.StreamContext;
import org.springframework.batch.item.stream.GenericStreamContext;
import org.springframework.batch.sample.domain.PlayerSummary;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.util.Assert;
@@ -28,7 +32,36 @@ public class JdbcPlayerSummaryDao extends JdbcDaoSupport implements ItemWriter {
getJdbcTemplate().update(INSERT_SUMMARY, args);
}
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#open()
*/
public void open() throws Exception {
// no-op
}
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#close()
*/
public void close() throws Exception {
// no-op
}
/**
* Return empty {@link StreamContext}.
* @see org.springframework.batch.item.ItemStream#getRestartData()
*/
public StreamContext getRestartData() {
return new GenericStreamContext(new Properties());
}
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#restoreFrom(org.springframework.batch.item.StreamContext)
*/
public void restoreFrom(StreamContext data) {
}
}

View File

@@ -5,6 +5,7 @@ import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import org.apache.commons.lang.SerializationUtils;
import org.apache.commons.logging.Log;
@@ -12,7 +13,8 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.batch.execution.scope.StepContext;
import org.springframework.batch.execution.scope.StepContextAware;
import org.springframework.batch.item.KeyedItemReader;
import org.springframework.batch.item.ResourceLifecycle;
import org.springframework.batch.item.StreamContext;
import org.springframework.batch.item.stream.GenericStreamContext;
import org.springframework.batch.repeat.synch.BatchTransactionSynchronizationManager;
import org.springframework.batch.sample.item.writer.StagingItemWriter;
import org.springframework.beans.factory.DisposableBean;
@@ -26,7 +28,7 @@ import org.springframework.transaction.support.TransactionSynchronizationAdapter
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.util.Assert;
public class StagingItemReader extends JdbcDaoSupport implements KeyedItemReader, ResourceLifecycle, DisposableBean,
public class StagingItemReader extends JdbcDaoSupport implements KeyedItemReader, DisposableBean,
StepContextAware {
// Key for buffer in transaction synchronization manager
@@ -250,5 +252,21 @@ public class StagingItemReader extends JdbcDaoSupport implements KeyedItemReader
return "list=" + list + "; iter.hasNext()=" + iter.hasNext();
}
}
/**
* Return empty {@link StreamContext}.
* @see org.springframework.batch.item.ItemStream#getRestartData()
*/
public StreamContext getRestartData() {
return new GenericStreamContext(new Properties());
}
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#restoreFrom(org.springframework.batch.item.StreamContext)
*/
public void restoreFrom(StreamContext data) {
}
}

View File

@@ -3,6 +3,7 @@ package org.springframework.batch.sample.item.writer;
import java.math.BigDecimal;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.writer.AbstractItemWriter;
import org.springframework.batch.sample.dao.CustomerCreditDao;
import org.springframework.batch.sample.domain.CustomerCredit;
@@ -11,7 +12,7 @@ import org.springframework.batch.sample.domain.CustomerCredit;
*
* @author Robert Kasanicky
*/
public class CustomerCreditIncreaseWriter implements ItemWriter {
public class CustomerCreditIncreaseWriter extends AbstractItemWriter implements ItemWriter {
public static final BigDecimal FIXED_AMOUNT = new BigDecimal(1000);
@@ -34,6 +35,9 @@ public class CustomerCreditIncreaseWriter implements ItemWriter {
customerCreditDao.writeCredit(customerCredit);
}
public void open() throws Exception {
}
public void close() throws Exception {
}

View File

@@ -17,12 +17,13 @@
package org.springframework.batch.sample.item.writer;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.writer.AbstractItemWriter;
import org.springframework.batch.sample.dao.CustomerCreditDao;
import org.springframework.batch.sample.domain.CustomerCredit;
public class CustomerCreditUpdateWriter implements ItemWriter {
public class CustomerCreditUpdateWriter extends AbstractItemWriter implements ItemWriter {
private double creditFilter = 800;
private CustomerCreditDao dao;

View File

@@ -17,6 +17,7 @@
package org.springframework.batch.sample.item.writer;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.writer.AbstractItemWriter;
import org.springframework.batch.sample.dao.JdbcCustomerDebitWriter;
import org.springframework.batch.sample.domain.CustomerDebit;
import org.springframework.batch.sample.domain.Trade;
@@ -27,7 +28,7 @@ import org.springframework.batch.sample.domain.Trade;
*
* @author Robert Kasanicky
*/
public class CustomerUpdateWriter implements ItemWriter {
public class CustomerUpdateWriter extends AbstractItemWriter implements ItemWriter {
private JdbcCustomerDebitWriter dao;
public void write(Object data) {
@@ -42,9 +43,6 @@ public class CustomerUpdateWriter implements ItemWriter {
this.dao = outputSource;
}
public void close() {
}
public void init() {
}
}

View File

@@ -15,19 +15,16 @@
*/
package org.springframework.batch.sample.item.writer;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.writer.AbstractItemWriter;
/**
* @author Dave Syer
*
*/
public class DummyItemWriter implements ItemWriter {
public class DummyItemWriter extends AbstractItemWriter {
public void write(Object item) throws Exception {
// NO-OP
}
public void close() throws Exception {
}
}

View File

@@ -18,12 +18,12 @@ package org.springframework.batch.sample.item.writer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.writer.AbstractItemWriter;
import org.springframework.batch.sample.domain.Person;
public class PersonWriter implements ItemWriter {
public class PersonWriter extends AbstractItemWriter {
private static Log log = LogFactory.getLog(PersonWriter.class);
public void write(Object data) {
@@ -36,9 +36,6 @@ public class PersonWriter implements ItemWriter {
log.debug("Processing: " + data);
}
public void close() {
}
public void init() {
}
}

View File

@@ -1,10 +1,10 @@
package org.springframework.batch.sample.item.writer;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.writer.AbstractItemWriter;
import org.springframework.batch.sample.dao.PlayerDao;
import org.springframework.batch.sample.domain.Player;
public class PlayerItemWriter implements ItemWriter {
public class PlayerItemWriter extends AbstractItemWriter {
PlayerDao playerDao;
@@ -16,6 +16,4 @@ public class PlayerItemWriter implements ItemWriter {
this.playerDao = playerDao;
}
public void close() throws Exception {
}
}

View File

@@ -2,11 +2,14 @@ package org.springframework.batch.sample.item.writer;
import java.io.Serializable;
import java.sql.Types;
import java.util.Properties;
import org.apache.commons.lang.SerializationUtils;
import org.springframework.batch.execution.scope.StepContext;
import org.springframework.batch.execution.scope.StepContextAware;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.StreamContext;
import org.springframework.batch.item.stream.GenericStreamContext;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
import org.springframework.util.Assert;
@@ -73,7 +76,37 @@ public class StagingItemWriter extends JdbcDaoSupport implements
new int[] { Types.BIGINT, Types.BIGINT, Types.BLOB, Types.CHAR});
}
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#open()
*/
public void open() throws Exception {
// no-op
}
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#close()
*/
public void close() throws Exception {
// no-op
}
/**
* Return empty {@link StreamContext}.
* @see org.springframework.batch.item.ItemStream#getRestartData()
*/
public StreamContext getRestartData() {
return new GenericStreamContext(new Properties());
}
/**
* Do nothing.
* @see org.springframework.batch.item.ItemStream#restoreFrom(org.springframework.batch.item.StreamContext)
*/
public void restoreFrom(StreamContext data) {
}
}

View File

@@ -18,13 +18,13 @@ package org.springframework.batch.sample.item.writer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.writer.AbstractItemWriter;
import org.springframework.batch.sample.dao.TradeDao;
import org.springframework.batch.sample.domain.Trade;
public class TradeWriter implements ItemWriter {
public class TradeWriter extends AbstractItemWriter {
private static Log log = LogFactory.getLog(TradeWriter.class);
private TradeDao dao;

View File

@@ -2,12 +2,11 @@ package org.springframework.batch.sample.dao;
import java.math.BigDecimal;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.ResourceLifecycle;
import org.springframework.batch.sample.domain.CustomerCredit;
import junit.framework.TestCase;
import org.easymock.MockControl;
import junit.framework.TestCase;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.sample.domain.CustomerCredit;
public class FlatFileCustomerCreditWriterTests extends TestCase {
@@ -27,7 +26,7 @@ public class FlatFileCustomerCreditWriterTests extends TestCase {
writer.setOutputSource(output);
}
public void testOpen() {
public void testOpen() throws Exception {
//set-up outputSource mock
output.open();
@@ -75,7 +74,7 @@ public class FlatFileCustomerCreditWriterTests extends TestCase {
outputControl.verify();
}
private interface ResourceLifecycleItemWriter extends ItemWriter, ResourceLifecycle {
private interface ResourceLifecycleItemWriter extends ItemWriter {
}
}