OPEN - BATCH-34: Support for multiple I/O files in a single jobRun for a particular scheduleDate.
tested multi-resource XML output
This commit is contained in:
@@ -46,6 +46,8 @@ public class MultiResourceItemWriter<T> extends ExecutionContextUserSupport impl
|
||||
|
||||
private ResourceSuffixCreator suffixCreator = new SimpleResourceSuffixCreator();
|
||||
|
||||
private boolean saveState = true;
|
||||
|
||||
public MultiResourceItemWriter() {
|
||||
setName(ClassUtils.getShortName(MultiResourceItemWriter.class));
|
||||
}
|
||||
@@ -55,7 +57,7 @@ public class MultiResourceItemWriter<T> extends ExecutionContextUserSupport impl
|
||||
delegate.close(new ExecutionContext());
|
||||
resourceIndex++;
|
||||
currentResourceItemCount = 0;
|
||||
pointDelegateToNextResource();
|
||||
setResourceToDelegate();
|
||||
delegate.open(new ExecutionContext());
|
||||
}
|
||||
delegate.write(items);
|
||||
@@ -88,12 +90,17 @@ public class MultiResourceItemWriter<T> extends ExecutionContextUserSupport impl
|
||||
/**
|
||||
* Prototype for output resources. Actual output files will be created in
|
||||
* the same directory and use the same name as this prototype with appended
|
||||
* suffix (according to {@link #setResourceSuffixCreator(ResourceSuffixCreator)}.
|
||||
* suffix (according to
|
||||
* {@link #setResourceSuffixCreator(ResourceSuffixCreator)}.
|
||||
*/
|
||||
public void setResource(Resource resource) {
|
||||
this.resource = resource;
|
||||
}
|
||||
|
||||
public void setSaveState(boolean saveState) {
|
||||
this.saveState = saveState;
|
||||
}
|
||||
|
||||
public void close(ExecutionContext executionContext) throws ItemStreamException {
|
||||
resourceIndex = 1;
|
||||
currentResourceItemCount = 0;
|
||||
@@ -103,9 +110,9 @@ public class MultiResourceItemWriter<T> extends ExecutionContextUserSupport impl
|
||||
public void open(ExecutionContext executionContext) throws ItemStreamException {
|
||||
resourceIndex = executionContext.getInt(getKey(RESOURCE_INDEX_KEY), 1);
|
||||
currentResourceItemCount = executionContext.getInt(getKey(CURRENT_RESOURCE_ITEM_COUNT), 0);
|
||||
|
||||
|
||||
try {
|
||||
pointDelegateToNextResource();
|
||||
setResourceToDelegate();
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new ItemStreamException("Couldn't open resource", e);
|
||||
@@ -114,15 +121,17 @@ public class MultiResourceItemWriter<T> extends ExecutionContextUserSupport impl
|
||||
}
|
||||
|
||||
public void update(ExecutionContext executionContext) throws ItemStreamException {
|
||||
delegate.update(executionContext);
|
||||
executionContext.putInt(getKey(CURRENT_RESOURCE_ITEM_COUNT), currentResourceItemCount);
|
||||
executionContext.putInt(getKey(RESOURCE_INDEX_KEY), resourceIndex);
|
||||
if (saveState) {
|
||||
delegate.update(executionContext);
|
||||
executionContext.putInt(getKey(CURRENT_RESOURCE_ITEM_COUNT), currentResourceItemCount);
|
||||
executionContext.putInt(getKey(RESOURCE_INDEX_KEY), resourceIndex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create next output resource and point the delegate to it.
|
||||
* Create output resource (if necessary) and point the delegate to it.
|
||||
*/
|
||||
private void pointDelegateToNextResource() throws IOException {
|
||||
private void setResourceToDelegate() throws IOException {
|
||||
String path = resource.getFile().getAbsolutePath() + suffixCreator.getSuffix(resourceIndex);
|
||||
File file = new File(path);
|
||||
file.createNewFile();
|
||||
|
||||
@@ -87,9 +87,6 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
|
||||
// root element attributes
|
||||
private Map<String, String> rootElementAttributes = null;
|
||||
|
||||
// signalizes that marshalling was restarted
|
||||
private boolean restarted = false;
|
||||
|
||||
// TRUE means, that output file will be overwritten if exists - default is
|
||||
// TRUE
|
||||
private boolean overwriteOutput = true;
|
||||
@@ -255,6 +252,7 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
|
||||
Assert.notNull(resource, "The resource must be set");
|
||||
|
||||
long startAtPosition = 0;
|
||||
boolean restarted = false;
|
||||
|
||||
// if restart data is provided, restart from provided offset
|
||||
// otherwise start from beginning
|
||||
@@ -263,7 +261,7 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
|
||||
restarted = true;
|
||||
}
|
||||
|
||||
open(startAtPosition);
|
||||
open(startAtPosition, restarted);
|
||||
|
||||
if (startAtPosition == 0) {
|
||||
try {
|
||||
@@ -281,7 +279,7 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
|
||||
/**
|
||||
* Helper method for opening output source at given file position
|
||||
*/
|
||||
private void open(long position) {
|
||||
private void open(long position, boolean restarted) {
|
||||
|
||||
File file;
|
||||
FileOutputStream os = null;
|
||||
@@ -433,12 +431,7 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
|
||||
|
||||
currentRecordCount += items.size();
|
||||
|
||||
doWrite(items);
|
||||
|
||||
}
|
||||
|
||||
private void doWrite(List<?> objects) throws XmlMappingException, IOException {
|
||||
for (Object object : objects) {
|
||||
for (Object object : items) {
|
||||
Assert.state(marshaller.supports(object.getClass()),
|
||||
"Marshaller must support the class of the marshalled object");
|
||||
marshaller.marshal(object, new StaxResult(eventWriter));
|
||||
@@ -449,6 +442,7 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
|
||||
catch (XMLStreamException e) {
|
||||
throw new FlushFailedException("Failed to flush the events", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user