OPEN - issue BATCH-788: Remove flush/clear from ItemWriter
Infrastructure and core working with no calls to flush (I think)
This commit is contained in:
@@ -16,7 +16,8 @@
|
||||
package org.springframework.batch.item;
|
||||
|
||||
/**
|
||||
* Unchecked exception indicating that an error has occurred while trying to call {@link ItemWriter#clear()}
|
||||
* Unchecked exception indicating that an error has occurred while trying to
|
||||
* clear a buffer on a rollback.
|
||||
*
|
||||
* @author Lucas Ward
|
||||
* @author Ben Hale
|
||||
@@ -24,7 +25,8 @@ package org.springframework.batch.item;
|
||||
public class ClearFailedException extends ItemWriterException {
|
||||
|
||||
/**
|
||||
* Create a new {@link ClearFailedException} based on a message and another exception.
|
||||
* Create a new {@link ClearFailedException} based on a message and another
|
||||
* exception.
|
||||
*
|
||||
* @param message the message for this exception
|
||||
* @param cause the other exception
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
package org.springframework.batch.item;
|
||||
|
||||
/**
|
||||
* Unchecked exception indicating that an error has occurred while trying to call {@link ItemWriter#flush()}
|
||||
* Unchecked exception indicating that an error has occurred while trying to flush a buffer.
|
||||
*
|
||||
* @author Lucas Ward
|
||||
* @author Ben Hale
|
||||
|
||||
@@ -56,6 +56,7 @@ public interface ItemWriter<T> {
|
||||
* @throws FlushFailedException in case of an error. If this exception is
|
||||
* thrown the writer may be in an inconsistent state and manual intervention
|
||||
* might be required to reconcile the data with persistent output.
|
||||
* @deprecated
|
||||
*/
|
||||
void flush() throws FlushFailedException;
|
||||
|
||||
@@ -65,6 +66,7 @@ public interface ItemWriter<T> {
|
||||
* @throws ClearFailedException in case of an error. If this exception is
|
||||
* thrown the writer may be in an inconsistent state and manual intervention
|
||||
* might be required to reconcile the data with persistent output.
|
||||
* @deprecated
|
||||
*/
|
||||
void clear() throws ClearFailedException;
|
||||
}
|
||||
|
||||
@@ -86,8 +86,6 @@ public class FlatFileItemWriter<T> extends ExecutionContextUserSupport implement
|
||||
|
||||
private String encoding = OutputState.DEFAULT_CHARSET;
|
||||
|
||||
private List<String> lineBuffer = new ArrayList<String>();
|
||||
|
||||
private List<String> headerLines = new ArrayList<String>();
|
||||
|
||||
private String lineSeparator = DEFAULT_LINE_SEPARATOR;
|
||||
@@ -185,16 +183,24 @@ public class FlatFileItemWriter<T> extends ExecutionContextUserSupport implement
|
||||
*/
|
||||
public void write(List<? extends T> items) throws Exception {
|
||||
|
||||
for (T item : items) {
|
||||
|
||||
if (getOutputState().isInitialized()) {
|
||||
lineBuffer.add(lineAggregator.aggregate(item) + lineSeparator);
|
||||
}
|
||||
else {
|
||||
throw new WriterNotOpenException("Writer must be open before it can be written to");
|
||||
}
|
||||
|
||||
if (!getOutputState().isInitialized()) {
|
||||
throw new WriterNotOpenException("Writer must be open before it can be written to");
|
||||
}
|
||||
|
||||
OutputState state = getOutputState();
|
||||
|
||||
for (T item : items) {
|
||||
String line = lineAggregator.aggregate(item) + lineSeparator;
|
||||
try {
|
||||
state.write(line);
|
||||
} catch (IOException e) {
|
||||
throw new FlushFailedException(
|
||||
"Could not write data. The file may be corrupt.", e);
|
||||
}
|
||||
}
|
||||
|
||||
state.mark();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -222,7 +228,7 @@ public class FlatFileItemWriter<T> extends ExecutionContextUserSupport implement
|
||||
}
|
||||
}
|
||||
|
||||
private void doOpen(ExecutionContext executionContext) {
|
||||
private void doOpen(ExecutionContext executionContext) throws ItemStreamException {
|
||||
OutputState outputState = getOutputState();
|
||||
if (executionContext.containsKey(getKey(RESTART_DATA_NAME))) {
|
||||
outputState.restoreFrom(executionContext);
|
||||
@@ -234,8 +240,14 @@ public class FlatFileItemWriter<T> extends ExecutionContextUserSupport implement
|
||||
throw new ItemStreamException("Failed to initialize writer", ioe);
|
||||
}
|
||||
if (outputState.lastMarkedByteOffsetPosition == 0) {
|
||||
for (String line : headerLines) {
|
||||
lineBuffer.add(line + lineSeparator);
|
||||
try {
|
||||
for (String line : headerLines) {
|
||||
outputState.write(line + lineSeparator);
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new FlushFailedException(
|
||||
"Could not write headers. The file may be corrupt.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -264,17 +276,6 @@ public class FlatFileItemWriter<T> extends ExecutionContextUserSupport implement
|
||||
}
|
||||
|
||||
public void flush() throws FlushFailedException {
|
||||
OutputState state = getOutputState();
|
||||
for (String line : lineBuffer) {
|
||||
try {
|
||||
state.write(line);
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new FlushFailedException("Failed to write line to output file: " + line, e);
|
||||
}
|
||||
}
|
||||
lineBuffer.clear();
|
||||
state.mark();
|
||||
}
|
||||
|
||||
// Returns object representing state.
|
||||
@@ -501,7 +502,6 @@ public class FlatFileItemWriter<T> extends ExecutionContextUserSupport implement
|
||||
}
|
||||
|
||||
public void clear() throws ClearFailedException {
|
||||
lineBuffer.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,8 +20,7 @@ import org.springframework.batch.item.FlushFailedException;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
|
||||
/**
|
||||
* Abstract {@link ItemWriter} that allows for base classes to only implement
|
||||
* the {@link #flush()} and {@link #clear()} methods if they need it.
|
||||
* Abstract {@link ItemWriter}.
|
||||
*
|
||||
* @author Lucas Ward
|
||||
*/
|
||||
|
||||
@@ -3,8 +3,6 @@ package org.springframework.batch.item.support;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.item.ClearFailedException;
|
||||
import org.springframework.batch.item.FlushFailedException;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
|
||||
/**
|
||||
@@ -15,7 +13,7 @@ import org.springframework.batch.item.ItemWriter;
|
||||
* @author Robert Kasanicky
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class CompositeItemWriter<T> implements ItemWriter<T> {
|
||||
public class CompositeItemWriter<T> extends AbstractItemWriter<T> {
|
||||
|
||||
private List<ItemWriter<? super T>> delegates;
|
||||
|
||||
@@ -32,16 +30,4 @@ public class CompositeItemWriter<T> implements ItemWriter<T> {
|
||||
}
|
||||
}
|
||||
|
||||
public void clear() throws ClearFailedException {
|
||||
for (ItemWriter<? super T> writer : delegates) {
|
||||
writer.clear();
|
||||
}
|
||||
}
|
||||
|
||||
public void flush() throws FlushFailedException {
|
||||
for (ItemWriter<? super T> writer : delegates) {
|
||||
writer.flush();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -109,18 +109,11 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
|
||||
// byte offset in file channel at last commit point
|
||||
private long lastCommitPointPosition = 0;
|
||||
|
||||
// processed record count at last commit point
|
||||
private long lastCommitPointRecordCount = 0;
|
||||
|
||||
// current count of processed records
|
||||
private long currentRecordCount = 0;
|
||||
|
||||
private boolean saveState = true;
|
||||
|
||||
// holds the list of items for writing before they are actually written on
|
||||
// #flush()
|
||||
private List<T> buffer = new ArrayList<T>();
|
||||
|
||||
private List<? extends T> headers = new ArrayList<T>();
|
||||
|
||||
public StaxEventItemWriter() {
|
||||
@@ -345,6 +338,8 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
writer.flush();
|
||||
|
||||
}
|
||||
|
||||
@@ -384,8 +379,8 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
|
||||
log.error(e);
|
||||
}
|
||||
|
||||
flush();
|
||||
try {
|
||||
delegateEventWriter.flush();
|
||||
endDocument(delegateEventWriter);
|
||||
eventWriter.close();
|
||||
channel.close();
|
||||
@@ -399,15 +394,25 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the value object to internal buffer.
|
||||
* Write the value objects and flush them to the file.
|
||||
*
|
||||
* @param item the value object
|
||||
* @see #flush()
|
||||
* @param items the value object
|
||||
*/
|
||||
public void write(List<? extends T> item) {
|
||||
public void write(List<? extends T> items) {
|
||||
|
||||
currentRecordCount+=item.size();
|
||||
buffer.addAll(item);
|
||||
currentRecordCount+=items.size();
|
||||
|
||||
for (T item : items) {
|
||||
serializer.serializeObject(eventWriter, item);
|
||||
}
|
||||
try {
|
||||
eventWriter.flush();
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new FlushFailedException("Failed to flush the events", e);
|
||||
}
|
||||
|
||||
lastCommitPointPosition = getPosition();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -468,28 +473,12 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
|
||||
* Writes buffered items to XML stream and marks restore point.
|
||||
*/
|
||||
public void flush() throws FlushFailedException {
|
||||
|
||||
for (T item : buffer) {
|
||||
serializer.serializeObject(eventWriter, item);
|
||||
}
|
||||
try {
|
||||
eventWriter.flush();
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new FlushFailedException("Failed to flush the events", e);
|
||||
}
|
||||
buffer.clear();
|
||||
|
||||
lastCommitPointPosition = getPosition();
|
||||
lastCommitPointRecordCount = currentRecordCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the output buffer
|
||||
*/
|
||||
public void clear() throws ClearFailedException {
|
||||
currentRecordCount = lastCommitPointRecordCount;
|
||||
buffer.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user