ActiveMQ整合Spring入門用法解析
一.ActiveMQ整合Spring基礎(chǔ)
ActiveMQ和Spring的整合,其實(shí)是把a(bǔ)ctivemq的一些對(duì)象交給spring來管理,比如連接工廠,queue,top等等
二.依賴
除了activemq本身提供的jar包外,還需要兩個(gè)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相關(guān)的對(duì)象
》生產(chǎn)者需要配置的對(duì)象:activemq原生連接工廠,spring包裝過的工廠,JmsTemplate(生產(chǎn)者),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"> <!-- 設(shè)置activemq的連接工廠 --> <property name="targetConnectionFactory" ref="activemqConnectionFactory"></property> </bean> <!-- 配置生產(chǎn)者對(duì)象:jmstemplate --> <bean name="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <!-- 設(shè)置工廠 --> <!-- <constructor-arg name="connectionFactory" ref="connectionFactory"></constructor-arg> --> <property name="connectionFactory" ref="connectionFactory"></property> </bean> <!-- 配置queue對(duì)象 --> <bean name="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg name="name" value="spring-active-queue"></constructor-arg> </bean> <!-- 配置topic對(duì)象 --> <bean name="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg name="name" value="spring-active-topic"></constructor-arg> </bean>
測(cè)試代碼:
//測(cè)試生產(chǎn)者發(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);
//獲取目的對(duì)象
Destination queue = (Destination) ioc.getBean("queueDestination");
//發(fā)送消息,并設(shè)置目的對(duì)象和消息
jmsTemplate.send(queue,new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
TextMessage message = session.createTextMessage();
message.setText("hello spring activemq");
return message;
}
});
小結(jié):生產(chǎn)者是需要在特定的時(shí)間發(fā)送指定的消息內(nèi)容,因此是需要書寫代碼的
》消費(fèi)者需要配置的對(duì)象:activemq原生連接工廠,spring包裝過的工廠,監(jiān)聽容器(消費(fèi)者),監(jiān)聽器,queue,topic
》之前監(jiān)聽器我們是直接實(shí)現(xiàn)匿名內(nèi)部類的方式,但是在spring這就不能了,還是書寫一個(gè)實(shí)現(xiàn)類并繼承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"> <!-- 設(shè)置activemq的連接工廠 --> <property name="targetConnectionFactory" ref="activemqConnectionFactory"></property> </bean> <!-- 配置監(jiān)聽器 --> <bean name="myMessageListener" class="cn.e3mall.search.listener.MyMessageListener"></bean> <!-- 配置監(jiān)聽容器:消費(fèi)者 --> <bean name="messageListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <!-- 設(shè)置連接工廠 --> <property name="connectionFactory" ref="connectionFactory"></property> <!-- 設(shè)置目的對(duì)象 --> <property name="destination" ref="queueDestination"></property> <!-- 設(shè)置監(jiān)聽器 --> <property name="messageListener" ref="myMessageListener"></property> </bean> <!-- 配置queue對(duì)象 --> <bean name="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg name="name" value="spring-active-queue"></constructor-arg> </bean> <!-- 配置topic對(duì)象 --> <bean name="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg name="name" value="spring-active-topic"></constructor-arg> </bean>
測(cè)試代碼:
@Test
public void testListener() throws IOException {
ApplicationContext ioc = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");
System.in.read();
}
小結(jié):我們的測(cè)試代碼很簡(jiǎn)單,只要把容器啟動(dòng)了,監(jiān)聽器就會(huì)一致監(jiān)聽著。不需要寫任何代碼,監(jiān)聽器只要一直關(guān)注目的對(duì)象Destination是否有消息發(fā)送過來
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java反射機(jī)制詳解_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Java反射機(jī)制的相關(guān)資料,主要包括反射的概念、作用2017-06-06
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(28)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07
Java調(diào)用dll文件的實(shí)現(xiàn)解析
這篇文章主要介紹了Java調(diào)用dll文件的實(shí)現(xiàn)解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
關(guān)于Java中的dozer對(duì)象轉(zhuǎn)換問題
Dozer是Java?Bean到Java?Bean映射器,它以遞歸方式將數(shù)據(jù)從一個(gè)對(duì)象復(fù)制到另一個(gè)對(duì)象,這篇文章主要介紹了Java中的dozer對(duì)象轉(zhuǎn)換的操作方法,需要的朋友可以參考下2022-08-08
SpringBoot webSocket實(shí)現(xiàn)發(fā)送廣播、點(diǎn)對(duì)點(diǎn)消息和Android接收
這篇文章主要介紹了SpringBoot webSocket實(shí)現(xiàn)發(fā)送廣播、點(diǎn)對(duì)點(diǎn)消息和Android接收,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
SpringBoot傳遞單一參數(shù)時(shí)@RequestParam和@RequestBody的區(qū)別小結(jié)
用SpringBoot框架做項(xiàng)目時(shí),經(jīng)常需要前端給后端傳遞參數(shù),本文主要介紹了SpringBoot傳遞單一參數(shù)時(shí)@RequestParam和@RequestBody的區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下2023-08-08
JAVA重復(fù)調(diào)用接口導(dǎo)致數(shù)據(jù)不一致的問題解決
在使用JAVA進(jìn)行開發(fā)時(shí),我們經(jīng)常會(huì)遇到要調(diào)用接口來獲取數(shù)據(jù)的情況,本文主要介紹了JAVA重復(fù)調(diào)用接口導(dǎo)致數(shù)據(jù)不一致的問題解決,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01
java中xml進(jìn)行報(bào)文發(fā)送和解析操作
這篇文章主要介紹了java中xml進(jìn)行報(bào)文發(fā)送和解析操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10

