詳解spring boot整合JMS(ActiveMQ實(shí)現(xiàn))
本文介紹了spring boot整合JMS(ActiveMQ實(shí)現(xiàn)),分享給大家,也給自己留個(gè)學(xué)習(xí)筆記。
一、安裝ActiveMQ
具體的安裝步驟,請(qǐng)參考我的另一篇文章:http://www.dbjr.com.cn/article/127117.htm
二、新建spring boot工程,并加入JMS(ActiveMQ)依賴
三、工程結(jié)構(gòu)
pom依賴如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.chhliu.springboot.jms</groupId> <artifactId>springboot-jms</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot-jms</name> <description>Demo project for Spring Boot Jms</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
四、修改application.properties配置文件
## URL of the ActiveMQ broker. Auto-generated by default. For instance `tcp://localhost:61616` # failover:(tcp://localhost:61616,tcp://localhost:61617) # tcp://localhost:61616 spring.activemq.broker-url=tcp://localhost:61616 spring.activemq.in-memory=true spring.activemq.pool.enabled=false //如果此處設(shè)置為true,需要加如下的依賴包,否則會(huì)自動(dòng)配置失敗,報(bào)JmsMessagingTemplate注入失敗
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> <!-- <version>5.7.0</version> --> </dependency>
五、消息生產(chǎn)者
package com.chhliu.springboot.jms; import javax.jms.Destination; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsMessagingTemplate; import org.springframework.stereotype.Service; @Service("producer") public class Producer { @Autowired // 也可以注入JmsTemplate,JmsMessagingTemplate對(duì)JmsTemplate進(jìn)行了封裝 private JmsMessagingTemplate jmsTemplate; // 發(fā)送消息,destination是發(fā)送到的隊(duì)列,message是待發(fā)送的消息 public void sendMessage(Destination destination, final String message){ jmsTemplate.convertAndSend(destination, message); } }
六、消息消費(fèi)者
package com.chhliu.springboot.jms; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; @Component public class Consumer { // 使用JmsListener配置消費(fèi)者監(jiān)聽(tīng)的隊(duì)列,其中text是接收到的消息 @JmsListener(destination = "mytest.queue") public void receiveQueue(String text) { System.out.println("Consumer收到的報(bào)文為:"+text); } }
消費(fèi)者2的代碼同上,注意,消息消費(fèi)者的類上必須加上@Component,或者是@Service,這樣的話,消息消費(fèi)者類就會(huì)被委派給Listener類,原理類似于使用SessionAwareMessageListener以及MessageListenerAdapter來(lái)實(shí)現(xiàn)消息驅(qū)動(dòng)POJO
七、測(cè)試
package com.chhliu.springboot.jms; import javax.jms.Destination; import org.apache.activemq.command.ActiveMQQueue; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class SpringbootJmsApplicationTests { @Autowired private Producer producer; @Test public void contextLoads() throws InterruptedException { Destination destination = new ActiveMQQueue("mytest.queue"); for(int i=0; i<100; i++){ producer.sendMessage(destination, "myname is chhliu!!!"); } } }
測(cè)試結(jié)果如下:
Consumer2收到的報(bào)文為:myname is chhliu!!! Consumer收到的報(bào)文為:myname is chhliu!!! Consumer2收到的報(bào)文為:myname is chhliu!!! Consumer收到的報(bào)文為:myname is chhliu!!! Consumer2收到的報(bào)文為:myname is chhliu!!! Consumer收到的報(bào)文為:myname is chhliu!!! Consumer2收到的報(bào)文為:myname is chhliu!!! Consumer收到的報(bào)文為:myname is chhliu!!! Consumer2收到的報(bào)文為:myname is chhliu!!! Consumer收到的報(bào)文為:myname is chhliu!!! Consumer2收到的報(bào)文為:myname is chhliu!!! Consumer收到的報(bào)文為:myname is chhliu!!! Consumer2收到的報(bào)文為:myname is chhliu!!!
經(jīng)過(guò)上面的幾個(gè)步驟,spring boot和Jms就基本上整合完成了,是不是使用起來(lái)很方便了!
八、實(shí)現(xiàn)雙向隊(duì)列
1、下面首先來(lái)對(duì)Consumer2這個(gè)消費(fèi)者來(lái)進(jìn)行下改造,代碼如下:
package com.chhliu.springboot.jms; import org.springframework.jms.annotation.JmsListener; import org.springframework.messaging.handler.annotation.SendTo; import org.springframework.stereotype.Component; @Component public class Consumer2 { @JmsListener(destination = "mytest.queue") @SendTo("out.queue") public String receiveQueue(String text) { System.out.println("Consumer2收到的報(bào)文為:"+text); return "return message"+text; } }
從上面的代碼可以看出,我們?cè)趓eceiveQueue方法上面多加了一個(gè)注解@SendTo("out.queue"),該注解的意思是將return回的值,再發(fā)送的"out.queue"隊(duì)列中,下面我們?cè)賮?lái)跑一下前面的測(cè)試,在監(jiān)控頁(yè)面中,我們發(fā)現(xiàn),"out.queue"隊(duì)列中已經(jīng)有內(nèi)容了,如下:
進(jìn)入Browse界面觀看:
最后看下收到的具體信息:
我們發(fā)現(xiàn),該隊(duì)列中的消息,就是我們返回的值!
九、對(duì)Producer進(jìn)行改造
通過(guò)上面的示例,我們現(xiàn)在對(duì)Producer進(jìn)行改造,使其既能生產(chǎn)報(bào)文,又能消費(fèi)隊(duì)列中的報(bào)文,代碼如下:
package com.chhliu.springboot.jms; import javax.jms.Destination; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.annotation.JmsListener; import org.springframework.jms.core.JmsMessagingTemplate; import org.springframework.stereotype.Service; @Service("producer") public class Producer { @Autowired private JmsMessagingTemplate jmsTemplate; public void sendMessage(Destination destination, final String message){ jmsTemplate.convertAndSend(destination, message); } @JmsListener(destination="out.queue") public void consumerMessage(String text){ System.out.println("從out.queue隊(duì)列收到的回復(fù)報(bào)文為:"+text); } }
測(cè)試結(jié)果如下:
從out.queue隊(duì)列收到的回復(fù)報(bào)文為:return messagemyname is chhliu!!! Consumer收到的報(bào)文為:myname is chhliu!!! Consumer2收到的報(bào)文為:myname is chhliu!!! 從out.queue隊(duì)列收到的回復(fù)報(bào)文為:return messagemyname is chhliu!!! Consumer收到的報(bào)文為:myname is chhliu!!! Consumer2收到的報(bào)文為:myname is chhliu!!! 從out.queue隊(duì)列收到的回復(fù)報(bào)文為:return messagemyname is chhliu!!! Consumer收到的報(bào)文為:myname is chhliu!!! Consumer2收到的報(bào)文為:myname is chhliu!!! 從out.queue隊(duì)列收到的回復(fù)報(bào)文為:return messagemyname is chhliu!!!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 基于SpringBoot中activeMq的JmsTemplate的實(shí)例
- SpringBoot集成JmsTemplate(隊(duì)列模式和主題模式)及xml和JavaConfig配置詳解
- SpringBoot 整合 JMSTemplate的示例代碼
- Spring Boot基于Active MQ實(shí)現(xiàn)整合JMS
- Spring整合Weblogic jms實(shí)例詳解
- spring整合JMS實(shí)現(xiàn)同步收發(fā)消息(基于ActiveMQ的實(shí)現(xiàn))
- Spring-boot JMS 發(fā)送消息慢的解決方法
- Spring Jms 模塊案例講解
相關(guān)文章
Spring Cloud Alibaba教程之Sentinel的使用
這篇文章主要介紹了Spring Cloud Alibaba教程之Sentinel的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Spring?Boot?RestController接口輸出到終端的操作代碼
這篇文章主要介紹了Spring?Boot?RestController接口如何輸出到終端,使用?HttpServletResponse?類,可以在使用curl執(zhí)行?Spring?Boot?REST接口的同時(shí),在控制臺(tái)輸出一些信息,給運(yùn)維人員知道當(dāng)前命令執(zhí)行的狀態(tài),感興趣的朋友跟隨小編一起看看吧2023-09-09Java實(shí)戰(zhàn)寵物醫(yī)院預(yù)約掛號(hào)系統(tǒng)的實(shí)現(xiàn)流程
只學(xué)書(shū)上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+JSP+Spring+SpringBoot+MyBatis+html+layui+maven+Mysql實(shí)現(xiàn)一個(gè)寵物醫(yī)院預(yù)約掛號(hào)系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2022-01-01如何解決Webservice第一次訪問(wèn)特別慢的問(wèn)題
這篇文章主要介紹了如何解決Webservice第一次訪問(wèn)特別慢的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06