本文主要介绍spring+activemq的整合代码,源代码是spring+spring mvc+mybaits+activemq的完整版
本人读书少,不善言辞,所以老规则,直接上代码
首先是配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:amq="http://activemq.apache.org/schema/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jsm/spring-jms-4.1.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.14.1.xsd"> <context:component-scan base-package="com.dnkx.activemq" /> <amq:connectionFactory id="amqConnectionFactory" brokerURL="tcp://localhost:61616" userName="admin" password="admin" /> <!-- 配置JMS连接工厂 --> <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"> <constructor-arg ref="amqConnectionFactory" /> <property name="sessionCacheSize" value="100" /> </bean> <!-- 定义消息队列(Queue) --> <bean id="dnkxQueueDestination" class="org.apache.activemq.command.ActiveMQQueue"> <!-- 设置消息队列的名字 --> <constructor-arg> <value>dnkxmq</value> </constructor-arg> </bean> <!-- 配置JMS模板(Queue),Spring提供的JMS工具类,它发送、接收消息。 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory" /> <property name="defaultDestination" ref="dnkxQueueDestination" /> <property name="receiveTimeout" value="10000" /> <!-- true是topic,false是queue,默认是false,此处显示写出false --> <property name="pubSubDomain" value="false" /> </bean> <!-- 配置消息队列监听者(Queue) --> <bean id="queueMessageListener" class="com.dnkx.activemq.listener.QueueMessageListener" /> <!-- 显示注入消息监听容器(Queue),配置连接工厂,监听的目标是dnkxQueueDestination,监听器是上面定义的监听器 --> <bean id="queueListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="dnkxQueueDestination" /> <property name="messageListener" ref="queueMessageListener" /> </bean> </beans>
package com.dnkx.activemq; import javax.annotation.Resource; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; import org.springframework.stereotype.Service; /** * 生产者 * @author 李小拐 2016年11月12日 12:48:35 * */ @Service public class ProductService { @Resource(name="jmsTemplate") private JmsTemplate jmsTemplate; /** * 向指定队列发送消息 * @param destination * @param msg */ public void sendMessage(Destination destination,final String msg) { System.out.println("向队列--"+destination.toString()+"--发送消息--"+msg); jmsTemplate.send(destination, new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { return session.createTextMessage(msg); } }); } /** * 向默认队列发送消息 * @param msg */ public void sendMessage(final String msg) { System.out.println("向队列--"+jmsTemplate.getDefaultDestinationName().toString()+"--发送消息--"+msg); jmsTemplate.send(new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { return session.createTextMessage(msg); } }); } }
package com.dnkx.activemq; import javax.annotation.Resource; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.TextMessage; import org.springframework.jms.core.JmsTemplate; import org.springframework.stereotype.Service; /** * 消费者 * @author 李小拐 2016年11月12日 12:48:13 * */ @Service public class ConsumerService { @Resource(name="jmsTemplate") private JmsTemplate jmsTemplate; /** * 接收消息 * @param destination * @return */ public TextMessage receive(Destination destination) { TextMessage tm=(TextMessage)jmsTemplate.receive(destination); try { System.out.println("从队列--"+destination.toString()+"--接收到消息---"+tm.getText()); } catch (JMSException e) { e.printStackTrace(); } return tm; } }
package com.dnkx.activemq.listener; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; import org.apache.activemq.store.journal.JournalTransactionStore.Tx; /** * 消息监听器 * @author 李小拐 2016年11月12日 12:49:04 * */ public class QueueMessageListener implements MessageListener{ @Override public void onMessage(Message message) { TextMessage tm = (TextMessage) message; try { System.out.println("收到的消息-----"+tm.getText()); } catch (JMSException e) { e.printStackTrace(); } } }
package com.dnkx.controller; import javax.annotation.Resource; import javax.jms.Destination; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.dnkx.activemq.ProductService; import com.dnkx.pojo.Test; import com.dnkx.service.TestService; /** * 测试 * @author 李小拐 2016年11月12日 12:59:52 * */ @Controller @RequestMapping("/test") public class TestController { @Resource private TestService testService; //队列生产者 @Resource(name="productService") private ProductService product; //队列名称 @Resource(name="dnkxQueueDestination") private Destination destination; @RequestMapping("/test.do") public String test(HttpServletRequest request) { Test test=testService.getTestById(1); System.out.println("用户名:"+test.getName()+" 密码:"+test.getPassword()); request.setAttribute("name", test.getName()); return "main"; } @RequestMapping("/test2.do") public String test2(HttpServletRequest request) { return "datagrid"; } //这里是生产者生产消息的的地方 @RequestMapping("/test1.do") public String test1(HttpServletRequest request) { product.sendMessage(destination, "这是一条mq消息"); System.out.println("------发送成功-------------"); return "index"; } }
最后一句:仅供参考,仅供参考,仅供参考!!!
源码下载地址