Added async item processor (could be moved into infrastructure at some point).
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
package org.springframework.batch.integration.async;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.item.ItemProcessor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.task.SimpleAsyncTaskExecutor;
|
||||
import org.springframework.integration.annotation.MessageEndpoint;
|
||||
import org.springframework.integration.annotation.ServiceActivator;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration
|
||||
public class AsyncItemProcessorMessagingGatewayTests {
|
||||
|
||||
private AsyncItemProcessor<String, String> processor = new AsyncItemProcessor<String, String>();
|
||||
|
||||
@Autowired
|
||||
private ItemProcessor<String, String> delegate;
|
||||
|
||||
@Test
|
||||
public void testMultiExecution() throws Exception {
|
||||
processor.setDelegate(delegate);
|
||||
processor.setTaskExecutor(new SimpleAsyncTaskExecutor());
|
||||
List<Future<String>> list = new ArrayList<Future<String>>();
|
||||
for (int count = 0; count < 10; count++) {
|
||||
list.add(processor.process("foo" + count));
|
||||
}
|
||||
for (Future<String> future : list) {
|
||||
String value = future.get();
|
||||
/**
|
||||
* TODO: this delegate is a Spring Integration MessagingGateway. It
|
||||
* can easily return null because of a timeout, but that will be
|
||||
* treated by Batch as a filtered item, whereas it is really more
|
||||
* like a skip. Maybe we should have an option to throw an exception
|
||||
* in the processor if an unexpected null value comes back?
|
||||
*/
|
||||
assertNotNull(value);
|
||||
assertTrue(value.matches("foo.*foo.*"));
|
||||
}
|
||||
}
|
||||
|
||||
@MessageEndpoint
|
||||
public static class Doubler {
|
||||
@ServiceActivator
|
||||
public String cat(String value) {
|
||||
return value + value;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package org.springframework.batch.integration.async;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.item.ItemProcessor;
|
||||
import org.springframework.core.task.SimpleAsyncTaskExecutor;
|
||||
|
||||
public class AsyncItemProcessorTests {
|
||||
|
||||
private AsyncItemProcessor<String, String> processor = new AsyncItemProcessor<String, String>();
|
||||
|
||||
private ItemProcessor<String, String> delegate = new ItemProcessor<String, String>() {
|
||||
public String process(String item) throws Exception {
|
||||
return item + item;
|
||||
};
|
||||
};
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testNoDelegate() throws Exception {
|
||||
processor.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecution() throws Exception {
|
||||
processor.setDelegate(delegate);
|
||||
Future<String> result = processor.process("foo");
|
||||
assertEquals("foofoo", result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultiExecution() throws Exception {
|
||||
processor.setDelegate(delegate);
|
||||
processor.setTaskExecutor(new SimpleAsyncTaskExecutor());
|
||||
List<Future<String>> list = new ArrayList<Future<String>>();
|
||||
for (int count = 0; count < 10; count++) {
|
||||
list.add(processor.process("foo" + count));
|
||||
}
|
||||
for (Future<String> future : list) {
|
||||
assertTrue(future.get().matches("foo.*foo.*"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans:beans xmlns="http://www.springframework.org/schema/integration"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
|
||||
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
|
||||
http://www.springframework.org/schema/integration
|
||||
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context-2.5.xsd
|
||||
http://www.springframework.org/schema/tx
|
||||
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
|
||||
<annotation-config />
|
||||
<channel id="requests" />
|
||||
<channel id="replies">
|
||||
<queue />
|
||||
</channel>
|
||||
<gateway service-interface="org.springframework.batch.item.ItemProcessor"
|
||||
id="processor" default-reply-timeout="1000" default-request-channel="requests"
|
||||
default-reply-channel="replies" />
|
||||
<service-activator input-channel="requests"
|
||||
output-channel="replies" ref="doubler" />
|
||||
<beans:bean id="doubler"
|
||||
class="org.springframework.batch.integration.async.AsyncItemProcessorMessagingGatewayTests$Doubler" />
|
||||
</beans:beans>
|
||||
@@ -23,13 +23,13 @@
|
||||
<integration:service-activator ref="stepExecutionRequestHandler"
|
||||
input-channel="requests" output-channel="staging">
|
||||
<integration:poller>
|
||||
<integration:interval-trigger interval="10"/>
|
||||
<integration:interval-trigger interval="10" />
|
||||
</integration:poller>
|
||||
</integration:service-activator>
|
||||
<integration:aggregator ref="partitionHandler"
|
||||
timeout="10000" input-channel="staging" output-channel="replies">
|
||||
<integration:poller>
|
||||
<integration:interval-trigger interval="10"/>
|
||||
<integration:interval-trigger interval="10" />
|
||||
</integration:poller>
|
||||
</integration:aggregator>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user