最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2017年10月30日 11:22:30   作者:牛奮lch  
本篇文章主要介紹了詳解spring boot整合JMS(ActiveMQ實現(xiàn)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

本文介紹了spring boot整合JMS(ActiveMQ實現(xiàn)),分享給大家,也給自己留個學習筆記。

一、安裝ActiveMQ

具體的安裝步驟,請參考我的另一篇文章:http://m.fzitv.net/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,需要加如下的依賴包,否則會自動配置失敗,報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對JmsTemplate進行了封裝 
  private JmsMessagingTemplate jmsTemplate; 
  // 發(fā)送消息,destination是發(fā)送到的隊列,message是待發(fā)送的消息 
  public void sendMessage(Destination destination, final String message){ 
    jmsTemplate.convertAndSend(destination, message); 
  } 
} 

六、消息消費者

package com.chhliu.springboot.jms; 
 
import org.springframework.jms.annotation.JmsListener; 
import org.springframework.stereotype.Component; 
 
@Component 
public class Consumer { 
    // 使用JmsListener配置消費者監(jiān)聽的隊列,其中text是接收到的消息 
  @JmsListener(destination = "mytest.queue") 
  public void receiveQueue(String text) { 
    System.out.println("Consumer收到的報文為:"+text); 
  } 
} 

消費者2的代碼同上,注意,消息消費者的類上必須加上@Component,或者是@Service,這樣的話,消息消費者類就會被委派給Listener類,原理類似于使用SessionAwareMessageListener以及MessageListenerAdapter來實現(xiàn)消息驅(qū)動POJO

七、測試

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!!!"); 
    } 
  } 
 
} 

測試結(jié)果如下:

Consumer2收到的報文為:myname is chhliu!!! 
Consumer收到的報文為:myname is chhliu!!! 
Consumer2收到的報文為:myname is chhliu!!! 
Consumer收到的報文為:myname is chhliu!!! 
Consumer2收到的報文為:myname is chhliu!!! 
Consumer收到的報文為:myname is chhliu!!! 
Consumer2收到的報文為:myname is chhliu!!! 
Consumer收到的報文為:myname is chhliu!!! 
Consumer2收到的報文為:myname is chhliu!!! 
Consumer收到的報文為:myname is chhliu!!! 
Consumer2收到的報文為:myname is chhliu!!! 
Consumer收到的報文為:myname is chhliu!!! 
Consumer2收到的報文為:myname is chhliu!!! 

經(jīng)過上面的幾個步驟,spring boot和Jms就基本上整合完成了,是不是使用起來很方便了!

八、實現(xiàn)雙向隊列

1、下面首先來對Consumer2這個消費者來進行下改造,代碼如下:

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收到的報文為:"+text); 
    return "return message"+text; 
  } 
} 

從上面的代碼可以看出,我們在receiveQueue方法上面多加了一個注解@SendTo("out.queue"),該注解的意思是將return回的值,再發(fā)送的"out.queue"隊列中,下面我們再來跑一下前面的測試,在監(jiān)控頁面中,我們發(fā)現(xiàn),"out.queue"隊列中已經(jīng)有內(nèi)容了,如下:

進入Browse界面觀看:

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

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

九、對Producer進行改造

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

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隊列收到的回復(fù)報文為:"+text); 
  } 
} 

測試結(jié)果如下:

從out.queue隊列收到的回復(fù)報文為:return messagemyname is chhliu!!! 
Consumer收到的報文為:myname is chhliu!!! 
Consumer2收到的報文為:myname is chhliu!!! 
從out.queue隊列收到的回復(fù)報文為:return messagemyname is chhliu!!! 
Consumer收到的報文為:myname is chhliu!!! 
Consumer2收到的報文為:myname is chhliu!!! 
從out.queue隊列收到的回復(fù)報文為:return messagemyname is chhliu!!! 
Consumer收到的報文為:myname is chhliu!!! 
Consumer2收到的報文為:myname is chhliu!!! 
從out.queue隊列收到的回復(fù)報文為:return messagemyname is chhliu!!! 

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • java List集合接口的介紹和使用全面教程

    java List集合接口的介紹和使用全面教程

    這篇文章主要為大家介紹了java List集合接口的介紹和使用全面教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • 淺析我對 String、StringBuilder、StringBuffer 的理解

    淺析我對 String、StringBuilder、StringBuffer 的理解

    StringBuilder、StringBuffer 和 String 一樣,都是用于存儲字符串的。這篇文章談?wù)勑【帉tring、StringBuilder、StringBuffer 的理解,感興趣的朋友跟隨小編一起看看吧
    2020-05-05
  • Java并發(fā)編程之同步容器與并發(fā)容器詳解

    Java并發(fā)編程之同步容器與并發(fā)容器詳解

    今天給大家?guī)淼奈恼率荍ava并發(fā)編程的相關(guān)知識,文中對java同步容器與并發(fā)容器做了非常詳細的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • springboot集成RocketMQ過程及使用示例詳解

    springboot集成RocketMQ過程及使用示例詳解

    這篇文章主要為大家介紹了springboot集成RocketMQ過程及使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • springboot集成mqtt的實踐開發(fā)

    springboot集成mqtt的實踐開發(fā)

    本篇文章主要介紹了springboot集成mqtt的實踐開發(fā),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • Java如何獲取Date的“昨天”與“明天”示例代碼

    Java如何獲取Date的“昨天”與“明天”示例代碼

    最近在做項目的時候用到Date和Calendar比較多,而且用到的方式也比較全,突然想到一個問題,Java如何獲取Date的"昨天"與"明天",也就是前一天和后一天呢?思考后寫出了方法,想著萬一以后用到,就總結(jié)出來,也方便有需要的朋友們參考借鑒,下面來一起看看吧。
    2016-12-12
  • Java中的OpenJDK使用原理

    Java中的OpenJDK使用原理

    這篇文章主要介紹了Java中的OpenJDK使用原理,OpenJDK是Java的開發(fā)工具包,關(guān)于Java為什么要使用它文章作簡單介紹,感興趣的朋友可以參考一下
    2022-06-06
  • jmap執(zhí)行失敗如何獲取heapdump詳解

    jmap執(zhí)行失敗如何獲取heapdump詳解

    這篇文章主要為大家介紹了jmap執(zhí)行失敗如何獲取heapdump詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • Java 深入淺出分析Synchronized原理與Callable接口

    Java 深入淺出分析Synchronized原理與Callable接口

    Synchronized關(guān)鍵字解決的是多個線程之間訪問資源的同步性,synchronized關(guān)鍵字可以保證被它修飾的方法或者代碼塊在任意時刻只能有一個線程執(zhí)行,Runnable是執(zhí)行工作的獨立任務(wù),但是不返回任何值。如果我們希望任務(wù)完成之后有返回值,可以實現(xiàn)Callable接口
    2022-03-03
  • 徹底搞明白Spring中的自動裝配和Autowired注解的使用

    徹底搞明白Spring中的自動裝配和Autowired注解的使用

    這篇文章主要介紹了徹底搞明白Spring中的自動裝配和Autowired注解的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03

最新評論

桃源县| 资兴市| 库伦旗| 合川市| 郴州市| 确山县| 广州市| 湛江市| 福建省| 乡城县| 门头沟区| 靖西县| 顺昌县| 庆元县| 阿拉善左旗| 思南县| 和平区| 阿拉善盟| 曲周县| 游戏| 财经| 湖口县| 岳池县| 福海县| 容城县| 清水县| 托克逊县| 武隆县| 嘉定区| 洪江市| 永川市| 城固县| 闸北区| 汉阴县| 图们市| 峡江县| 霍城县| 平江县| 礼泉县| 铅山县| 若尔盖县|