配置文件,在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. "二分法"-"折半法"-查找算法-之通俗易懂,图文+代码详解-java编程

    转自http://blog.csdn.net/nzfxx/article/details/51615439 1.特点及概念介绍 下面给大家讲解一下"二分法查找"这个java基础查找 ...

  2. CFile类的open方法中的参数说明

    CFile::Open是在MFC里面,打开一个文件的方法. Open函数的原型如下: virtual BOOL Open( LPCTSTR lpszFileName, UINT nOpenFlags, ...

  3. 【卡特兰数】BZOJ1485: [HNOI2009]有趣的数列

    Description 我们称一个长度为2n的数列是有趣的,当且仅当该数列满足以下三个条件: (1)它是从1到2n共2n个整数的一个排列{ai}: (2)所有的奇数项满足a1<a3<…&l ...

  4. K短路 (A*算法) [Usaco2008 Mar]牛跑步&[Sdoi2010]魔法猪学院

    A*属于搜索的一种,启发式搜索,即:每次搜索时加一个估价函数 这个算法可以用来解决K短路问题,常用的估价函数是:已经走过的距离+期望上最短的距离 通常和Dijkstra一起解决K短路 BZOJ1598 ...

  5. java基于BasicPlayer调用 播放音乐

    无聊中想想用java调用下听音乐的api.晚上很多文章用的比较老大方法了,都是用原生的代码写,而且不支持mp3格式,BasicPlayer第三方包提供了很好的api调用,简单的3行代码就可以调用mp3 ...

  6. centos7 启动docker失败的解决

    控制端使用yum install docker安装完成docker后启动docker失败,出现以下信息: Job for docker.service failed because the contr ...

  7. pyqt5实现注册界面并获得文本框内容

    获取框里面的内容,有一个BUG,搞了好久才搞定. __author__ = 'ayew'import sysfrom PyQt5.QtCore import*from PyQt5.QtWidgets ...

  8. c#位运算基本概念与计算过程

    前言 一些非常基础的东西,在实际工作中没有用到.很少用到.一旦遇到,又不知所云.最近遇到一个问题,把一个int16(short) .两个bool变量整合成一个int32(int),当听到这个要求时,我 ...

  9. JS的 try catch使用心得

    try{ //正常执行 }catch(e/*你感觉会出错的 错误类型*/){ // 可能出现的意外 eg:用户自己操作失误 或者 函数少条件 不影响下面的函数执行 // 有时也会用在 比如 focus ...

  10. com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server 报错问题

    com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known serve ...