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

Spring Boot RabbitMQ 延遲消息實(shí)現(xiàn)完整版示例

 更新時(shí)間:2018年05月04日 14:39:43   作者:Sam哥哥  
本篇文章主要介紹了Spring Boot RabbitMQ 延遲消息實(shí)現(xiàn)完整版示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

概述

曾經(jīng)去網(wǎng)易面試的時(shí)候,面試官問(wèn)了我一個(gè)問(wèn)題,說(shuō)

下完訂單后,如果用戶未支付,需要取消訂單,可以怎么做

我當(dāng)時(shí)的回答是,用定時(shí)任務(wù)掃描DB表即可。面試官不是很滿意,提出:

用定時(shí)任務(wù)無(wú)法做到準(zhǔn)實(shí)時(shí)通知,有沒(méi)有其他辦法?

我當(dāng)時(shí)的回答是:

可以用隊(duì)列,訂單下完后,發(fā)送一個(gè)消息到隊(duì)列里,并指定過(guò)期時(shí)間,時(shí)間一到,執(zhí)行回調(diào)接口。

面試官聽(tīng)完后,就不再問(wèn)了。其實(shí)我當(dāng)時(shí)的思路是對(duì)的,只不過(guò)講的不是很專業(yè)而已。專業(yè)說(shuō)法是利用 延遲消息 。

其實(shí)用定時(shí)任務(wù),確實(shí)有點(diǎn)問(wèn)題,原本業(yè)務(wù)系統(tǒng)希望10分鐘后,如果訂單未支付,就馬上取消訂單,并釋放商品庫(kù)存。但是一旦數(shù)據(jù)量大的話,就會(huì)加長(zhǎng)獲取未支付訂單數(shù)據(jù)的時(shí)間,部分訂單就做不到10分鐘后取消了,可能是15分鐘,20分鐘之類的。這樣的話,庫(kù)存就無(wú)法及時(shí)得到釋放,也就會(huì)影響成單數(shù)。而利用延遲消息,則理論上是可以做到按照設(shè)定的時(shí)間,進(jìn)行訂單取消操作的。

目前網(wǎng)上關(guān)于使用RabbitMQ實(shí)現(xiàn)延遲消息的文章,大多都是講如何利用RabbitMQ的死信隊(duì)列來(lái)實(shí)現(xiàn),實(shí)現(xiàn)方案看起來(lái)都很繁瑣復(fù)雜,并且還是使用原始的RabbitMQ Client API來(lái)實(shí)現(xiàn)的,更加顯得啰嗦。

Spring Boot 已經(jīng)對(duì)RabbitMQ Client API進(jìn)行了包裝,使用起來(lái)簡(jiǎn)潔很多,下面詳細(xì)介紹一下如何利用 rabbitmq_delayed_message_exchange 插件和Spring Boot來(lái)實(shí)現(xiàn)延遲消息。

軟件準(zhǔn)備

erlang

本文使用的版本是:Erlang 20.3

RabbitMQ

本文使用的是 window 版本的RabbitMQ,版本號(hào)是:3.7.4

rabbitmq_delayed_message_exchange插件

插件下載地址:http://www.rabbitmq.com/community-plugins.html

打開(kāi)網(wǎng)址后,ctrl + f,搜索 rabbitmq_delayed_message_exchange 。

千萬(wàn)記住,一定選好版本號(hào),由于我使用的是RabbitMQ 3.7.4,因此對(duì)應(yīng)的 rabbitmq_delayed_message_exchange 插件也必須選擇3.7.x的。

如果沒(méi)有選對(duì)版本,在使用延遲消息的時(shí)候,會(huì)遇到各種各樣的奇葩問(wèn)題,而且網(wǎng)上還找不到解決方案。我因?yàn)檫@個(gè)問(wèn)題,折騰了整整一個(gè)晚上。請(qǐng)牢記,要選對(duì)插件版本。

下載完插件后,將其放置到RabbitMQ安裝目錄下的 plugins 目錄下,并使用如下命令啟動(dòng)這個(gè)插件:

rabbitmq-plugins enable rabbitmq_delayed_message_exchange

如果啟動(dòng)成功會(huì)出現(xiàn)如下信息:

The following plugins have been enabled:   rabbitmq_delayed_message_exchange

啟動(dòng)插件成功后,記得重啟一下RabbitMQ,讓其生效。

集成RabbitMQ

這個(gè)就非常簡(jiǎn)單了,直接在maven工程的pom.xml文件中加入

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

Spring Boot的版本我使用的是 2.0.1.RELEASE .

接下來(lái)在 application.properties 文件中加入redis配置:

spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

定義ConnectionFactory和RabbitTemplate

也很簡(jiǎn)單,代碼如下:

package com.mq.rabbitmq;

import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "spring.rabbitmq")
public class RabbitMqConfig {
  private String host;
  private int port;
  private String userName;
  private String password;

  @Bean
  public ConnectionFactory connectionFactory() {
    CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(host,port);
    cachingConnectionFactory.setUsername(userName);
    cachingConnectionFactory.setPassword(password);
    cachingConnectionFactory.setVirtualHost("/");
    cachingConnectionFactory.setPublisherConfirms(true);
    return cachingConnectionFactory;
  }

  @Bean
  public RabbitTemplate rabbitTemplate() {
    RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());
    return rabbitTemplate;
  }

  public String getHost() {
    return host;
  }

  public void setHost(String host) {
    this.host = host;
  }

  public int getPort() {
    return port;
  }

  public void setPort(int port) {
    this.port = port;
  }

  public String getUserName() {
    return userName;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }
}

Exchange和Queue配置

package com.mq.rabbitmq;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class QueueConfig {

  @Bean
  public CustomExchange delayExchange() {
    Map<String, Object> args = new HashMap<>();
    args.put("x-delayed-type", "direct");
    return new CustomExchange("test_exchange", "x-delayed-message",true, false,args);
  }

  @Bean
  public Queue queue() {
    Queue queue = new Queue("test_queue_1", true);
    return queue;
  }

  @Bean
  public Binding binding() {
    return BindingBuilder.bind(queue()).to(delayExchange()).with("test_queue_1").noargs();
  }
}

這里要特別注意的是,使用的是 CustomExchange ,不是 DirectExchange ,另外 CustomExchange 的類型必須是 x-delayed-message 。

實(shí)現(xiàn)消息發(fā)送

package com.mq.rabbitmq;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class MessageServiceImpl {

  @Autowired
  private RabbitTemplate rabbitTemplate;

  public void sendMsg(String queueName,String msg) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println("消息發(fā)送時(shí)間:"+sdf.format(new Date()));
    rabbitTemplate.convertAndSend("test_exchange", queueName, msg, new MessagePostProcessor() {
      @Override
      public Message postProcessMessage(Message message) throws AmqpException {
        message.getMessageProperties().setHeader("x-delay",3000);
        return message;
      }
    });
  }
}

注意在發(fā)送的時(shí)候,必須加上一個(gè)header

x-delay

在這里我設(shè)置的延遲時(shí)間是3秒。

消息消費(fèi)者

package com.mq.rabbitmq;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
@Component
public class MessageReceiver {

  @RabbitListener(queues = "test_queue_1")
  public void receive(String msg) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println("消息接收時(shí)間:"+sdf.format(new Date()));
    System.out.println("接收到的消息:"+msg);
  }
}

運(yùn)行Spring Boot程序和發(fā)送消息

直接在main方法里運(yùn)行Spring Boot程序,Spring Boot會(huì)自動(dòng)解析 MessageReceiver 類的。

接下來(lái)只需要用Junit運(yùn)行一下發(fā)送消息的接口即可。

package com.mq.rabbitmq;
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 RabbitmqApplicationTests {
  @Autowired
  private MessageServiceImpl messageService;
  @Test
  public void send() {
    messageService.sendMsg("test_queue_1","hello i am delay msg");
  }
}

運(yùn)行完后,可以看到如下信息:

消息發(fā)送時(shí)間:2018-05-03 12:44:53
3秒鐘后,Spring Boot控制臺(tái)會(huì)輸出:
消息接收時(shí)間:2018-05-03 12:44:56
接收到的消息:hello i am delay msg

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

相關(guān)文章

  • Java消息隊(duì)列RabbitMQ入門詳解

    Java消息隊(duì)列RabbitMQ入門詳解

    這篇文章主要介紹了Java消息隊(duì)列RabbitMQ入門詳解,RabbitMQ是使用Erlang語(yǔ)言開(kāi)發(fā)的開(kāi)源消息隊(duì)列系統(tǒng),基于AMQP協(xié)議 來(lái)實(shí)現(xiàn),AMQP的主要特征是面向消息、隊(duì)列、路由(包括點(diǎn)對(duì)點(diǎn)和發(fā)布 /訂閱)、可靠性、安全,需要的朋友可以參考下
    2023-07-07
  • MyBatis中的resultMap簡(jiǎn)要概述

    MyBatis中的resultMap簡(jiǎn)要概述

    這篇文章主要介紹了MyBatis中的resultMap簡(jiǎn)要概述的相關(guān)資料,需要的朋友可以參考下
    2016-07-07
  • SpringBoot之自定義啟動(dòng)異常堆棧信息打印方式

    SpringBoot之自定義啟動(dòng)異常堆棧信息打印方式

    這篇文章主要介紹了SpringBoot之自定義啟動(dòng)異常堆棧信息打印方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 501 Command "HELO" requires an argument問(wèn)題的解決方法

    501 Command "HELO" requires an argument問(wèn)題的解決方法

    換一個(gè)windows服務(wù)器,發(fā)現(xiàn)就沒(méi)這樣的問(wèn)題,僅在一臺(tái)Linux服務(wù)器上可以重現(xiàn),直觀感覺(jué)就是這臺(tái)Linux服務(wù)器某些配置有問(wèn)題
    2013-08-08
  • SpringBoot整合Mybatis-plus關(guān)鍵詞模糊查詢結(jié)果為空

    SpringBoot整合Mybatis-plus關(guān)鍵詞模糊查詢結(jié)果為空

    SpringBoot整合Mybatis-plus使用關(guān)鍵詞模糊查詢的時(shí)候,數(shù)據(jù)庫(kù)中有數(shù)據(jù),但是無(wú)法查找出來(lái),本文就來(lái)介紹一下SpringBoot整合Mybatis-plus關(guān)鍵詞模糊查詢結(jié)果為空的解決方法
    2025-04-04
  • Java中監(jiān)聽(tīng)器Listener詳解

    Java中監(jiān)聽(tīng)器Listener詳解

    Listener是由Java編寫(xiě)的WEB組件,主要完成對(duì)內(nèi)置對(duì)象狀態(tài)的變化 (創(chuàng)建、銷毀)和屬性的變化進(jìn)行監(jiān)聽(tīng),做進(jìn)一步的處理,主要對(duì)session和application內(nèi)置對(duì)象監(jiān)聽(tīng),這篇文章主要介紹了Java中監(jiān)聽(tīng)器Listener,需要的朋友可以參考下
    2023-08-08
  • springboot2中HikariCP連接池的相關(guān)配置問(wèn)題

    springboot2中HikariCP連接池的相關(guān)配置問(wèn)題

    這篇文章主要介紹了springboot2中HikariCP連接池的相關(guān)配置問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • java 文件上傳到讀取文件內(nèi)容的實(shí)例

    java 文件上傳到讀取文件內(nèi)容的實(shí)例

    今天小編就為大家分享一篇java 文件上傳到讀取文件內(nèi)容的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • Java實(shí)現(xiàn)替換Word中文本和圖片功能

    Java實(shí)現(xiàn)替換Word中文本和圖片功能

    Word中的替換功能以查找指定文本然后替換為新的文本,可單個(gè)替換或全部替換。本文將用Java語(yǔ)言實(shí)現(xiàn)Word中的文本、圖片替換功能,需要的可以參考一下
    2022-06-06
  • Java中接口的多態(tài)詳解

    Java中接口的多態(tài)詳解

    大家好,本篇文章主要講的是Java中接口的多態(tài)詳解,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-02-02

最新評(píng)論

岳池县| 崇信县| 阜城县| 泰顺县| 阳西县| 山阴县| 永福县| 收藏| 靖州| 丹棱县| 花莲县| 温泉县| 乾安县| 宜都市| 黑水县| 大冶市| 金昌市| 渝北区| 忻城县| 淮北市| 青冈县| 张北县| 隆德县| 古蔺县| 寿光市| 乌拉特中旗| 西城区| 富源县| 平湖市| 辉县市| 纳雍县| 建平县| 荥经县| 米林县| 贡觉县| 双城市| 新昌县| 察隅县| 宜良县| 县级市| 柞水县|