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

簡單了解如何在spring中使用RabbitMQ

 更新時間:2019年12月12日 09:33:13   作者:Runtimeing  
這篇文章主要介紹了簡單了解如何在spring中使用RabbitMQ,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

這篇文章主要介紹了簡單了解如何在spring中使用RabbitMQ,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

常見的消息中間件產品:

(1)ActiveMQ

ActiveMQ 是Apache出品,最流行的,能力強勁的開源消息總線。ActiveMQ 是一個完全支持JMS1.1和J2EE 1.4規(guī)范的 JMS Provider實現。

(2)RabbitMQ

AMQP協(xié)議的領導實現,支持多種場景。淘寶的MySQL集群內部有使用它進行通訊,OpenStack開源云平臺的通信組件,最先在金融行業(yè)得到運用。我們在本次課程中介紹 RabbitMQ的使用。

(3)ZeroMQ

史上最快的消息隊列系統(tǒng)

(4)Kafka

Apache下的一個子項目 。特點:高吞吐,在一臺普通的服務器上既可以達到10W/s的吞吐速率;完全的分布式系統(tǒng)。適合處理海量數據。

(5)RocketMQ 阿里巴巴

消息中間件利用高效可靠的消息傳遞機制進行平臺無關的數據交流,并基于數據通信來進行分布式系統(tǒng)的集成。通過提供消息傳遞和消息排隊模型,它可以在分布式環(huán)境下擴展進程間的通信。對于消息中間件,常見的角色大致也就有Producer(生產者)、Consumer(消費者)。

消息隊列中間件是分布式系統(tǒng)中重要的組件,主要解決應用解耦,異步消息,流量削鋒等問題,實現高性能,高可用,可伸縮和最終一致性架構。

​ Spring-amqp是對AMQP協(xié)議的抽象實現,而spring-rabbit 是對協(xié)議的具體實現,也是目前的唯一實現。底層使用的就是RabbitMQ。

已經配置好了ssm的開發(fā)環(huán)境

1.導入依賴

<dependencies>
  <dependency>
    <groupId>com.rabbitmq</groupId>
    <artifactId>amqp-client</artifactId>
    <version>5.5.3</version>
  </dependency>
  <dependency>
    <groupId>org.springframework.amqp</groupId>
    <artifactId>spring-rabbit</artifactId>
    <version>2.1.3.RELEASE</version>
  </dependency>

  <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.5</version>
  </dependency>
</dependencies>

2.編寫生產者

2.1配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
    xsi:schemaLocation="http://www.springframework.org/schema/rabbit
  http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd">

  <context:component-scan base-package="cn.test.rabbitmq.spring"/>

<!-- 配置連接工廠 -->
<rabbit:connection-factory id="connectionFactory" virtual-host="/saas"
              host="127.0.0.1" port="5672" username="saas" password="saas" />
<!-- 定義mq管理 -->
<rabbit:admin connection-factory="connectionFactory" />

<!-- 聲明隊列 -->
<rabbit:queue name="spring.test.queue" auto-declare="true" durable="true" />

<!-- 定義交換機綁定隊列(路由模式) -->
<rabbit:direct-exchange name="spring.test.exchange">
  <rabbit:bindings>
    <rabbit:binding queue="spring.test.queue" key="user.insert" />
  </rabbit:bindings>
</rabbit:direct-exchange>
<!-- 定義交換機綁定隊列(路由模式)使用匹配符
<rabbit:topic-exchange id="springTestExchange" name="spring.test.exchange">
  <rabbit:bindings>
    <rabbit:binding queue="spring.test.queue" pattern="#.#" />
  </rabbit:bindings>
</rabbit:topic-exchange>
-->
<!-- 消息對象json轉換類 -->
<bean id="jsonMessageConverter"
   class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" />

<!-- 定義模版 -->
<rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"
         exchange="spring.test.exchange"
         message-converter="jsonMessageConverter"/>

</beans>

2.2 發(fā)送方代碼

這里是往RabbitMQ隊列中放入任務,讓消費者去取

package cn.test.rabbitmq.spring;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MqSender {
  @Autowired
  private AmqpTemplate amqpTemplate;
  public void sendMessage(){
    //根據key發(fā)送到對應的隊列
    amqpTemplate.convertAndSend("user.insert","spring整合RabbitMQ消息");
    System.out.println("發(fā)送成功........");
  }
}

2.3 測試代碼

package cn.test.rabbitmq.spring;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-mq-send.xml")
public class MqSendDemo {

  @Autowired
  private MqSender mqSender;
  @Test
  public void test(){
    //根據key發(fā)送到對應的隊列
    mqSender.sendMessage();
  }
}

3.編寫消費者

3.1 配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
    xsi:schemaLocation="http://www.springframework.org/schema/rabbit
  http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd">


  <!-- 配置連接工廠 -->
  <rabbit:connection-factory id="connectionFactory" virtual-host="/saas"
                host="127.0.0.1" port="5672" username="saas" password="saas" />
  <!-- 定義mq管理 -->
  <rabbit:admin connection-factory="connectionFactory" />

  <!-- 聲明隊列 -->
  <rabbit:queue name="spring.test.queue" auto-declare="true" durable="true" />

  <!-- 定義消費者 -->
  <bean id="testMqListener" class="cn.test.rabbitmq.spring.MqListener" />

  <!-- 定義消費者監(jiān)聽隊列 -->
  <rabbit:listener-container
      connection-factory="connectionFactory">
    <rabbit:listener ref="testMqListener" queues="spring.test.queue" />
  </rabbit:listener-container>

</beans>

3.2 監(jiān)聽代碼

package cn.test.rabbitmq.spring;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

public class MqListener implements MessageListener {

  public void onMessage(Message message) {
    try {
      System.out.println(message.getBody());
      String ms = new String(message.getBody(), "UTF-8");
      System.out.println(ms);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

3.3 測試代碼

package cn.itcast.rabbitmq.spring;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-mq-receive.xml")
public class MqReceiveDemo {

  @Test
  public void test(){
   //等待隊列中放入任務,如果有任務,立即消費任務
    while (true){
    }
  }
}

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

相關文章

  • 詳解SpringBoot如何讓指定的Bean先加載

    詳解SpringBoot如何讓指定的Bean先加載

    這篇文章主要給大家介紹了在 SpringBoot 中如何讓自己的某個指定的 Bean 在其他 Bean 前完成被 Spring 加載,文中通過代碼示例給大家講解的非常詳細,對大家的學習或工作有一定的幫助,需要的朋友可以參考下
    2024-06-06
  • idea環(huán)境下Maven無法正常下載pom中配置的包問題

    idea環(huán)境下Maven無法正常下載pom中配置的包問題

    這篇文章主要介紹了idea環(huán)境下Maven無法正常下載pom中配置的包的問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • Java實現TCP和UDP協(xié)議詳解

    Java實現TCP和UDP協(xié)議詳解

    這篇文章主要介紹了Java實現TCP和UDP協(xié)議詳解,TCP(傳輸控制協(xié)議)和UDP(用戶數據報協(xié)議)是兩種最常用的傳輸層協(xié)議,它們都用于在網絡上傳輸數據,但是它們之間有很多不同之處,需要的朋友可以參考下
    2023-07-07
  • Java中的異常處理機制介紹(非常全面!)

    Java中的異常處理機制介紹(非常全面!)

    異??赡苁窃诔绦驁?zhí)行過程中產生的,也可能是程序中throw主動拋出的,下面這篇文章主要給大家介紹了關于Java中異常處理機制的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-01-01
  • mybatis-plus如何使用mapper的xml

    mybatis-plus如何使用mapper的xml

    這篇文章主要介紹了mybatis-plus如何使用mapper的xml問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Java 鎖的知識總結及實例代碼

    Java 鎖的知識總結及實例代碼

    這篇文章主要介紹了Java 鎖的知識總結及實例代碼,需要的朋友可以參考下
    2016-09-09
  • java垃圾收集器與內存分配策略詳解

    java垃圾收集器與內存分配策略詳解

    本篇文章主要介紹了Java垃圾收集器與內存分配策略的方法和原理總結,Java垃圾回收器是Java虛擬機的重要模塊,具有一定的參考價值,有興趣的可以了解一下
    2021-08-08
  • 利用spring-boot-maven-plugin插件打包SpringBoot應用方式

    利用spring-boot-maven-plugin插件打包SpringBoot應用方式

    spring-boot-maven-plugin插件可以將SpringBoot應用打成帶依賴的jar包,該包中不僅包含應用自身的代碼,還包含了pom.xml中配置的依賴,修改pom.xml打包后,生成的jar包就包含了項目依賴,生成的jar包位于項目的target文件夾下
    2025-02-02
  • SpringMVC 中配置 Swagger 插件的教程(分享)

    SpringMVC 中配置 Swagger 插件的教程(分享)

    下面小編就為大家分享一篇SpringMVC 中配置 Swagger 插件的教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • Gradle的SpringBoot項目構建圖解

    Gradle的SpringBoot項目構建圖解

    這篇文章主要介紹了Gradle的SpringBoot項目構建圖解,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01

最新評論

科技| 濮阳县| 丹寨县| 陵水| 盘山县| 信丰县| 西宁市| 广灵县| 龙门县| 辽中县| 恭城| 丹凤县| 页游| 永川市| 巴马| 连云港市| 崇文区| 万安县| 公主岭市| 西乌珠穆沁旗| 新疆| 福安市| 郯城县| 华亭县| 勐海县| 老河口市| 宜宾市| 毕节市| 沾化县| 凉城县| 河曲县| 策勒县| 益阳市| 长顺县| 沁水县| 嫩江县| 绥阳县| 化隆| 秭归县| 克山县| 德令哈市|