BATCH-1103: Added partitioning sample

This commit is contained in:
dsyer
2009-02-25 12:31:13 +00:00
parent a07a7166fb
commit 044a91eddf
10 changed files with 461 additions and 2 deletions

View File

@@ -78,7 +78,7 @@ public class StepListenerFactoryBean implements FactoryBean, InitializingBean{
Set<Class<? extends StepListener>> listenerInterfaces = new HashSet<Class<? extends StepListener>>();
//For every entry in th emap, try and find a method by interface, name, or annotation. If the same
//For every entry in the map, try and find a method by interface, name, or annotation. If the same
for(Entry<String, String> entry : metaDataMap.entrySet()){
StepListenerMetaData metaData = StepListenerMetaData.fromPropertyName(entry.getKey());
Set<MethodInvoker> invokers = new NullIgnoringSet<MethodInvoker>();

View File

@@ -0,0 +1,89 @@
/*
* Copyright 2006-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.core.partition.support;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
/**
* Implementation of {@link Partitioner} that locates multiple resources and
* associates their file names with execution context keys. Creates an
* {@link ExecutionContext} per resource, and labels them as
* <code>{partition0, partition1, ..., partitionN}</code>. The grid size is
* ignored.
*
* @author Dave Syer
*
*/
public class MultiResourcePartitioner implements Partitioner {
private static final String DEFAULT_KEY_NAME = "fileName";
private static final String PARTITION_KEY = "partition";
private Resource[] resources = new Resource[0];
private String keyName = DEFAULT_KEY_NAME;
/**
* The resources to assign to each partition. In Spring configuration you
* can use a pattern to select multiple resources.
* @param resources the resources to use
*/
public void setResources(Resource[] resources) {
this.resources = resources;
}
/**
* The name of the key for the file name in each {@link ExecutionContext}.
* Defaults to "fileName".
* @param keyName the value of the key
*/
public void setKeyName(String keyName) {
this.keyName = keyName;
}
/**
* Assign the filename of each of the injected resources to an
* {@link ExecutionContext}.
*
* @see Partitioner#partition(int)
*/
public Map<String, ExecutionContext> partition(int gridSize) {
Map<String, ExecutionContext> map = new HashMap<String, ExecutionContext>(gridSize);
int i = 0;
for (Resource resource : resources) {
ExecutionContext context = new ExecutionContext();
Assert.state(resource.exists(), "Resource does not exist: "+resource);
try {
context.putString(keyName, resource.getURL().toExternalForm());
}
catch (IOException e) {
throw new IllegalArgumentException("File could not be located for: "+resource, e);
}
map.put(PARTITION_KEY + i, context);
i++;
}
return map;
}
}

View File

@@ -0,0 +1,54 @@
package org.springframework.batch.core.partition.support;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.core.io.support.ResourceArrayPropertyEditor;
public class MultiResourcePartitionerTests {
private MultiResourcePartitioner partitioner = new MultiResourcePartitioner();
@Before
public void setUp() {
ResourceArrayPropertyEditor editor = new ResourceArrayPropertyEditor();
editor.setAsText("classpath:log4j*");
partitioner.setResources((Resource[]) editor.getValue());
}
@Test(expected = IllegalStateException.class)
public void testMissingResource() {
partitioner.setResources(new Resource[] { new FileSystemResource("does-not-exist") });
partitioner.partition(0);
}
@Test
public void testPartitionSizeAndKey() {
Map<String, ExecutionContext> partition = partitioner.partition(0);
assertEquals(1, partition.size());
assertTrue(partition.containsKey("partition0"));
}
@Test
public void testReadFile() throws Exception {
Map<String, ExecutionContext> partition = partitioner.partition(0);
String url = partition.get("partition0").getString("fileName");
assertTrue(new UrlResource(url).exists());
}
@Test
public void testSetKeyName() {
partitioner.setKeyName("foo");
Map<String, ExecutionContext> partition = partitioner.partition(0);
assertTrue(partition.get("partition0").containsKey("foo"));
}
}