BATCH-365: Added flush() and clear() to ItemWriter interface. These should be removed from ItemStream after the same is done for the ItemReader interface.

This commit is contained in:
lucasward
2008-02-24 01:55:48 +00:00
parent 325a383eae
commit 111a7c9629
13 changed files with 240 additions and 184 deletions

View File

@@ -52,6 +52,10 @@ import org.springframework.util.Assert;
*
* Use {@link #write(String)} method to output a line to an item writer.
*
* <p>This class will be updated in the future to use a buffering approach
* to handling transactions, rather than outputting directly to the file and
* truncating on rollback</p>
*
* @author Waseem Malik
* @author Tomas Slanina
* @author Robert Kasanicky
@@ -465,23 +469,31 @@ public class FlatFileItemWriter extends AbstractTransactionalIoSource implements
return true;
}
/*
/* To be deleted once interface changes are complete
* (non-Javadoc)
* @see org.springframework.batch.io.support.AbstractTransactionalIoSource#mark(org.springframework.batch.item.ExecutionContext)
*/
public void mark() {
getOutputState().mark();
}
/*
/* To be deleted once interface changes are complete
* (non-Javadoc)
* @see org.springframework.batch.io.support.AbstractTransactionalIoSource#reset(org.springframework.batch.item.ExecutionContext)
*/
public void reset() throws ResetFailedException {
}
public void clear() throws Exception {
try {
getOutputState().reset();
} catch (BatchCriticalException e) {
throw new ResetFailedException(e);
}
}
public void flush() throws Exception {
getOutputState().mark();
}
}

View File

@@ -146,32 +146,7 @@ public class HibernateAwareItemWriter implements ItemWriter, RepeatListener, Ini
* @see org.springframework.batch.repeat.RepeatListener#close(org.springframework.batch.repeat.RepeatContext)
*/
public void close(RepeatContext context) {
try {
if (delegate instanceof RepeatListener) {
RepeatListener interceptor = (RepeatListener) delegate;
interceptor.close(context);
}
flush();
} catch (RuntimeException e) {
synchronized (failed) {
failed.addAll(getProcessed());
}
// onError will not be called after close() by the framework so we
// have to do it here.
onError(context, e);
throw e;
}
unsetContext();
}
/**
* Wrapper for Hibernate flush.
*/
private void flush() {
hibernateTemplate.flush();
// This should happen when the transaction commits anyway, but to be
// sure...
hibernateTemplate.clear();
}
/**
@@ -246,7 +221,7 @@ public class HibernateAwareItemWriter implements ItemWriter, RepeatListener, Ini
*
* @return the context
*/
private void flushIfNecessary(Object output) {
private void flushIfNecessary(Object output) throws Exception{
RepeatContext context = (RepeatContext) TransactionSynchronizationManager.getResource(WRITER_REPEAT_CONTEXT);
boolean flush;
synchronized (failed) {
@@ -264,4 +239,34 @@ public class HibernateAwareItemWriter implements ItemWriter, RepeatListener, Ini
}
public void clear() throws Exception {
if(delegate != null){
delegate.clear();
}
hibernateTemplate.clear();
}
/**
* Flush the Hibernate session. The delegate flush will also be called before finishing.
*/
public void flush() throws Exception {
try {
if (delegate != null) {
delegate.flush();
}
hibernateTemplate.flush();
// This should happen when the transaction commits anyway, but to be
// sure...
hibernateTemplate.clear();
} catch (RuntimeException e) {
synchronized (failed) {
failed.addAll(getProcessed());
}
// This used to contain a call to onError, however, I think this
// should be handled within the step.
throw e;
}
unsetContext();
}
}

View File

@@ -438,20 +438,27 @@ public class StaxEventItemWriter implements ItemWriter, ItemStream, Initializing
return true;
}
/*
/* TODO remove once ItemStream interface is modified.
* (non-Javadoc)
* @see org.springframework.batch.item.ItemStream#mark(org.springframework.batch.item.ExecutionContext)
*/
public void mark() {
}
public void flush() throws Exception {
lastCommitPointPosition = getPosition();
lastCommitPointRecordCount = currentRecordCount;
}
/*
/* TODO remove once ItemStream interface is modified.
* (non-Javadoc)
* @see org.springframework.batch.item.ItemStream#reset(org.springframework.batch.item.ExecutionContext)
*/
public void reset() {
}
public void clear() throws Exception {
currentRecordCount = lastCommitPointRecordCount;
// close output
close();

View File

@@ -17,10 +17,18 @@
package org.springframework.batch.item;
/**
* Basic interface for generic output operations. Class implementing this
* <p>Basic interface for generic output operations. Class implementing this
* interface will be responsible for serializing objects as necessary.
* Generally, it is responsibility of implementing class to decide which
* technology to use for mapping and how it should be configured.
* technology to use for mapping and how it should be configured.</p>
*
* <p>
* Due to the nature of batch processing, it is expected that most writers
* will buffer output. A flush method is provided to the interface in order
* to ensure that any buffers can be flushed before a transaction is
* committed. Along the same lines, if a transaction has been rolled back,
* then the contents of any buffers should be thrown away.
* </p>
*
* @author Dave Syer
* @author Lucas Ward
@@ -38,4 +46,19 @@ public interface ItemWriter {
*/
public void write(Object item) throws Exception;
/**
* Flush any buffers that are being held. This will usually be performed
* prior to committing any transactions.
*
* @throws Exception
*/
public void flush() throws Exception;
/**
* Clear any buffers that are being held. This will usually be performed
* prior to rolling back any transactions.
*
* @throws Exception
*/
public void clear() throws Exception;
}

View File

@@ -19,11 +19,17 @@ import org.springframework.batch.item.ItemWriter;
/**
* Abstract {@link ItemWriter} that allows for base classes to only
* implement the close method if they need it.
* implement the close method if they need it. Because it is likely
* that the flush and clear methods may not need to be implemented,
* they are provided in this class.
*
* @author Lucas Ward
*
*/
public abstract class AbstractItemWriter implements ItemWriter{
public void flush() throws Exception {
}
public void clear() throws Exception {
}
}

View File

@@ -1,6 +1,5 @@
package org.springframework.batch.item.writer;
import org.springframework.batch.io.Skippable;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
@@ -11,7 +10,7 @@ import org.springframework.util.Assert;
* @author Dave Syer
* @author Robert Kasanicky
*/
public class DelegatingItemWriter implements ItemWriter, Skippable, InitializingBean {
public class DelegatingItemWriter implements ItemWriter, InitializingBean {
private ItemWriter writer;
@@ -43,14 +42,22 @@ public class DelegatingItemWriter implements ItemWriter, Skippable, Initializing
this.writer = writer;
}
public void skip() {
if (writer instanceof Skippable) {
((Skippable) writer).skip();
}
}
public void afterPropertiesSet() throws Exception {
Assert.notNull(writer);
}
/**
* Delegates to {@link ItemWriter#clear()}
*/
public void clear() throws Exception {
writer.clear();
}
/**
* Delegates to {@link ItemWriter#flush()}
*/
public void flush() throws Exception {
writer.flush();
}
}

View File

@@ -37,11 +37,15 @@ public class ItemWriterAdapter extends AbstractMethodInvokingDelegator implement
/*
* No-op, can't call more than one method.
*
* (non-Javadoc)
* @see org.springframework.batch.item.ItemWriter#close()
*/
public void close() throws Exception {
public void clear() throws Exception {
}
/*
* No-op, can't call more than one method.
*
*/
public void flush() throws Exception {
}
}

View File

@@ -67,6 +67,10 @@ public class PropertyExtractingDelegatingItemWriter extends AbstractMethodInvoki
}
public void close() throws Exception {
public void clear() throws Exception {
}
public void flush() throws Exception {
}
}