And now commit with the files that have been added for the sample.

This commit is contained in:
wxlund
2012-12-18 10:40:31 -08:00
committed by Michael Minella
parent bfb406a1cf
commit 96f870af4f
10 changed files with 237 additions and 0 deletions

View File

@@ -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;
/**
* <p>
* Simple producer class that sends {@link String} messages to the configured queue to be processed.
* </p>
*/
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();
}
}

View File

@@ -0,0 +1,17 @@
package org.springframework.batch.sample.rabbitmq.processor;
import org.springframework.batch.item.ItemProcessor;
import java.util.Date;
/**
* <p>
* Simple {@link ItemProcessor} implementation to append a "processed on" {@link Date} to a received message.
* </p>
*/
public class MessageProcessor implements ItemProcessor<String, String> {
public String process(String message) throws Exception {
return "Message: \"" + message + "\" processed on: " + new Date();
}
}

View File

@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- Default batch context. Uses HSQL as the datasource and initalizes it. -->
<context:property-placeholder location="classpath:default.properties" />
<jdbc:initialize-database data-source="dataSource">
<jdbc:script location="${batch.schema.script}" />
</jdbc:initialize-database>
<batch:job-repository id="jobRepository" />
<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
p:driverClassName="${batch.jdbc.driver}"
p:url="${batch.jdbc.url}"
p:username="${batch.jdbc.user}"
p:password="${batch.jdbc.password}"/>
<beans:bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource"/>
<beans:bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"
p:jobRepository-ref="jobRepository"/>
</beans:beans>

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- Property values used in the application -->
<context:property-placeholder location="classpath:default.amqp.properties" />
</beans:beans>

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
xmlns:beans="http://www.springframework.org/schema/beans"
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-3.1.xsd">
<beans:bean id="amqpItemReader" class="org.springframework.batch.item.amqp.AmqpItemReader">
<beans:constructor-arg ref="inboundAmqpTemplate"/>
</beans:bean>
<beans:bean id="amqpItemProcessor" class="org.springframework.batch.sample.rabbitmq.processor.MessageProcessor"/>
<beans:bean id="amqpItemWriter" class="org.springframework.batch.item.amqp.AmqpItemWriter">
<beans:constructor-arg ref="outboundAmqpTemplate"/>
</beans:bean>
</beans:beans>

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:batch="http://www.springframework.org/schema/batch"
xsi:schemaLocation="
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<beans:import resource="classpath:/META-INF/spring/config-beans.xml"/>
<!-- <beans:import resource="classpath:/META-INF/spring/batch-beans.xml"/> -->
<beans:import resource="classpath:/META-INF/spring/jobs/messaging/rabbitmq-beans.xml"/>
<beans:import resource="classpath:/META-INF/spring/jobs/amqp/amqp-example-job-beans.xml"/>
<batch:job id="amqp-example-job">
<batch:step id="processQueue">
<batch:tasklet>
<batch:chunk reader="amqpItemReader" processor="amqpItemProcessor" writer="amqpItemWriter" commit-interval="10"/>
</batch:tasklet>
</batch:step>
</batch:job>
</beans:beans>

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<rabbit:connection-factory id="rabbitConnectionFactory" port="${rabbitmq.port}" host="${rabbitmq.host}"/>
<rabbit:admin connection-factory="rabbitConnectionFactory"/>
<rabbit:queue name="${rabbitmq.inbound.queue}"/>
<rabbit:queue name="${rabbitmq.outbound.queue}"/>
<beans:bean id="inboundAmqpTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate"
p:connectionFactory-ref="rabbitConnectionFactory" p:routingKey="${rabbitmq.inbound.queue}"
p:queue="${rabbitmq.inbound.queue}"/>
<beans:bean id="outboundAmqpTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate"
p:connectionFactory-ref="rabbitConnectionFactory" p:routingKey="${rabbitmq.outbound.queue}"/>
</beans:beans>

View File

@@ -0,0 +1,4 @@
rabbitmq.port=5672
rabbitmq.host=127.0.0.1
rabbitmq.inbound.queue=test.inbound
rabbitmq.outbound.queue=test.outbound

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:batch="http://www.springframework.org/schema/batch"
xsi:schemaLocation="
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<beans:import resource="classpath:/META-INF/spring/config-beans.xml"/>
<!-- <beans:import resource="classpath:/META-INF/spring/batch-beans.xml"/> -->
<beans:import resource="classpath:/META-INF/spring/jobs/messaging/rabbitmq-beans.xml"/>
<beans:import resource="classpath:/META-INF/spring/jobs/amqp/amqp-example-job-beans.xml"/>
<batch:job id="amqp-example-job">
<batch:step id="processQueue">
<batch:tasklet>
<batch:chunk reader="amqpItemReader" processor="amqpItemProcessor" writer="amqpItemWriter" commit-interval="10"/>
</batch:tasklet>
</batch:step>
</batch:job>
</beans:beans>

View File

@@ -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);
}
}