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

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

服務器之家 - 編程語言 - Java教程 - springboot實現rabbitmq的隊列初始化和綁定

springboot實現rabbitmq的隊列初始化和綁定

2021-06-02 14:20張占嶺(倉儲大叔,Lind) Java教程

這篇文章主要介紹了springboot實現rabbitmq的隊列初始化和綁定,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

配置文件,在rabbit中自動建立exchange,queue和綁定它們的關系

  1. 代碼里初始化exchange
  2. 代碼里初始化queue
  3. 代碼里綁定exchange,queue和routekey
  4. 配置文件,直接聲明vhost

代碼里初始化exchange

?
1
2
3
4
5
6
7
8
9
/**
  * rabbitmq里初始化exchange.
  *
  * @return
  */
 @bean
 public topicexchange crmexchange() {
  return new topicexchange(exchange);
 }

代碼里初始化queue

?
1
2
3
4
5
6
7
8
9
/**
  * rabbitmq里初始化隊列crm.hello.
  *
  * @return
  */
 @bean
 public queue helloqueue() {
  return new queue(hello);
 }

代碼里綁定exchange,queue和routekey

?
1
2
3
4
5
6
7
8
9
10
11
/**
  * 綁定exchange & queue & routekey.
  *
  * @param queuemessage 隊列
  * @param exchange   交換機
  * @param routekey   路由
  * @return
  */
 public binding bindingexchange(queue queuemessage, topicexchange exchange, string routekey) {
  return bindingbuilder.bind(queuemessage).to(exchange).with(routekey);
 }

配置文件

?
1
2
3
4
5
6
7
spring:
  rabbitmq:
  host: localhost
  port: 5672
  username: guest
  password: guest
  virtual-host: lind

完整代碼

?
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.lind.microservice.productcenter.mq;
 
import org.springframework.amqp.core.binding;
import org.springframework.amqp.core.bindingbuilder;
import org.springframework.amqp.core.queue;
import org.springframework.amqp.core.topicexchange;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
 
/**
 * amqp配置.
 */
@configuration
public class amqpconfig {
 
 /**
  * 交換機.
  */
 public final static string exchange = "crm";
 /**
  * hello隊列.
  */
 public final static string hello = "crm.hello";
 /**
  * 建立訂單隊列.
  */
 public final static string lind_generate_order = "crm.generate.order";
 
 
 /**
  * 綁定exchange & queue & routekey.
  *
  * @param queuemessage 隊列
  * @param exchange   交換機
  * @param routekey   路由
  * @return
  */
 public binding bindingexchange(queue queuemessage, topicexchange exchange, string routekey) {
  return bindingbuilder.bind(queuemessage).to(exchange).with(routekey);
 }
 
 
 /**
  * rabbitmq里初始化exchange.
  *
  * @return
  */
 @bean
 public topicexchange crmexchange() {
  return new topicexchange(exchange);
 }
 
 /**
  * rabbitmq里初始化隊列crm.hello.
  *
  * @return
  */
 @bean
 public queue helloqueue() {
  return new queue(hello);
 }
 
 /**
  * rabbitmq里初始化隊列crm.generate.order.
  *
  * @return
  */
 @bean
 public queue orderqueue() {
  return new queue(lind_generate_order);
 }
}

隊列發布者

?
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
package com.lind.microservice.productcenter.mq;
 
import java.util.date;
import org.springframework.amqp.core.amqptemplate;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.context.annotation.configuration;
 
@configuration
public class hellopublisher {
 @autowired
 amqptemplate rabbittemplate;
 @autowired
 amqpconfig amqpconfig;
 
 public void hello() {
  string context = "hello " + new date();
  system.out.println("hellopublisher : " + context);
  amqpconfig.bindingexchange(
    amqpconfig.helloqueue(),
    amqpconfig.crmexchange(),
    "crm.hello.#"
  );
  this.rabbittemplate.convertandsend(amqpconfig.exchange, amqpconfig.hello, context);
 }
}

隊列訂閱者

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.lind.microservice.productcenter.mq;
 
import org.springframework.amqp.rabbit.annotation.rabbithandler;
import org.springframework.amqp.rabbit.annotation.rabbitlistener;
import org.springframework.stereotype.component;
 
@component
@rabbitlistener(queues = amqpconfig.hello)
public class hellosubscriber {
 @rabbithandler
 public void process(string hello) {
  system.out.println("hellosubscriber : " + hello);
 }
}

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。

原文鏈接:http://www.cnblogs.com/lori/p/9739130.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 俄罗斯性高清完整版 | 日韩一级片免费观看 | 天堂樱桃bt在线www | 国产成人影院在线观看 | 99精品国产成人一区二区在线 | 国产精品毛片va一区二区三区 | 日本一卡二卡3卡四卡网站精品 | 99任你躁精品视频 | 香蕉eeww99国产精品 | 91麻豆精品 | 国产精品久久久久不卡绿巨人 | 亚洲午夜久久久久影院 | 日本高清视频网站www | ai换脸明星专区在线观看 | 国产免费一区不卡在线 | 513热点网 | 国产精品女主播自在线拍 | 国产精品一区二区久久 | 亚洲AV无码乱码在线观看浪潮 | 日韩在线天堂免费观看 | 午夜第九达达兔鲁鲁 | 成 人免费va视频 | 免费观看日本 | 超兴奋朋友的中文字幕下 | 無码一区中文字幕少妇熟女网站 | 亚洲精品一区二区久久这里 | 午夜AV内射一区二区三区红桃视 | 污影院 | 国产精品九九热 | 亚洲 制服 欧美 中文字幕 | 精品无码人妻一区二区免费AV | 免费岛国| 欧美性bbbbbxxxxxxx| 四虎官网 | 我半夜摸妺妺的奶C了她软件 | 国内精品露脸在线视频播放 | 免费标准高清看机机桶机机 | chanelpreston欧美网站 | 波多野结中文字幕在线69视频 | 日本免费一区二区三区 | 1986葫芦兄弟全集免费观看第十集 |