RESOLVED - issue BATCH-165: delegating ItemProvider and ItemProcessor
http://opensource.atlassian.com/projects/spring/browse/BATCH-165 Patch applied from JIRA.
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* 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.sample.domain;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.batch.item.ItemProcessor;
|
||||
import org.springframework.batch.item.ItemProvider;
|
||||
|
||||
|
||||
/**
|
||||
* Custom class that contains logic that would normally be
|
||||
* be contained in {@link ItemProvider} (<code>getData()</code>) and
|
||||
* {@link ItemProcessor} (<code>processData(..)</code>).
|
||||
*
|
||||
* @author tomas.slanina
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
public class PersonService {
|
||||
|
||||
private static final int GENERATION_LIMIT = 10;
|
||||
|
||||
private int generatedCounter = 0;
|
||||
private int processedCounter = 0;
|
||||
|
||||
public Person getData() {
|
||||
if (generatedCounter >= GENERATION_LIMIT) return null;
|
||||
|
||||
Person person = new Person();
|
||||
Address address = new Address();
|
||||
Child child = new Child();
|
||||
List children = new ArrayList(1);
|
||||
|
||||
children.add(child);
|
||||
|
||||
person.setFirstName("John" + generatedCounter);
|
||||
person.setAge(20 + generatedCounter);
|
||||
address.setCity("Johnsville" + generatedCounter);
|
||||
child.setName("Little Johny" + generatedCounter);
|
||||
|
||||
person.setAddress(address);
|
||||
person.setChildren(children);
|
||||
|
||||
generatedCounter++;
|
||||
|
||||
return person;
|
||||
}
|
||||
|
||||
/**
|
||||
* Badly designed method signature which accepts multiple implicitly related
|
||||
* arguments instead of a single Person argument.
|
||||
*/
|
||||
public void processPerson(String name, String city) {
|
||||
processedCounter++;
|
||||
}
|
||||
|
||||
public int getReturnedCount() {
|
||||
return generatedCounter;
|
||||
}
|
||||
|
||||
public int getReceivedCount() {
|
||||
return processedCounter;
|
||||
}
|
||||
}
|
||||
52
samples/src/main/resources/jobs/delegatingJob.xml
Normal file
52
samples/src/main/resources/jobs/delegatingJob.xml
Normal file
@@ -0,0 +1,52 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
|
||||
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
|
||||
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
|
||||
|
||||
<description>
|
||||
The intent is to to give an example of how existing bean
|
||||
definitions (e.g. from custom application's domain layer)
|
||||
can be integrated into a batch job.
|
||||
</description>
|
||||
|
||||
<bean class="org.springframework.batch.execution.configuration.JobConfigurationRegistryBeanPostProcessor">
|
||||
<property name="jobConfigurationRegistry" ref="jobConfigurationRegistry"/>
|
||||
</bean>
|
||||
|
||||
<bean id="jobConfiguration" parent="simpleJob">
|
||||
<property name="steps">
|
||||
<bean id="step1" parent="simpleStep">
|
||||
<property name="tasklet">
|
||||
<bean
|
||||
class="org.springframework.batch.execution.tasklet.ItemProviderProcessTasklet">
|
||||
<property name="itemProvider">
|
||||
<bean class="org.springframework.batch.item.provider.DelegatingItemProvider">
|
||||
<property name="targetObject" ref="delegatingObject" />
|
||||
<property name="targetMethod" value="getData" />
|
||||
</bean>
|
||||
</property>
|
||||
<property name="itemProcessor">
|
||||
<bean class="org.springframework.batch.item.processor.PropertyExtractingDelegatingItemProcessor">
|
||||
<property name="targetObject" ref="delegatingObject" />
|
||||
<property name="targetMethod" value="processPerson" />
|
||||
<property name="fieldsUsedAsTargetMethodArguments">
|
||||
<list>
|
||||
<value>firstName</value>
|
||||
<value>address.city</value>
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
<bean id="delegatingObject" class="org.springframework.batch.sample.domain.PersonService" />
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.springframework.batch.sample;
|
||||
|
||||
import org.springframework.batch.sample.domain.PersonService;
|
||||
|
||||
public class DelegatingJobFunctionalTests extends AbstractValidatingBatchLauncherTests {
|
||||
|
||||
private PersonService personService;
|
||||
|
||||
protected String[] getConfigLocations() {
|
||||
return new String[] {"jobs/delegatingJob.xml"};
|
||||
}
|
||||
|
||||
protected void validatePostConditions() throws Exception {
|
||||
assertTrue(personService.getReturnedCount() > 0);
|
||||
assertEquals(personService.getReturnedCount(), personService.getReceivedCount());
|
||||
|
||||
}
|
||||
|
||||
// setter for auto-injection
|
||||
public void setPersonService(PersonService personService) {
|
||||
this.personService = personService;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user