欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2018年10月03日 09:59:47   作者:張占嶺(倉儲大叔,Lind)  
這篇文章主要介紹了springboot實現(xiàn)rabbitmq的隊列初始化和綁定,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

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

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

相關(guān)文章

  • 關(guān)于logback日志級別動態(tài)切換的四種方式

    關(guān)于logback日志級別動態(tài)切換的四種方式

    這篇文章主要介紹了關(guān)于logback日志級別動態(tài)切換的四種方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Javadoc標簽和Javadoc注釋規(guī)范說明

    Javadoc標簽和Javadoc注釋規(guī)范說明

    這篇文章主要介紹了Javadoc標簽和Javadoc注釋規(guī)范說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • java構(gòu)造函數(shù)的三種類型總結(jié)

    java構(gòu)造函數(shù)的三種類型總結(jié)

    在本篇文章里小編給大家整理了一篇關(guān)于java構(gòu)造函數(shù)的三種類型總結(jié)內(nèi)容,有需要的朋友們可以學(xué)習(xí)參考下。
    2021-01-01
  • Java 創(chuàng)建線程的兩個方法詳解及實例

    Java 創(chuàng)建線程的兩個方法詳解及實例

    這篇文章主要介紹了Java 創(chuàng)建線程的兩個方法詳解及實例的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • SpringBoot利用Junit動態(tài)代理實現(xiàn)Mock方法

    SpringBoot利用Junit動態(tài)代理實現(xiàn)Mock方法

    說到Spring Boot 單元測試主要有兩個主流集成分別是Mockito,Junit,這個各有特點,在實際開發(fā)中,我想要的測試框架應(yīng)該是這個框架集成者,本文給大家介紹了SpringBoot利用Junit動態(tài)代理實現(xiàn)Mock方法,需要的朋友可以參考下
    2024-04-04
  • Springboot常用方法參數(shù)注解示例詳解

    Springboot常用方法參數(shù)注解示例詳解

    這篇文章主要介紹了Springboot常用方法參數(shù)注解及示例,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08
  • Java多線程死鎖問題詳解(wait和notify)

    Java多線程死鎖問題詳解(wait和notify)

    線程之間形成相互等待資源的環(huán)時,就會形成順序死鎖,下面這篇文章主要給大家介紹了關(guān)于Java多線程死鎖問題(wait和notify)的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-01-01
  • Java網(wǎng)絡(luò)編程三要素及通信程序詳解

    Java網(wǎng)絡(luò)編程三要素及通信程序詳解

    這篇文章主要介紹了Java網(wǎng)絡(luò)編程三要素及通信程序詳解,Java網(wǎng)絡(luò)編程是在網(wǎng)絡(luò)通信協(xié)議下,實現(xiàn)網(wǎng)絡(luò)互連的不同計算機上運行的程序間可以進行數(shù)據(jù)交換,需要的朋友可以參考下
    2023-07-07
  • JVM的垃圾回收機制真是通俗易懂

    JVM的垃圾回收機制真是通俗易懂

    這篇文章主要為大家詳細介紹了JVM的垃圾回收機制,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • Java?Map的compute方法舉例詳解

    Java?Map的compute方法舉例詳解

    Java中的Map是一種用于存儲鍵值對的數(shù)據(jù)結(jié)構(gòu),它提供了一系列的方法來操作和訪問其中的元素,下面這篇文章主要給大家介紹了關(guān)于Java?Map的compute方法舉例詳解的相關(guān)資料,需要的朋友可以參考下
    2024-06-06

最新評論