BATCH-919: reuse ChunkProcessor in integration

This commit is contained in:
dsyer
2008-12-30 16:32:29 +00:00
parent 7d7480578d
commit a6479602e2
18 changed files with 166 additions and 200 deletions

View File

@@ -12,8 +12,11 @@ import org.junit.runner.RunWith;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.job.SimpleJob;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
@@ -24,6 +27,7 @@ import org.springframework.batch.core.repository.dao.MapJobExecutionDao;
import org.springframework.batch.core.repository.dao.MapJobInstanceDao;
import org.springframework.batch.core.repository.dao.MapStepExecutionDao;
import org.springframework.batch.core.repository.support.SimpleJobRepository;
import org.springframework.batch.core.step.item.Chunk;
import org.springframework.batch.core.step.item.SimpleStepFactoryBean;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.item.support.ListItemReader;
@@ -69,7 +73,7 @@ public class ChunkMessageItemWriterIntegrationTests {
factory.setBeanName("step");
factory.setItemWriter(writer);
factory.setCommitInterval(4);
SimpleMessagingGateway gateway = new SimpleMessagingGateway();
writer.setMessagingGateway(gateway);
@@ -85,7 +89,7 @@ public class ChunkMessageItemWriterIntegrationTests {
System.err.println(message);
message = replies.receive(10);
}
}
@After
@@ -186,7 +190,10 @@ public class ChunkMessageItemWriterIntegrationTests {
*/
@SuppressWarnings("unchecked")
private GenericMessage<ChunkRequest> getSimpleMessage(String string, Long jobId) {
ChunkRequest chunk = new ChunkRequest(StringUtils.commaDelimitedListToSet(string), jobId, 0);
StepContribution stepContribution = new JobExecution(new JobInstance(0L, new JobParameters(), "job"), 1L)
.createStepExecution("step").createStepContribution();
ChunkRequest chunk = new ChunkRequest(new Chunk<String>(StringUtils.commaDelimitedListToSet(string)), jobId,
stepContribution);
GenericMessage<ChunkRequest> message = new GenericMessage<ChunkRequest>(chunk);
return message;
}

View File

@@ -3,9 +3,13 @@ package org.springframework.batch.integration.chunk;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Collection;
import org.junit.Test;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobInstance;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.step.item.Chunk;
import org.springframework.batch.core.step.item.ChunkProcessor;
import org.springframework.util.StringUtils;
public class ChunkProcessorChunkHandlerTests {
@@ -17,15 +21,15 @@ public class ChunkProcessorChunkHandlerTests {
@Test
public void testVanillaHandleChunk() {
handler.setChunkProcessor(new ChunkProcessor<Object>() {
public int process(Collection<? extends Object> items, int skipCount) throws Exception {
count += items.size();
return 0;
public void process(StepContribution contribution, Chunk<Object> chunk) throws Exception {
count += chunk.size();
}
});
StepContribution stepContribution = new JobExecution(new JobInstance(0L, new JobParameters(), "job"), 1L).createStepExecution("step").createStepContribution();
@SuppressWarnings("unchecked")
ChunkResponse response = handler.handleChunk(new ChunkRequest<Object>(StringUtils
.commaDelimitedListToSet("foo,bar"), 12L, 10));
assertEquals(0, response.getSkipCount());
ChunkResponse response = handler.handleChunk(new ChunkRequest<Object>(new Chunk<Object>(StringUtils
.commaDelimitedListToSet("foo,bar")), 12L, stepContribution));
assertEquals(stepContribution, response.getStepContribution());
assertEquals(12, response.getJobId().longValue());
assertTrue(response.isSuccessful());
assertEquals(2, count);

View File

@@ -30,7 +30,7 @@ public class RetryRepeatTransactionalPollingIntegrationTests implements Applicat
private Log logger = LogFactory.getLog(getClass());
private static List<String> list = new ArrayList<String>();
private volatile static List<String> list = new ArrayList<String>();
@Autowired
private SimpleRecoverer recoverer;
@@ -38,10 +38,10 @@ public class RetryRepeatTransactionalPollingIntegrationTests implements Applicat
@Autowired
private SimpleService service;
private Lifecycle bus;
private Lifecycle lifecycle;
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
bus = (Lifecycle) applicationContext;
lifecycle = (Lifecycle) applicationContext;
}
private static volatile int count = 0;
@@ -73,7 +73,7 @@ public class RetryRepeatTransactionalPollingIntegrationTests implements Applicat
List<String> expected = TransactionAwareProxyFactory.createTransactionalList(Arrays.asList(StringUtils
.commaDelimitedListToStringArray("a,b,c,d")));
service.setExpected(expected);
waitForResults(bus, expected.size(), 60);
waitForResults(lifecycle, expected.size(), 60);
assertEquals(4,service.getProcessed().size()); // a,b,c,d
assertEquals(expected, service.getProcessed());
}
@@ -86,8 +86,8 @@ public class RetryRepeatTransactionalPollingIntegrationTests implements Applicat
List<String> expected = TransactionAwareProxyFactory.createTransactionalList(Arrays.asList(StringUtils
.commaDelimitedListToStringArray("a,b,fail,fail,d,e,f")));
service.setExpected(expected);
waitForResults(bus, expected.size(), 60);
waitForResults(bus, 6, 100); // (a,b), (fail), (fail), ([fail],d), (e,f)
waitForResults(lifecycle, expected.size(), 60); // (a,b), (fail), (fail), ([fail],d), (e,f)
System.err.println(service.getProcessed());
assertEquals(7,service.getProcessed().size()); // a,b,fail,fail,d,e,f
assertEquals(1,recoverer.getRecovered().size()); // fail
assertEquals(expected, service.getProcessed());

View File

@@ -69,7 +69,9 @@ public class TransactionalPollingIntegrationTests implements ApplicationContextA
}
public void output(String message) {
handled.add(message);
if (count < expected.size()) {
handled.add(message);
}
logger.debug("Handled: " + message);
}

View File

@@ -19,7 +19,7 @@
<tx:annotation-driven />
<beans:bean id="chunkHandler" class="org.springframework.batch.integration.chunk.ChunkProcessorChunkHandler">
<beans:property name="chunkProcessor">
<beans:bean class="org.springframework.batch.integration.chunk.SimpleChunkProcessor">
<beans:bean class="org.springframework.batch.core.step.item.SimpleChunkProcessor">
<beans:property name="itemWriter">
<beans:bean class="org.springframework.batch.integration.chunk.TestItemWriter" />
</beans:property>

View File

@@ -20,7 +20,7 @@
<integration:inbound-channel-adapter
ref="testCase" method="input" channel="requests">
<integration:poller max-messages-per-poll="1">
<integration:interval-trigger interval="10" />
<integration:interval-trigger interval="10" initial-delay="100"/>
<integration:advice-chain>
<ref bean="txAdvice"/>
<ref bean="repeatAdvice"/>

View File

@@ -1,7 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:integration="http://www.springframework.org/schema/integration"
xmlns:context="http://www.springframework.org/schema/context" 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"
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:integration="http://www.springframework.org/schema/integration"
xmlns:context="http://www.springframework.org/schema/context"
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.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
@@ -18,10 +20,16 @@
<integration:inbound-channel-adapter
ref="testCase" method="input" channel="requests">
<integration:poller max-messages-per-poll="1">
<integration:interval-trigger interval="10" />
<!--
TODO: the initial delay is a hack - it usually prevents the poller
from picking up data in the list before it is cleared, but one day
100ms will not be enough... (see INT-536)
-->
<integration:interval-trigger interval="10"
initial-delay="100" />
<integration:advice-chain>
<ref bean="txAdvice"/>
<ref bean="repeatAdvice"/>
<ref bean="txAdvice" />
<ref bean="repeatAdvice" />
</integration:advice-chain>
</integration:poller>
</integration:inbound-channel-adapter>
@@ -39,32 +47,44 @@
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<bean id="repeatAdvice" class="org.springframework.batch.repeat.interceptor.RepeatOperationsInterceptor">
<bean id="repeatAdvice"
class="org.springframework.batch.repeat.interceptor.RepeatOperationsInterceptor">
<property name="repeatOperations">
<bean class="org.springframework.batch.repeat.support.RepeatTemplate">
<property name="completionPolicy">
<bean class="org.springframework.batch.repeat.policy.SimpleCompletionPolicy">
<bean
class="org.springframework.batch.repeat.policy.SimpleCompletionPolicy">
<property name="chunkSize" value="2" />
</bean>
</property>
</bean>
</property>
</bean>
<bean id="service" class="org.springframework.batch.integration.retry.SimpleService" />
<bean id="recoverer" class="org.springframework.batch.integration.retry.SimpleRecoverer" />
<bean id="retryAdvice" class="org.springframework.batch.retry.interceptor.StatefulRetryOperationsInterceptor">
<bean id="service"
class="org.springframework.batch.integration.retry.SimpleService" />
<bean id="recoverer"
class="org.springframework.batch.integration.retry.SimpleRecoverer" />
<bean id="retryAdvice"
class="org.springframework.batch.retry.interceptor.StatefulRetryOperationsInterceptor">
<property name="retryOperations">
<bean class="org.springframework.batch.retry.support.RetryTemplate">
<property name="retryPolicy">
<bean class="org.springframework.batch.retry.policy.SimpleRetryPolicy">
<property name="maxAttempts" value="2"/>
<property name="maxAttempts" value="2" />
</bean>
</property>
</bean>
</property>
<property name="recoverer" ref="recoverer" />
</bean>
<aop:config proxy-target-class="true">
<aop:advisor advice-ref="retryAdvice" pointcut="execution(* org.springframework.batch.integration.retry.Service+.process(..))" />
<aop:advisor advice-ref="retryAdvice"
pointcut="execution(* org.springframework.batch.integration.retry.Service+.process(..))" />
</aop:config>
</beans>

View File

@@ -18,7 +18,7 @@
<integration:inbound-channel-adapter
ref="testCase" method="input" channel="requests">
<integration:poller max-messages-per-poll="1">
<integration:interval-trigger interval="10" />
<integration:interval-trigger interval="10" initial-delay="100" />
<integration:transactional />
</integration:poller>
</integration:inbound-channel-adapter>

View File

@@ -20,7 +20,7 @@
<integration:inbound-channel-adapter
ref="testCase" method="input" channel="requests">
<integration:poller max-messages-per-poll="1">
<integration:interval-trigger interval="10" />
<integration:interval-trigger interval="10" initial-delay="100"/>
<integration:transactional />
</integration:poller>
</integration:inbound-channel-adapter>