av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

ActiveMQ整合Spring入門用法解析

瀏覽:2日期:2023-08-05 15:34:13

一.ActiveMQ整合Spring基礎

ActiveMQ和Spring的整合,其實是把activemq的一些對象交給spring來管理,比如連接工廠,queue,top等等

二.依賴

除了activemq本身提供的jar包外,還需要兩個spring整合activemq的jar:

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> <!-- activemq --> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> </dependency>

三.在spring配置文件中,配置activemq相關的對象

》生產者需要配置的對象:activemq原生連接工廠,spring包裝過的工廠,JmsTemplate(生產者),queue,topic

<!-- 配置activemq的連接工廠 --><bean name='activemqConnectionFactory' class='org.apache.activemq.ActiveMQConnectionFactory'> <constructor-arg index='0' value='tcp://192.168.xx.xxx:61616'></constructor-arg></bean><!-- 配置spring包裝的連接工廠 --><bean name='connectionFactory' class='org.springframework.jms.connection.SingleConnectionFactory'> <!-- 設置activemq的連接工廠 --> <property name='targetConnectionFactory' ref='activemqConnectionFactory'></property></bean><!-- 配置生產者對象:jmstemplate --><bean name='jmsTemplate' class='org.springframework.jms.core.JmsTemplate'> <!-- 設置工廠 --> <!-- <constructor-arg name='connectionFactory' ref='connectionFactory'></constructor-arg> --> <property name='connectionFactory' ref='connectionFactory'></property></bean><!-- 配置queue對象 --><bean name='queueDestination' class='org.apache.activemq.command.ActiveMQQueue'> <constructor-arg name='name' value='spring-active-queue'></constructor-arg></bean><!-- 配置topic對象 --><bean name='topicDestination' class='org.apache.activemq.command.ActiveMQTopic'> <constructor-arg name='name' value='spring-active-topic'></constructor-arg></bean>

測試代碼:

//測試生產者發(fā)生queue @Test public void testJMSTemplateByQueue() throws Exception {//創(chuàng)建spring容器 ApplicationContext ioc = new ClassPathXmlApplicationContext('classpath:spring/applicationContext-activemq.xml');//獲取jmstemplate JmsTemplate jmsTemplate = ioc.getBean(JmsTemplate.class);//獲取目的對象 Destination queue = (Destination) ioc.getBean('queueDestination');//發(fā)送消息,并設置目的對象和消息 jmsTemplate.send(queue,new MessageCreator() { public Message createMessage(Session session) throws JMSException {TextMessage message = session.createTextMessage();message.setText('hello spring activemq');return message; } });

小結:生產者是需要在特定的時間發(fā)送指定的消息內容,因此是需要書寫代碼的

》消費者需要配置的對象:activemq原生連接工廠,spring包裝過的工廠,監(jiān)聽容器(消費者),監(jiān)聽器,queue,topic

》之前監(jiān)聽器我們是直接實現匿名內部類的方式,但是在spring這就不能了,還是書寫一個實現類并繼承messageListener接口

<!-- 配置activemq的連接工廠 --><bean name='activemqConnectionFactory' class='org.apache.activemq.ActiveMQConnectionFactory'> <constructor-arg index='0' value='tcp://192.168.xx.xxx:61616'></constructor-arg></bean><!-- 配置spring包裝的連接工廠 --><bean name='connectionFactory' class='org.springframework.jms.connection.SingleConnectionFactory'> <!-- 設置activemq的連接工廠 --> <property name='targetConnectionFactory' ref='activemqConnectionFactory'></property></bean><!-- 配置監(jiān)聽器 --><bean name='myMessageListener' class='cn.e3mall.search.listener.MyMessageListener'></bean><!-- 配置監(jiān)聽容器:消費者 --><bean name='messageListenerContainer' class='org.springframework.jms.listener.DefaultMessageListenerContainer'> <!-- 設置連接工廠 --> <property name='connectionFactory' ref='connectionFactory'></property> <!-- 設置目的對象 --> <property name='destination' ref='queueDestination'></property> <!-- 設置監(jiān)聽器 --> <property name='messageListener' ref='myMessageListener'></property></bean><!-- 配置queue對象 --><bean name='queueDestination' class='org.apache.activemq.command.ActiveMQQueue'> <constructor-arg name='name' value='spring-active-queue'></constructor-arg></bean><!-- 配置topic對象 --><bean name='topicDestination' class='org.apache.activemq.command.ActiveMQTopic'> <constructor-arg name='name' value='spring-active-topic'></constructor-arg></bean>

測試代碼:

@Test public void testListener() throws IOException { ApplicationContext ioc = new ClassPathXmlApplicationContext('classpath:spring/applicationContext-activemq.xml'); System.in.read(); }

小結:我們的測試代碼很簡單,只要把容器啟動了,監(jiān)聽器就會一致監(jiān)聽著。不需要寫任何代碼,監(jiān)聽器只要一直關注目的對象Destination是否有消息發(fā)送過來

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 欧美v免费| 国产精品日产欧美久久久久 | 欧美福利在线 | 成人亚洲视频 | 亚洲精品乱码久久久久久按摩观 | 一级中国毛片 | 精品久久一 | 中国美女撒尿txxxxx视频 | 黄片毛片免费看 | 国产情品 | 久久精品日 | 国产成人91视频 | 999国产视频 | 日韩一区二区三区四区五区六区 | 蜜臀网| 性一交一乱一伦视频免费观看 | 亚洲欧美激情精品一区二区 | 日韩视频免费在线 | 久久精品亚洲精品国产欧美kt∨ | 天天操天天射综合网 | 国产精品婷婷 | 久久国产精品一区二区三区 | 亚洲一二三区av | 在线免费看91 | 中文字幕在线观看www | 91就要激情| 一级看片 | 日本高清中文字幕 | 天天弄天天操 | 日韩网站在线 | 中文字幕人成人 | 91一区二区三区在线观看 | 久久一级 | 亚洲国产一区二区三区 | 精品一二三区 | 国产真实精品久久二三区 | 久久久久久久久久久久亚洲 | 中文字幕在线观看 | 亚洲成在线观看 | 日韩av成人在线 | 男女一区二区三区 |