IN PROGRESS - BATCH-34: Support for multiple I/O files in a single jobRun for a particular scheduleDate.

customizable resource suffixes
This commit is contained in:
robokaso
2008-11-10 11:45:07 +00:00
parent fe42d53fc5
commit fdbf216dc7
5 changed files with 86 additions and 12 deletions

View File

@@ -17,7 +17,8 @@ import org.springframework.util.ClassUtils;
/**
* Wraps a {@link ResourceAwareItemWriterItemStream} and creates a new output
* resource when the count of items written in current resource exceeds
* {@link #setItemCountLimitPerResource(int)}.
* {@link #setItemCountLimitPerResource(int)}. Suffix creation can be customized
* with {@link #setResourceSuffixCreator(ResourceSuffixCreator)}.
*
* Note that new resources are created only at chunk boundaries i.e. the number
* of items written into one resource is between the limit set by
@@ -25,8 +26,6 @@ import org.springframework.util.ClassUtils;
*
* @param <T> item type
*
* TODO strategise naming of the created resources
*
* @author Robert Kasanicky
*/
public class MultiResourceItemWriter<T> extends ExecutionContextUserSupport implements ItemWriter<T>, ItemStream {
@@ -45,6 +44,8 @@ public class MultiResourceItemWriter<T> extends ExecutionContextUserSupport impl
private int resourceIndex = 1;
private ResourceSuffixCreator suffixCreator = new SimpleResourceSuffixCreator();
public MultiResourceItemWriter() {
setName(ClassUtils.getShortName(MultiResourceItemWriter.class));
}
@@ -61,14 +62,34 @@ public class MultiResourceItemWriter<T> extends ExecutionContextUserSupport impl
currentResourceItemCount += items.size();
}
/**
* Allows customization of the suffix of the created resources based on the
* index.
*/
public void setResourceSuffixCreator(ResourceSuffixCreator suffixCreator) {
this.suffixCreator = suffixCreator;
}
/**
* After this limit is exceeded the next chunk will be written into newly
* created resource.
*/
public void setItemCountLimitPerResource(int itemCountLimitPerResource) {
this.itemCountLimitPerResource = itemCountLimitPerResource;
}
/**
* Delegate used for actual writing of the output.
*/
public void setDelegate(ResourceAwareItemWriterItemStream<? super T> delegate) {
this.delegate = delegate;
}
/**
* 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)}.
*/
public void setResource(Resource resource) {
this.resource = resource;
}
@@ -81,8 +102,8 @@ public class MultiResourceItemWriter<T> extends ExecutionContextUserSupport impl
public void open(ExecutionContext executionContext) throws ItemStreamException {
resourceIndex = Long.valueOf(executionContext.getLong(getKey(RESOURCE_INDEX_KEY), 1L)).intValue();
currentResourceItemCount = Long.valueOf(
executionContext.getLong(getKey(CURRENT_RESOURCE_ITEM_COUNT), 0L)).intValue();
currentResourceItemCount = Long.valueOf(executionContext.getLong(getKey(CURRENT_RESOURCE_ITEM_COUNT), 0L))
.intValue();
try {
pointDelegateToNextResource();
}
@@ -102,7 +123,7 @@ public class MultiResourceItemWriter<T> extends ExecutionContextUserSupport impl
* Create next output resource and point the delegate to it.
*/
private void pointDelegateToNextResource() throws IOException {
String path = resource.getFile().getAbsolutePath() + "." + resourceIndex;
String path = resource.getFile().getAbsolutePath() + suffixCreator.getSuffix(resourceIndex);
File file = new File(path);
file.createNewFile();
Assert.state(file.canWrite(), "Output resource " + path + " must be writable");

View File

@@ -0,0 +1,15 @@
package org.springframework.batch.item.file;
/**
* Strategy interface for translating resource index into unique filename
* suffix.
*
* @see MultiResourceItemWriter
* @see SimpleResourceSuffixCreator
*
* @author Robert Kasanicky
*/
public interface ResourceSuffixCreator {
String getSuffix(int index);
}

View File

@@ -0,0 +1,15 @@
package org.springframework.batch.item.file;
/**
* Trivial implementation of {@link ResourceSuffixCreator} that uses the index
* itself as suffix, separated by dot.
*
* @author Robert Kasanicky
*/
public class SimpleResourceSuffixCreator implements ResourceSuffixCreator {
public String getSuffix(int index) {
return "." + index;
}
}