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

队列发布者

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~rabbitmq的队列初始化和绑定的更多相关文章

  1. springboot rabbitmq 死信队列应用场景和完整demo

    何为死信队列? 死信队列实际上就是,当我们的业务队列处理失败(比如抛异常并且达到了retry的上限),就会将消息重新投递到另一个Exchange(Dead Letter Exchanges),该Exc ...

  2. SpringBoot RabbitMQ 延迟队列代码实现

    场景 用户下单后,如果30min未支付,则删除该订单,这时候就要可以用延迟队列 准备 利用rabbitmq_delayed_message_exchange插件: 首先下载该插件:https://ww ...

  3. RabbitMQ镜像队列初始化连接时的“优化”

    之前发过一篇帖子应用.Net+Consul维护RabbitMq的高可用性,然后最近老大问我当初我这么搞是抽的什么想法- -然后顺便贴了两行C#代码: var factory = new Connect ...

  4. SpringBoot使用消息队列RabbitMQ

    RabbitMQ 即一个消息队列,主要是用来实现应用程序的异步和解耦,同时也能起到消息缓冲.消息分发的作用.RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,AMQP,即Advan ...

  5. RabbitMQ镜像队列集群搭建、与SpringBoot整合

    镜像模式 集群模式非常经典的就是Mirror镜像模式,保证100%数据不丢失,在实际工作中也是用的最多的,并且实现集群比较的简单. Mirror镜像队列,目的是为了保证 RabbitMQ 数据的高可靠 ...

  6. springboot~rabbitmq自己通过UI手动发布队列需要注意的地方

    springboot里发布队列消息 为了兼容性和可读性更好,我们一般使用json字符串做为数据载体. public void decreaseCallMonitor(CallMonitorInfo c ...

  7. springboot+rabbitmq整合示例程

    关于什么是rabbitmq,请看另一篇文: http://www.cnblogs.com/boshen-hzb/p/6840064.html 一.新建maven工程:springboot-rabbit ...

  8. springboot + rabbitmq 整合示例

    几个概念说明:Broker:简单来说就是消息队列服务器实体.Exchange:消息交换机,它指定消息按什么规则,路由到哪个队列.Queue:消息队列载体,每个消息都会被投入到一个或多个队列.Bindi ...

  9. springboot+rabbitmq例子

    demo目录 贴代码 1.ProducerConfig.java package com.test.config; import org.springframework.amqp.core.Bindi ...

随机推荐

  1. 在Windows Server 2008 R2下搭建jsp环境(二)-JDK的下载安装

    因为服务器上的Tomcat的运行环境需要JDK的支持,所以,掌握JDK的安装与下载和配置是一个重要步骤.   1.首先下载最新的JDK版本.网络上提供了最新版本的JDK下载,如图所示.首先选择&quo ...

  2. nsqd.go

    }

  3. Oracle 启动监听命令

    win10系统,在win右击"运行"-->输入"services.msc" ,来到服务窗口,找Oracle的相关服务 OracleOraDb10g_hom ...

  4. VS编译代码未通过,常见问题。

    问题一:LNK2028 这个问题一般是什么函数在哪里被引用.修改的方法是:先检查是否包含头文件,如果已经包含了头文件,则检查在源文件的"import.cpp"中是否包含了该lib文 ...

  5. BZOJ_4269_再见Xor_线性基

    BZOJ_4269_再见Xor_线性基 Description 给定N个数,你可以在这些数中任意选一些数出来,每个数可以选任意多次,试求出你能选出的数的异或和的最大值和严格次大值. Input 第一行 ...

  6. 遥远的国度 bzoj3083

    分析: 这个题一看就是裸的树剖... 唯一值得考虑的就是它的根一直在变化,我们可以这样想,如果假根在这个点的子树外,那么直接将这个点的子树作为答案区间,如果在子树内,则相对复杂,我们假设son为roo ...

  7. 从MVC和三层架构说到SSH整合开发

    相信很多人都认同JavaWeb开发是遵从MVC开发模式的,遵从三层架构进行开发的,是的,大家都这么认同.但是相信大家都会有过这样一个疑问,if(MVC三层模式==三层架构思想)out.println( ...

  8. 记一次MySQL数据库拒绝访问的解决过程

    问题背景 用wordpress搭博客,数据库采用MySQL.为了调试方便,创建账户my_account ,允许它从任意主机访问数据库. CREATE USER `my_account`@'%' IDE ...

  9. Docker最全教程——Redis容器化以及排行榜实战(十三)

    前言 容器教程的路还很长,笔者尽量根据实践来不断地完善.由于在编写的过程中还会有完善和补充,后续可能会以番外来补充. 接下来会分享TeamCity.树莓派等内容,节奏可能会有点跳脱. 另外,长沙.NE ...

  10. CentOS 本地和网络yum源简单说明及配置

    1.简述 Yellow dog Updater, Modified由Duke University团队,修改Yellow Dog Linux的Yellow Dog Updater开发而成,是一个基于R ...