一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 詳解spring boot整合JMS(ActiveMQ實(shí)現(xiàn))

詳解spring boot整合JMS(ActiveMQ實(shí)現(xiàn))

2021-01-27 11:55牛奮lch Java教程

本篇文章主要介紹了詳解spring boot整合JMS(ActiveMQ實(shí)現(xiàn)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

本文介紹了spring boot整合jms(activemq實(shí)現(xiàn)),分享給大家,也給自己留個(gè)學(xué)習(xí)筆記。

一、安裝activemq

具體的安裝步驟,請(qǐng)參考我的另一篇文章:http://m.ythuaji.com.cn/article/135795.html

二、新建spring boot工程,并加入jms(activemq)依賴

詳解spring boot整合JMS(ActiveMQ實(shí)現(xiàn))

三、工程結(jié)構(gòu)

詳解spring boot整合JMS(ActiveMQ實(shí)現(xiàn))

pom依賴如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?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配置文件

?
1
2
3
4
5
6
## 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注入失敗
?
1
2
3
4
5
<dependency>
      <groupid>org.apache.activemq</groupid>
      <artifactid>activemq-pool</artifactid>
      <!-- <version>5.7.0</version> -->
    </dependency>

五、消息生產(chǎn)者

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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)者

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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)聽的隊(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來實(shí)現(xiàn)消息驅(qū)動(dòng)pojo

七、測(cè)試

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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é)果如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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)過上面的幾個(gè)步驟,spring boot和jms就基本上整合完成了,是不是使用起來很方便了!

八、實(shí)現(xiàn)雙向隊(duì)列

1、下面首先來對(duì)consumer2這個(gè)消費(fèi)者來進(jìn)行下改造,代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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è)賮砼芤幌虑懊娴臏y(cè)試,在監(jiān)控頁面中,我們發(fā)現(xiàn),"out.queue"隊(duì)列中已經(jīng)有內(nèi)容了,如下:

詳解spring boot整合JMS(ActiveMQ實(shí)現(xiàn))

進(jìn)入browse界面觀看:

詳解spring boot整合JMS(ActiveMQ實(shí)現(xiàn))

最后看下收到的具體信息:

詳解spring boot整合JMS(ActiveMQ實(shí)現(xiàn))

我們發(fā)現(xiàn),該隊(duì)列中的消息,就是我們返回的值!

九、對(duì)producer進(jìn)行改造

通過上面的示例,我們現(xiàn)在對(duì)producer進(jìn)行改造,使其既能生產(chǎn)報(bào)文,又能消費(fèi)隊(duì)列中的報(bào)文,代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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é)果如下:

?
1
2
3
4
5
6
7
8
9
10
從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í)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://blog.csdn.net/liuchuanhong1/article/details/54603546

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日日网| 亚洲AV精品一区二区三区不卡 | 亚洲上最大成网人站4438 | 国产一二三区视频 | 免费人成网址在线观看国内 | 国产aⅴ一区二区三区 | 日韩免费高清完整版 | 91系列在线观看免费 | 精品精品国产自在久久高清 | 国产一区精品视频 | 亚洲精品久久久打桩机 | 国产小嫩模好紧 | 午夜家庭影院 | 午夜精品久视频在线观看 | 无敌秦墨漫画免费阅读 | 久久视频在线视频观看天天看视频 | 青青青国产在线观看 | 日本在线视频免费看 | 波多野结衣52部合集在线观看 | 91短视频在线观看2019 | 特黄aa级毛片免费视频播放 | 无码欧美喷潮福利XXXX | 国内精品久久久久久中文字幕 | 扒开双腿猛进入爽爽视频ai | 午夜一区二区免费视频 | 幸福草电视剧演员表介绍 | 久久se视频精品视频在线 | 亚洲美女人黄网成人女 | 四虎永久免费地址在线网站 | 日韩一级免费毛片 | 国产亚洲视频网站 | 精品国产乱码久久久久久免费 | 欧美一级特黄特色大片免费 | 双子母性本能在线 | 男女真实无遮挡xx00动态图软件 | 黑人粗长大战亚洲女 | 亚洲精品一区二区三区在线播放 | sihu国产午夜精品一区二区三区 | 国产视频一区在线观看 | 日本午夜小视频 | 摸进老太婆的裤裆小说 |