spring activeMq 配置
1 <!-- ActiveMQ 连接工厂 --> 2 <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供--> 3 <!-- 如果连接网络:tcp://ip:61616;未连接网络:tcp://localhost:61616 以及用户名,密码--> 4 <amq:connectionFactory id="amqConnectionFactory" 5 brokerURL="tcp://127.0.0.1:61616" userName="admin" password="admin" /> 6 7 <!-- spring caching连接工厂 --> 8 <!-- spring用于管理真正的Connectionfactory的connectionFactory --> 9 <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"> 10 <!-- 目标ConnectionFacory对应真实的可以产生JMS Connection的ConnectionFactory --> 11 <property name="targetConnectionFactory" ref="amqConnectionFactory"></property> 12 <!-- Session缓存数量 --> 13 <property name="sessionCacheSize" value="100"></property> 14 </bean> 15 16 <!-- Spring JmsTemplate的消息生产者 start --> 17 18 <!-- 定义JmsTemplate的Queue类型 --> 19 <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate"> 20 <!-- 这个connectionFactory对应的是我们定义的spring提供的那个ConnectionFactory对象 --> 21 <constructor-arg ref="connectionFactory"></constructor-arg> 22 <!-- 非pub/sub模型(发布/订阅),即队列模式 --> 23 <property name="pubSubDomain" value="false"></property> 24 </bean> 25 26 <!-- 定义JmsTemplate的Topic类型 --> 27 <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate"> 28 <constructor-arg ref="connectionFactory"></constructor-arg> 29 <!-- pub/sub模型(发布/订阅) --> 30 <property name="pubSubDomain" value="true"></property> 31 </bean> 32 33 <!-- Spring JmsTemplate 的消息生产者end--> 34 35 <!-- 消息消费者 start --> 36 37 <!-- 定义Queue监听器 --> 38 <jms:listener-container destination-type="queue" container-type="default" connection-factory="connectionFactory" acknowledge="auto"> 39 <jms:listener destination="test.queue" ref="queueReceiver1"></jms:listener> 40 <jms:listener destination="test.queue" ref="queueReceiver1"></jms:listener> 41 </jms:listener-container> 42 43 <!-- 定义Topic监听器 --> 44 <jms:listener-container destination-type="topic" container-type="default" connection-factory="connectionFactory" acknowledge="auto"> 45 <jms:listener destination="test.topic" ref="topicReceiver1"/> 46 <jms:listener destination="test.topic" ref="topicReceiver2"/> 47 </jms:listener-container> 48 49 <!-- 消息消费者end -->
消息生产者:
1 @Service 2 public class ProducerService { 3 4 private final Logger LOG = LoggerFactory.getLogger(ProducerService.class); 5 6 @Resource(name = "jmsTemplate") 7 private JmsTemplate jmsTemplate; 8 9 /** 10 * 向指定队列发送消息 11 */ 12 public void sendMessage(Destination destination, final String msg) { 13 System.out.println("向队列" + destination.toString() + "发送了消息------------" + msg); 14 jmsTemplate.send(destination, new MessageCreator() { 15 @Override 16 public Message createMessage(Session session) throws JMSException { 17 return session.createTextMessage(msg); 18 } 19 }); 20 } 21 22 }
消息消费者(不设置消息监听时):
1 @Service 2 public class ConsumerService { 3 4 private final Logger LOG = LoggerFactory.getLogger(ConsumerService.class); 5 6 @Resource(name="jmsTemplate") 7 private JmsTemplate jmsTemplate; 8 9 /** 10 * 接收消息 11 */ 12 public TextMessage receive(Destination destination) { 13 TextMessage tm = (TextMessage) jmsTemplate.receive(destination); 14 try { 15 System.out.println("从队列" + destination.toString() + "收到了消息: " 16 + tm.getText()); 17 } catch (JMSException e) { 18 e.printStackTrace(); 19 } 20 21 return tm; 22 23 } 24 }
java activeMq监听器:
1 package com.css.cn.consumer; 2 3 import javax.jms.JMSException; 4 import javax.jms.Message; 5 import javax.jms.MessageListener; 6 import javax.jms.TextMessage; 7 8 import org.springframework.beans.factory.annotation.Autowired; 9 import org.springframework.stereotype.Component; 10 11 import com.css.cn.dao.ActiveInsertDao; 12 13 /** 14 * 点对点消息队列监听器 15 * @ClassName: QueueReceiver1 16 * @author zyj 17 * @date 2016年11月30日 下午3:17:46 18 * 19 * @version V1.0 20 */ 21 @Component 22 public class QueueReceiver1 implements MessageListener{ 23 24 @Autowired 25 private ActiveInsertDao activeInsert; 26 27 @Override 28 public void onMessage(Message message) { 29 // TODO Auto-generated method stub 30 try { 31 String text = ((TextMessage) message).getText(); 32 System.out.println("点对点QueueReceiver1监听器"+text); 33 activeInsert.insertValue(text); 34 } catch (Exception e) { 35 // TODO Auto-generated catch block 36 e.printStackTrace(); 37 } 38 } 39 40 41 }