Spring下集成ActiveMQ推送
本文是将ActiveMQ消息制造者集成进spring,通过spring后台推送消息的实现。
首先是spring的applicationContext的配置,如下
<?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:amq="http://activemq.apache.org/schema/core" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"> <!-- 使@autowired注解生效 --> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> <!-- 配置自动扫描Commpent的位置 --> <context:component-scan base-package="com.test.maven.service" /> <!-- 配置目标连接工厂,配置与ActiveMQ的连接 --> <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <!-- ActiveMq连接地址 --> <property name="brokerURL" value="tcp://localhost:61616"/> <property name="useAsyncSend" value="true"/> </bean> <!-- 配置连接工厂 --> <bean id="cachingConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"> <!-- 缓存数量 --> <property name="sessionCacheSize" value="10"/> <property name="targetConnectionFactory" ref="targetConnectionFactory"/> </bean> <!-- 发送消息的目的地(订阅模式) --> <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"> <!-- 设置消息主题的名字 --> <constructor-arg index="0" value="cmbc.CmbcPushTopic" /> </bean> <!-- 发送消息的目的地(P2P模式) --> <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg index="0" value="cmbc.cmbcPushQueue"/> </bean> <!-- 配置JMS模板 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="cachingConnectionFactory" /> <property name="defaultDestination" ref="topicDestination" /> <!-- 订阅发布模式 --> <property name="pubSubDomain" value="true" /> <property name="receiveTimeout" value="10000" /> </bean> </beans>
配置后,建立一个service层和service实现层,把推送服务进行封装。
package com.test.maven.service; import javax.jms.Destination; public interface ActiveMQProducerService { public void sendMsg(Destination destination, final String msg); }
下边是具体实现:
package com.test.maven.service.impl; import javax.annotation.Resource; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session; import javax.jms.Topic; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; import org.springframework.stereotype.Component; import com.test.maven.service.ActiveMQProducerService; @Component public class ActiveMQProducerServiceImpl implements ActiveMQProducerService { @Resource(name = "jmsTemplate") private JmsTemplate jmsTemplate; public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } public void sendMsg(Destination destination, final String msg) { jmsTemplate.send(destination, new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createTextMessage(msg); } }); System.out.println("push String:" + msg); try { System.out.println("destination:" + ((Topic) destination).getTopicName()); } catch (JMSException e) { e.printStackTrace(); } } }
下边附上使用例子:
package com.test.maven.controller; import javax.jms.Destination; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.test.maven.service.ActiveMQProducerService; @Controller public class SayHello { // @AutoWired 和 @Resource 功能类似,autowired是按type在bean中检索,resoure // 若指定name则按照name搜,指定type就按照类型搜,两个都指定则唯一匹配 @Autowired private ActiveMQProducerService activeMQProducerService; // 当Autowired检索时不唯一,则需要Qualifier进行筛选 @Autowired @Qualifier("topicDestination") private Destination destination; @RequestMapping(value = "/push") public void testPushService(HttpServletRequest requset, HttpServletResponse response) { String msg = requset.getParameter("msg"); activeMQProducerService.sendMsg(destination, msg); } }
过几天我再进行详细研究,想把ActiveMQ直接嵌入到spring中,这样就不用再开启ActiveMQ了。
每天不用多,进步一点点就够了