欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

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

小編給大家分享一下springboot如何實現(xiàn)rabbitmq的隊列初始化和綁定,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設、高性價比欽南網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式欽南網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設找我們,業(yè)務覆蓋欽南地區(qū)。費用合理售后完善,十載實體公司更值得信賴。

配置文件,在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配置.
 */
@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);
 }
}

隊列發(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;

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

隊列訂閱者

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

springboot是什么

springboot一種全新的編程規(guī)范,其設計目的是用來簡化新Spring應用的初始搭建以及開發(fā)過程,SpringBoot也是一個服務于框架的框架,服務范圍是簡化配置文件。

看完了這篇文章,相信你對“springboot如何實現(xiàn)rabbitmq的隊列初始化和綁定”有了一定的了解,如果想了解更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

當前名稱:springboot如何實現(xiàn)rabbitmq的隊列初始化和綁定
轉載來源:http://www.chinadenli.net/article30/ggihpo.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供虛擬主機商城網(wǎng)站企業(yè)網(wǎng)站制作網(wǎng)站制作網(wǎng)站改版ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營