欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

常見的消息中間件產(chǎn)品:

(1)ActiveMQ

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

(2)RabbitMQ

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

(3)ZeroMQ

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

(4)Kafka

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

(5)RocketMQ 阿里巴巴

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

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

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

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

1.導(dǎo)入依賴

<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.編寫生產(chǎn)者

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轉(zhuǎn)換類 -->
<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隊列中放入任務(wù),讓消費者去取

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(){
    //根據(jù)key發(fā)送到對應(yīng)的隊列
    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(){
    //根據(jù)key發(fā)送到對應(yīng)的隊列
    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(){
   //等待隊列中放入任務(wù),如果有任務(wù),立即消費任務(wù)
    while (true){
    }
  }
}

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

相關(guān)文章

  • 詳解Spring Security中獲取當(dāng)前登錄用戶的詳細(xì)信息的幾種方法

    詳解Spring Security中獲取當(dāng)前登錄用戶的詳細(xì)信息的幾種方法

    本文主要介紹了詳解Spring Security中獲取當(dāng)前登錄用戶的詳細(xì)信息的幾種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • java模擬實現(xiàn)銀行ATM機操作

    java模擬實現(xiàn)銀行ATM機操作

    這篇文章主要為大家詳細(xì)介紹了java模擬實現(xiàn)銀行ATM機操作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • spring boot和mybatis集成分頁插件

    spring boot和mybatis集成分頁插件

    這篇文章主要為大家詳細(xì)介紹了spring boot和mybatis集成分頁插件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • Docker容器中的SSH免密登錄詳解

    Docker容器中的SSH免密登錄詳解

    這篇文章主要介紹了Docker容器中的SSH免密登錄詳解,在日常的開發(fā)和測試環(huán)境中經(jīng)常需要創(chuàng)建和管理Docker容器,有時,出于調(diào)試或管理的目的,可能需要SSH到容器內(nèi)部,本文將介紹如何創(chuàng)建一個Docker容器,它在啟動時自動運行SSH服務(wù),并支持免密登錄,需要的朋友可以參考下
    2023-08-08
  • Java?Agent?(代理)探針技術(shù)詳情

    Java?Agent?(代理)探針技術(shù)詳情

    這篇文章主要介紹了Java?Agent?探針技術(shù)詳情,Java?中的?Agent?技術(shù)可以讓我們無侵入性的去進行代理,最常用于程序調(diào)試、熱部署、性能診斷分析等場景,下文更多相關(guān)資料,感興趣的小伙伴可以參考一下
    2022-04-04
  • Java常用類庫StringBuffer,Runtime,日期操作類等類庫總結(jié)

    Java常用類庫StringBuffer,Runtime,日期操作類等類庫總結(jié)

    這篇文章主要介紹了Java常用類庫StringBuffer,Runtime,日期操作類等類庫總結(jié),需要的朋友可以參考下
    2020-02-02
  • java文件輸出流寫文件的幾種方法

    java文件輸出流寫文件的幾種方法

    這篇文章主要介紹了java文件輸出流寫文件的幾種方法,需要的朋友可以參考下
    2014-04-04
  • 網(wǎng)絡(luò)爬蟲案例解析

    網(wǎng)絡(luò)爬蟲案例解析

    本文主要介紹了網(wǎng)絡(luò)爬蟲的小案例。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-03-03
  • JavaSE實現(xiàn)三子棋游戲

    JavaSE實現(xiàn)三子棋游戲

    這篇文章主要為大家詳細(xì)介紹了JavaSE實現(xiàn)三子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • mybatis foreach 循環(huán) list(map)實例

    mybatis foreach 循環(huán) list(map)實例

    這篇文章主要介紹了mybatis foreach 循環(huán) list(map)實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03

最新評論