麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發(fā) > Java > 正文

springboot實現(xiàn)rabbitmq的隊列初始化和綁定

2024-07-14 08:42:27
字體:
供稿:網(wǎng)友

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

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

代碼里初始化exchange

/**  * rabbitMq里初始化exchange.  *  * @return  */ @Bean public TopicExchange crmExchange() {  return new TopicExchange(EXCHANGE); }

代碼里初始化queue

/**  * rabbitMq里初始化隊列crm.hello.  *  * @return  */ @Bean public Queue helloQueue() {  return new Queue(HELLO); }

代碼里綁定exchange,queue和routekey

/**  * 綁定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); }

配置文件

spring:  rabbitmq:  host: localhost  port: 5672  username: guest  password: guest  virtual-host: lind

完整代碼

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配置. */@Configurationpublic 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); }}

隊列發(fā)布者

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;@Configurationpublic 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); }}

隊列訂閱者

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

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對VeVb武林網(wǎng)的支持。


注:相關(guān)教程知識閱讀請移步到JAVA教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 国产精品久久久久久久四虎电影 | 国外成人在线视频 | 国产精品呻吟 | 亚洲综合视频一区 | 久久99综合 | 国产午夜精品在线 | 亚洲第一页综合 | 亚洲精品午夜在线 | 国产精品一品二区三区四区18 | 性猛交ⅹxxx乱巴西 在线播放中文 | 精品一区二区三区中文字幕 | 久久久久久久久久久久久国产精品 | 日韩精品久久久久久久九岛 | 夜夜看| 亚洲一区 国产 | 一区二区三区欧美日韩 | 日本精品中文字幕 | xnxx 日本19| 奶子吧naiziba.cc免费午夜片在线观看 | 国产一区二区观看 | 免费香蕉成视频成人网 | 91豆奶| 久草视频福利在线观看 | 国产一国产精品一级毛片 | 一二区成人影院电影网 | 一级一级一级一级毛片 | 亚洲精品无码不卡在线播放he | 午夜伊人| 国产一级二级在线播放 | 国产一区二区不卡视频 | 一区二区三区日本在线观看 | 一级黄色欧美 | 中国产一级毛片 | 伊人99在线| 伊人二本二区 | 国产一级免费在线视频 | 一边吃奶一边摸下娇喘 | 一区二区久久久久草草 | 日本视频网 | 欧美成人精品一区二区 | 精品999www|