ActiveMQ与spring整合
生活随笔
收集整理的這篇文章主要介紹了
ActiveMQ与spring整合
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
1 生產(chǎn)者
第一步:引用相關(guān)的jar包。
<dependency> <groupId>org.springframework</groupId><artifactId>spring-jms</artifactId> </dependency> <dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId> </dependency> <!-- 消息隊(duì)列 --> <dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-all</artifactId> </dependency>第二步:配置Activemq整合spring。配置ConnectionFactory applicationContext-activemq.xml
第三步:配置生產(chǎn)者。
使用JMSTemplate對象。發(fā)送消息。
第四步:在spring容器中配置Destination。
<?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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 真正可以產(chǎn)生Connection的ConnectionFactory,由對應(yīng)的 JMS服務(wù)廠商提供 --><bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><property name="brokerURL" value="tcp://192.168.36.40:61616" /></bean><!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --><bean id="connectionFactory"class="org.springframework.jms.connection.SingleConnectionFactory"><!-- 目標(biāo)ConnectionFactory對應(yīng)真實(shí)的可以產(chǎn)生JMS Connection的ConnectionFactory --><property name="targetConnectionFactory" ref="targetConnectionFactory" /></bean><!-- 配置生產(chǎn)者 --><!-- Spring提供的JMS工具類,它可以進(jìn)行消息發(fā)送、接收等 --><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><!-- 這個connectionFactory對應(yīng)的是我們定義的Spring提供的那個ConnectionFactory對象 --><property name="connectionFactory" ref="connectionFactory" /></bean><!--這個是隊(duì)列目的地,點(diǎn)對點(diǎn)的 --><bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg><value>spring-queue</value></constructor-arg></bean><!--這個是主題目的地,一對多的 --><bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"><constructor-arg value="topic" /></bean></beans>第五步:代碼測試
package com.shi.page;import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator;/*** * @author: SHF* @date: 2018年3月16日 下午1:39:20* @Description: spring 整合activemq測試*/ public class springActiveMqTest {@Testpublic void testpublichMQ()throws Exception{//1 初始化spring容器ApplicationContext application=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");//2 從spring容器中獲得jsmTemplate對象JmsTemplate jmsTemplate=application.getBean(JmsTemplate.class);//3 從spring容器中獲取Distination對象Destination distination=(Destination) application.getBean("queueDestination");//4 使用jmsTempate對象發(fā)送消息jmsTemplate.send(distination,new MessageCreator() {@Overridepublic Message createMessage(Session session) throws JMSException {// 創(chuàng)建一個消息對象并返回return session.createTextMessage("spring active queue22222");}});} }2 消費(fèi)者
2.2.2. 接收消息
e3-search-Service中接收消息。
第一步:把Activemq相關(guān)的jar包添加到工程中
第二步:創(chuàng)建一個MessageListener的實(shí)現(xiàn)類。
package com.shi.search.activemq;import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage;/*** * @author: SHF* @date: 2018年3月16日 下午2:33:05* @Description: 消息隊(duì)列監(jiān)聽對象,消費(fèi)者。用來接受消息*/ public class MyMessageListener implements MessageListener {@Overridepublic void onMessage(Message paramMessage) {// 監(jiān)聽中 處理接收到的消息TextMessage textMessage=(TextMessage) paramMessage;try {String text = textMessage.getText();System.out.println(text);} catch (JMSException e) {e.printStackTrace();}}}第三步:配置spring和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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 真正可以產(chǎn)生Connection的ConnectionFactory,由對應(yīng)的 JMS服務(wù)廠商提供 --><bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><property name="brokerURL" value="tcp://192.168.36.40:61616" /></bean><!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --><bean id="connectionFactory"class="org.springframework.jms.connection.SingleConnectionFactory"><!-- 目標(biāo)ConnectionFactory對應(yīng)真實(shí)的可以產(chǎn)生JMS Connection的ConnectionFactory --><property name="targetConnectionFactory" ref="targetConnectionFactory" /></bean><!--這個是隊(duì)列目的地,點(diǎn)對點(diǎn)的 --><bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg><value>spring-queue</value></constructor-arg></bean><!--這個是主題目的地,一對多的 --><bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"><constructor-arg value="topic" /></bean><!-- 自己配置的監(jiān)聽器 實(shí)現(xiàn)了MessageListener接口 --><bean id="myMessageListener" class="com.shi.search.activemq.MyMessageListener"></bean><!-- 消息監(jiān)聽容器 --><bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="connectionFactory" /><property name="destination" ref="queueDestination" /><property name="messageListener" ref="myMessageListener" /></bean></beans>第四步:測試代碼。
package com.shi.solr;import java.io.IOException;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;/*** * @author: SHF* @date: 2018年3月16日 下午2:45:16* @Description: ActiveMQ消費(fèi)者測試*/ public class ActiveMQCusomerTest {@Testpublic void customerTest() throws IOException{//只需要初始化spring容器,系統(tǒng)會幫我們監(jiān)聽的ApplicationContext application=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");//等待System.in.read();} }轉(zhuǎn)載于:https://my.oschina.net/u/3677987/blog/1635889
總結(jié)
以上是生活随笔為你收集整理的ActiveMQ与spring整合的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python之新式类与经典类
- 下一篇: javaweb学习中的路径问题