springboot實現(xiàn)rabbitmq的隊列初始化和綁定
配置文件,在rabbit中自動建立exchange,queue和綁定它們的關系
- 代碼里初始化exchange
- 代碼里初始化queue
- 代碼里綁定exchange,queue和routekey
- 配置文件,直接聲明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)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關文章
java書店系統(tǒng)畢業(yè)設計 用戶模塊(3)
這篇文章主要介紹了java書店系統(tǒng)畢業(yè)設計,第三步系統(tǒng)總體設計,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
解決JavaEE開發(fā)中字符編碼出現(xiàn)亂碼的問題
下面小編就為大家?guī)硪黄鉀QJavaEE開發(fā)中字符編碼出現(xiàn)亂碼的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
基于Java class對象說明、Java 靜態(tài)變量聲明和賦值說明(詳解)
下面小編就為大家?guī)硪黄贘ava class對象說明、Java 靜態(tài)變量聲明和賦值說明(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
解決在Idea 2020.2下使用 Lombok的注解不生效的問題(插件安裝了,依賴也寫了,自動注解也設置了)
這篇文章主要介紹了在Idea 2020.2下使用 Lombok的注解不生效的問題(插件安裝了,依賴也寫了,自動注解也設置了),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
springboot項目數(shù)據(jù)庫配置類DatabaseConfig示例詳解
這篇文章主要介紹了springboot項目數(shù)據(jù)庫配置類DatabaseConfig實現(xiàn)代碼,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08

