diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/rabbitmq/amqp/AmqpMessageProducer.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/rabbitmq/amqp/AmqpMessageProducer.java new file mode 100644 index 000000000..72e053f49 --- /dev/null +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/rabbitmq/amqp/AmqpMessageProducer.java @@ -0,0 +1,29 @@ +package org.springframework.batch.sample.rabbitmq.amqp; + +import org.springframework.amqp.core.AmqpTemplate; +import org.springframework.amqp.rabbit.core.RabbitTemplate; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + *

+ * Simple producer class that sends {@link String} messages to the configured queue to be processed. + *

+ */ +public class AmqpMessageProducer { + public static final int SEND_MESSAGE_COUNT = 10; + public static final String[] BEAN_CONFIG = { "classpath:/META-INF/spring/jobs/messaging/rabbitmq-beans.xml", + "classpath:/META-INF/spring/config-beans.xml" }; + + public static void main(String[] args) { + ApplicationContext applicationContext = new ClassPathXmlApplicationContext(BEAN_CONFIG); + AmqpTemplate amqpTemplate = applicationContext.getBean("inboundAmqpTemplate", RabbitTemplate.class); + + for (int i = 0; i < SEND_MESSAGE_COUNT; i++ ) { + amqpTemplate.convertAndSend("foo message: " + i); + } + + ((ConfigurableApplicationContext) applicationContext).close(); + } +} diff --git a/spring-batch-samples/src/main/java/org/springframework/batch/sample/rabbitmq/processor/MessageProcessor.java b/spring-batch-samples/src/main/java/org/springframework/batch/sample/rabbitmq/processor/MessageProcessor.java new file mode 100644 index 000000000..e527a2973 --- /dev/null +++ b/spring-batch-samples/src/main/java/org/springframework/batch/sample/rabbitmq/processor/MessageProcessor.java @@ -0,0 +1,17 @@ +package org.springframework.batch.sample.rabbitmq.processor; + +import org.springframework.batch.item.ItemProcessor; + +import java.util.Date; + +/** + *

+ * Simple {@link ItemProcessor} implementation to append a "processed on" {@link Date} to a received message. + *

+ */ +public class MessageProcessor implements ItemProcessor { + + public String process(String message) throws Exception { + return "Message: \"" + message + "\" processed on: " + new Date(); + } +} diff --git a/spring-batch-samples/src/main/resources/META-INF/spring/batch-beans.xml b/spring-batch-samples/src/main/resources/META-INF/spring/batch-beans.xml new file mode 100644 index 000000000..dedbab573 --- /dev/null +++ b/spring-batch-samples/src/main/resources/META-INF/spring/batch-beans.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/spring-batch-samples/src/main/resources/META-INF/spring/config-beans.xml b/spring-batch-samples/src/main/resources/META-INF/spring/config-beans.xml new file mode 100644 index 000000000..f5dc575de --- /dev/null +++ b/spring-batch-samples/src/main/resources/META-INF/spring/config-beans.xml @@ -0,0 +1,13 @@ + + + + + + + diff --git a/spring-batch-samples/src/main/resources/META-INF/spring/jobs/amqp/amqp-example-job-beans.xml b/spring-batch-samples/src/main/resources/META-INF/spring/jobs/amqp/amqp-example-job-beans.xml new file mode 100644 index 000000000..8d77e3866 --- /dev/null +++ b/spring-batch-samples/src/main/resources/META-INF/spring/jobs/amqp/amqp-example-job-beans.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + diff --git a/spring-batch-samples/src/main/resources/META-INF/spring/jobs/amqp/amqp-example-job.xml b/spring-batch-samples/src/main/resources/META-INF/spring/jobs/amqp/amqp-example-job.xml new file mode 100644 index 000000000..2e74b98eb --- /dev/null +++ b/spring-batch-samples/src/main/resources/META-INF/spring/jobs/amqp/amqp-example-job.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + diff --git a/spring-batch-samples/src/main/resources/META-INF/spring/jobs/messaging/rabbitmq-beans.xml b/spring-batch-samples/src/main/resources/META-INF/spring/jobs/messaging/rabbitmq-beans.xml new file mode 100644 index 000000000..4bca5f143 --- /dev/null +++ b/spring-batch-samples/src/main/resources/META-INF/spring/jobs/messaging/rabbitmq-beans.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + diff --git a/spring-batch-samples/src/main/resources/default.amqp.properties b/spring-batch-samples/src/main/resources/default.amqp.properties new file mode 100644 index 000000000..9394facee --- /dev/null +++ b/spring-batch-samples/src/main/resources/default.amqp.properties @@ -0,0 +1,4 @@ +rabbitmq.port=5672 +rabbitmq.host=127.0.0.1 +rabbitmq.inbound.queue=test.inbound +rabbitmq.outbound.queue=test.outbound diff --git a/spring-batch-samples/src/main/resources/jobs/amqp-example-job.xml b/spring-batch-samples/src/main/resources/jobs/amqp-example-job.xml new file mode 100644 index 000000000..3c5b7ffdc --- /dev/null +++ b/spring-batch-samples/src/main/resources/jobs/amqp-example-job.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + diff --git a/spring-batch-samples/src/test/java/org/springframework/batch/sample/AMQPJobFunctionalTests.java b/spring-batch-samples/src/test/java/org/springframework/batch/sample/AMQPJobFunctionalTests.java new file mode 100644 index 000000000..773a8eeb1 --- /dev/null +++ b/spring-batch-samples/src/test/java/org/springframework/batch/sample/AMQPJobFunctionalTests.java @@ -0,0 +1,49 @@ +package org.springframework.batch.sample; + +import static org.junit.Assert.assertTrue; + +import javax.sql.DataSource; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.batch.core.explore.JobExplorer; +import org.springframework.batch.test.JobLauncherTestUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * Run the job to read from the "test.inbound" queue, process the messages and write them to the "test.outbound" queue: + * mvn -q exec:java -Dexec.mainClass="org.springframework.batch.core.launch.support.CommandLineJobRunner" \ + * -Dexec.arguments="classpath*:/META-INF/spring/jobs/amqp/amqp-example-job.xml,amqp-example-job" +*/ + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/jobs/amqp-example-job.xml", "/job-runner-context.xml" }) +public class AMQPJobFunctionalTests { + + @Autowired + private JobLauncherTestUtils jobLauncherTestUtils; + private SimpleJdbcTemplate simpleJdbcTemplate; + @Autowired + private JobExplorer jobExplorer; + + @Autowired + public void setDataSource(DataSource dataSource) { + this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource); + } + + + @Test + public void testLaunchJob() throws Exception { + + jobLauncherTestUtils.launchJob(); + + int count = jobExplorer.getJobInstances("amqp-example-job", 0, 1).size(); + + assertTrue(count > 0); + + } + +}