springboot~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);
}
}
队列发布者
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的队列初始化和绑定的更多相关文章
- springboot rabbitmq 死信队列应用场景和完整demo
何为死信队列? 死信队列实际上就是,当我们的业务队列处理失败(比如抛异常并且达到了retry的上限),就会将消息重新投递到另一个Exchange(Dead Letter Exchanges),该Exc ...
- SpringBoot RabbitMQ 延迟队列代码实现
场景 用户下单后,如果30min未支付,则删除该订单,这时候就要可以用延迟队列 准备 利用rabbitmq_delayed_message_exchange插件: 首先下载该插件:https://ww ...
- RabbitMQ镜像队列初始化连接时的“优化”
之前发过一篇帖子应用.Net+Consul维护RabbitMq的高可用性,然后最近老大问我当初我这么搞是抽的什么想法- -然后顺便贴了两行C#代码: var factory = new Connect ...
- SpringBoot使用消息队列RabbitMQ
RabbitMQ 即一个消息队列,主要是用来实现应用程序的异步和解耦,同时也能起到消息缓冲.消息分发的作用.RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,AMQP,即Advan ...
- RabbitMQ镜像队列集群搭建、与SpringBoot整合
镜像模式 集群模式非常经典的就是Mirror镜像模式,保证100%数据不丢失,在实际工作中也是用的最多的,并且实现集群比较的简单. Mirror镜像队列,目的是为了保证 RabbitMQ 数据的高可靠 ...
- springboot~rabbitmq自己通过UI手动发布队列需要注意的地方
springboot里发布队列消息 为了兼容性和可读性更好,我们一般使用json字符串做为数据载体. public void decreaseCallMonitor(CallMonitorInfo c ...
- springboot+rabbitmq整合示例程
关于什么是rabbitmq,请看另一篇文: http://www.cnblogs.com/boshen-hzb/p/6840064.html 一.新建maven工程:springboot-rabbit ...
- springboot + rabbitmq 整合示例
几个概念说明:Broker:简单来说就是消息队列服务器实体.Exchange:消息交换机,它指定消息按什么规则,路由到哪个队列.Queue:消息队列载体,每个消息都会被投入到一个或多个队列.Bindi ...
- springboot+rabbitmq例子
demo目录 贴代码 1.ProducerConfig.java package com.test.config; import org.springframework.amqp.core.Bindi ...
随机推荐
- Java一次读取文本文件所有内容
转自https://www.cnblogs.com/longronglang/p/7458027.html#undefined 我们做文本处理的时候的最常用的就是读写文件了,尤其是读取文件,不论是什么 ...
- 【状压dp】Bzoj1231 [Usaco2008 Nov]mixup2 混乱的奶牛
Description 混乱的奶牛 [Don Piele, 2007] Farmer John的N(4 <= N <= 16)头奶牛中的每一头都有一个唯一的编号S_i (1 <= S ...
- 深入剖析最新IE0day漏洞
在2018年4月下旬,我们使用沙箱发现了IE0day漏洞;自从在野外发现上一个样本(CVE-2016-0189)已经有两年多了.从许多方面来看,这个特别的漏洞及其后续的开发比较有趣.下一篇文章将分析最 ...
- ASP.NET Core2.1 你不得不了解的GDPR(Cookie处理)
前言 时间一晃 ASP.NET Core已经迭代到2.1版本了. 迫不及待的的下载了最新的版本,然后生成了一个模版项目来试试水. ...然后就碰到问题了... 我发现..cookie竟然存不进去了.. ...
- 依赖注入容器-- Autofac
目录: 一.简介 二.如何使用 2.1.基本使用 2.2.接口使用 2.3. 其他注入 2.4. 注入的生命周期 一.简介 在上一篇文章中讲到替换默认服务容器,我们选择了Autofac Autofac ...
- 微信小程序 组件通信相关知识整理
1.自定义组件间通信与事件 https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/events.htm ...
- vue之$root,$parent
$root vue状态管理使用vuex,如果项目不大,逻辑不多,name我们没必要用vuex给项目增加难度,只需要用$root设置vue实例的data就行了,如下 main.js new Vue({ ...
- css常见的各种布局上(两列布局)
常见的布局上(两列布局) 1.常见的两列布局 1.1左边固定,右边自适应,左边宽度已知,右边默认占满整行,使用left 左浮动,右边不浮动,设置margin-left:左侧宽度 <style&g ...
- 看一眼就学会的 HTML 小游戏搭建!
本文作者:CODING 用户 - xfly 身边经常会有小伙伴问我有没有办法不买服务器也能上线自己的个人项目,比如不少同学都非常喜欢搭建一个属于自己的博客站点或者小游戏等. 目前相对比较简便的且不花自 ...
- openlayers4 入门开发系列之地图属性查询篇(附源码下载)
前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...