国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

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

2024-07-14 08:42:27
字體:
來源:轉載
供稿:網友

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

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

隊列發布者

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

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


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 通州区| 秦安县| 金阳县| 炎陵县| 湘潭县| 伊川县| 贡山| 武宣县| 镇沅| 内乡县| 方正县| 横山县| 高碑店市| 绥化市| 休宁县| 竹溪县| 镇安县| 子洲县| 牙克石市| 衡阳市| 油尖旺区| 忻城县| 德格县| 北安市| 新邵县| 卓资县| 克什克腾旗| 卢氏县| 曲松县| 江西省| 南投县| 霞浦县| 皋兰县| 连山| 娱乐| 汾西县| 宜丰县| 太和县| 盐亭县| 武陟县| 交口县|