标签:
1 src/main/resources/ 2 |---- jndi.properties 3 |---- spring-beans.xml 4 src/main/java/ 5 |---- cn.sinobest.asj.producer.springsupport.simple 6 |---- SimpleProducer.java # 发送测试类 7 pom.xml
1 java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory 2 3 # use the following property to configure the default connector 4 java.naming.provider.url=tcp://localhost:61616 5 6 # register some queues in JNDI using the form 7 # queue.[jndiName] = [physicalName] 8 queue.exampleQueue=example.queue 9 10 # register some topics in JNDI using the form 11 # topic.[jndiName] = [physicalName] 12 topic.exampleTopic=example.topic
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context-4.2.xsd"> 9 <!-- import outer properties file --> 10 <context:property-placeholder location="classpath:jndi.properties" /> 11 12 <!-- create pooled connection factory --> 13 <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"> 14 <property name="connectionFactory"> 15 <bean class="org.apache.activemq.ActiveMQConnectionFactory"> 16 <property name="brokerURL" value="${java.naming.provider.url}" /> 17 <property name="closeTimeout" value="60000" /> 18 </bean> 19 </property> 20 </bean> 21 22 <!-- create queue destination --> 23 <bean id="exampleQueue" class="org.apache.activemq.command.ActiveMQQueue"> 24 <constructor-arg value="${queue.exampleQueue}" /> 25 </bean> 26 27 <!-- create topic destination --> 28 <bean id="exampleTopic" class="org.apache.activemq.command.ActiveMQTopic"> 29 <constructor-arg value="${topic.exampleTopic}" /> 30 </bean> 31 32 <!-- create template for send message --> 33 <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> 34 <!-- bind the connection factory --> 35 <property name="connectionFactory" ref="pooledConnectionFactory" /> 36 <!-- bind the default destination, but you can also appoint other destination when send --> 37 <property name="defaultDestination" ref="exampleQueue" /> 38 </bean> 39 </beans>
说明:
1 package cn.sinobest.asj.producer.springsupport.simple; 2 import javax.annotation.Resource; 3 import javax.jms.Destination; 4 import javax.jms.JMSException; 5 import javax.jms.Message; 6 import javax.jms.Session; 7 import javax.jms.TextMessage; 8 import org.junit.Test; 9 import org.junit.runner.RunWith; 10 import org.springframework.jms.core.JmsTemplate; 11 import org.springframework.jms.core.MessageCreator; 12 import org.springframework.test.context.ContextConfiguration; 13 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 14 /** 15 * 单元测试类.<br> 16 * 基于Spring运行环境的测试. 17 * @author lijinlong 18 * 19 */ 20 @RunWith(SpringJUnit4ClassRunner.class) // 配置spring组件运行时 21 @ContextConfiguration("/spring-beans.xml") // 配置文件 22 public class SimpleProducer { 23 @Resource(name="exampleTopic") 24 private Destination exampleTopic; 25 26 @Resource(name="jmsTemplate") 27 private JmsTemplate jmsTemplate; 28 29 @Test 30 public void test() { 31 // 发送到默认的destination - queue 32 jmsTemplate.send(new MessageCreator() { 33 34 public Message createMessage(Session session) throws JMSException { 35 TextMessage msg = session.createTextMessage(); 36 msg.setText("This message is plan to the queue."); 37 return msg; 38 } 39 }); 40 41 // 发送到指定的destination - topic 42 jmsTemplate.send(exampleTopic, new MessageCreator() { 43 44 public Message createMessage(Session session) throws JMSException { 45 TextMessage msg = session.createTextMessage(); 46 msg.setText("This message is plan to the topic."); 47 return msg; 48 } 49 }); 50 } 51 }
说明:
1 <!-- import org.apache.activemq.pool.PooledConnectionFactory using in spring-beans.xml --> 2 <dependency> 3 <groupId>org.apache.activemq</groupId> 4 <artifactId>activemq-pool</artifactId> 5 <version>5.13.2</version> 6 </dependency> 7 <!-- import for unit test --> 8 <dependency> 9 <groupId>junit</groupId> 10 <artifactId>junit</artifactId> 11 <version>4.12</version> 12 </dependency> 13 <!-- import spring support --> 14 <dependency> 15 <groupId>org.springframework</groupId> 16 <artifactId>spring-context</artifactId> 17 <version>4.2.4.RELEASE</version> 18 </dependency> 19 <dependency> 20 <groupId>org.springframework</groupId> 21 <artifactId>spring-jms</artifactId> 22 <version>4.2.4.RELEASE</version> 23 </dependency> 24 <dependency> 25 <groupId>org.springframework</groupId> 26 <artifactId>spring-test</artifactId> 27 <version>4.2.4.RELEASE</version> 28 </dependency>
1 src/main/resources/ 2 |---- jndi.properties 3 |---- spring-beans.xml 4 src/main/java/ 5 |---- cn.sinobest.asj.consumer.util 6 |---- Hold.java # 提供pause功能 7 |---- cn.sinobest.asj.consumer.springsupport.simple 8 |---- SimpleConsumer.java # 单元测试类 9 pom.xml
1 java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory 2 3 # use the following property to configure the default connector 4 java.naming.provider.url=tcp://localhost:61616 5 6 # register some queues in JNDI using the form 7 # queue.[jndiName] = [physicalName] 8 queue.exampleQueue=example.queue 9 10 # register some topics in JNDI using the form 11 # topic.[jndiName] = [physicalName] 12 topic.exampleTopic=example.topic
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:jms="http://www.springframework.org/schema/jms" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context-4.2.xsd 10 http://www.springframework.org/schema/jms 11 http://www.springframework.org/schema/jms/spring-jms-4.2.xsd"> 12 <!-- import outer properties file --> 13 <context:property-placeholder location="classpath:jndi.properties" /> 14 15 <!-- create pooled connection factory --> 16 <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"> 17 <property name="connectionFactory"> 18 <bean class="org.apache.activemq.ActiveMQConnectionFactory"> 19 <property name="brokerURL" value="${java.naming.provider.url}" /> 20 <property name="closeTimeout" value="60000" /> 21 </bean> 22 </property> 23 </bean> 24 25 <bean id="simpleConsumer" class="cn.sinobest.asj.consumer.springsupport.simple.SimpleConsumer" /> 26 27 <!-- config listener container for queues --> 28 <jms:listener-container connection-factory="pooledConnectionFactory"> 29 <jms:listener destination="${queue.exampleQueue}" ref="simpleConsumer" 30 method="onMessageFromQueue" /> 31 </jms:listener-container> 32 33 <!-- config listener container for topics --> 34 <jms:listener-container connection-factory="pooledConnectionFactory" 35 destination-type="topic"> 36 <jms:listener destination="${topic.exampleTopic}" ref="simpleConsumer" 37 method="onMessageFromTopic" /> 38 </jms:listener-container> 39 </beans>
1 <bean id="messageContainer" 2 class="org.springframework.jms.listener.DefaultMessageListenerContainer"> 3 <property name="connectionFactory" ref="connectionFactory" /> 4 <property name="destination" ref="destination" /> 5 <property name="messageListener" ref="messageListener" /> 6 </bean>
基于listener-container的配置 | 基于bean的配置 |
一个container可以配置多个listener | 一个container只能配置一个listener |
配置消息处理类来接收消息,且对这个类没有类型的要求。 | 用messageListener属性配置消息监听器,要求实现javax.jms.MessageListener接口。 |
对Message进行了预处理:根据接收到的Message的类型进行解封,根据解封的类型选择一个兼容的处理方法调用 | 没有对Message进行预处理 |
1 package cn.sinobest.asj.consumer.util; 2 /** 3 * 防止程序立即退出.<br> 4 * @author lijinlong 5 * 6 */ 7 public class Hold { 8 /** 9 * 防止程序立即退出,直到用户输入Enter键. 10 */ 11 public static void hold() { 12 @SuppressWarnings("resource") 13 java.util.Scanner scanner = new java.util.Scanner(System.in); 14 System.out.println("按Enter键退出:"); 15 scanner.nextLine(); 16 } 17 }
1 package cn.sinobest.asj.consumer.springsupport.simple; 2 import org.junit.Test; 3 import org.junit.runner.RunWith; 4 import org.springframework.test.context.ContextConfiguration; 5 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 6 import cn.sinobest.asj.consumer.util.Hold; 7 @RunWith(SpringJUnit4ClassRunner.class) 8 @ContextConfiguration("/spring-beans.xml") 9 public class SimpleConsumer { 10 @Test 11 public void test() { 12 Hold.hold(); 13 } 14 15 public void onMessageFromQueue(String message) { 16 System.out.println("从queue收到消息:" + message); 17 } 18 19 public void onMessageFromTopic(String message) { 20 System.out.println("从Topic收到消息:" + message); 21 } 22 }
1 <!-- import org.apache.activemq.pool.PooledConnectionFactory using in spring-beans.xml --> 2 <dependency> 3 <groupId>org.apache.activemq</groupId> 4 <artifactId>activemq-pool</artifactId> 5 <version>5.13.2</version> 6 </dependency> 7 <!-- import for unit test --> 8 <dependency> 9 <groupId>junit</groupId> 10 <artifactId>junit</artifactId> 11 <version>4.12</version> 12 </dependency> 13 <!-- import spring support --> 14 <dependency> 15 <groupId>org.springframework</groupId> 16 <artifactId>spring-context</artifactId> 17 <version>4.2.4.RELEASE</version> 18 </dependency> 19 <dependency> 20 <groupId>org.springframework</groupId> 21 <artifactId>spring-jms</artifactId> 22 <version>4.2.4.RELEASE</version> 23 </dependency> 24 <dependency> 25 <groupId>org.springframework</groupId> 26 <artifactId>spring-test</artifactId> 27 <version>4.2.4.RELEASE</version> 28 </dependency>
标签:
原文地址:http://www.cnblogs.com/ywjy/p/5330353.html