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;
}
}

View File

@@ -21,6 +21,8 @@ public class MultiResourceItemWriterTests {
private MultiResourceItemWriter<String> tested = new MultiResourceItemWriter<String>();
private File file;
private ResourceSuffixCreator suffixCreator = new SimpleResourceSuffixCreator();
private ResourceAwareItemWriterItemStream<String> delegate = new FlatFileItemWriter<String>() {
{
@@ -35,6 +37,7 @@ public class MultiResourceItemWriterTests {
file = File.createTempFile(MultiResourceItemWriterTests.class.getSimpleName(), null);
tested.setResource(new FileSystemResource(file));
tested.setDelegate(delegate);
tested.setResourceSuffixCreator(suffixCreator);
tested.setItemCountLimitPerResource(2);
tested.open(executionContext);
@@ -45,12 +48,12 @@ public class MultiResourceItemWriterTests {
tested.write(Arrays.asList("1", "2", "3"));
File part1 = new File(file.getAbsolutePath() + ".1");
File part1 = new File(file.getAbsolutePath() + suffixCreator.getSuffix(1));
assertTrue(part1.exists());
assertEquals("123", readFile(part1));
tested.write(Arrays.asList("4"));
File part2 = new File(file.getAbsolutePath() + ".2");
File part2 = new File(file.getAbsolutePath() + suffixCreator.getSuffix(2));
assertTrue(part2.exists());
assertEquals("4", readFile(part2));
@@ -58,7 +61,7 @@ public class MultiResourceItemWriterTests {
assertEquals("45", readFile(part2));
tested.write(Arrays.asList("6", "7", "8", "9"));
File part3 = new File(file.getAbsolutePath() + ".3");
File part3 = new File(file.getAbsolutePath() + suffixCreator.getSuffix(3));
assertTrue(part3.exists());
assertEquals("6789", readFile(part3));
}
@@ -67,12 +70,12 @@ public class MultiResourceItemWriterTests {
public void testRestart() throws Exception {
tested.write(Arrays.asList("1", "2", "3"));
File part1 = new File(file.getAbsolutePath() + ".1");
File part1 = new File(file.getAbsolutePath() + suffixCreator.getSuffix(1));
assertTrue(part1.exists());
assertEquals("123", readFile(part1));
tested.write(Arrays.asList("4"));
File part2 = new File(file.getAbsolutePath() + ".2");
File part2 = new File(file.getAbsolutePath() + suffixCreator.getSuffix(2));
assertTrue(part2.exists());
assertEquals("4", readFile(part2));
@@ -84,7 +87,7 @@ public class MultiResourceItemWriterTests {
assertEquals("45", readFile(part2));
tested.write(Arrays.asList("6", "7", "8", "9"));
File part3 = new File(file.getAbsolutePath() + ".3");
File part3 = new File(file.getAbsolutePath() + suffixCreator.getSuffix(3));
assertTrue(part3.exists());
assertEquals("6789", readFile(part3));
}

View File

@@ -0,0 +1,20 @@
package org.springframework.batch.item.file;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* Tests for {@link SimpleResourceSuffixCreator}.
*/
public class SimpleResourceSuffixCreatorTests {
private SimpleResourceSuffixCreator tested = new SimpleResourceSuffixCreator();
@Test
public void testGetSuffix() {
assertEquals("0", tested.getSuffix(0));
assertEquals("1", tested.getSuffix(1));
assertEquals("3463457", tested.getSuffix(3463457));
}
}