SpringBoot Rabbitmq发送消息
官方文档:https://docs.spring.io/spring-boot/docs/2.1.3.RELEASE/reference/htmlsingle/#boot-features-amqp
引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
发送消息代码:
@RestController
@RequestMapping("/")
public class SenderMsgController { @Autowired
private AmqpTemplate amqpTemplate; @RequestMapping(value = "/{str}")
public void testSend(@RequestParam("str") String str) throws InterruptedException {
for (int i = 0; i < 10; i++) {
int millis = 500;
Thread.sleep(new Long(millis));
if (i%2 == 1) {
String isr = "{\n" + "\t\"dd\":" + i + "}"; amqpTemplate.convertAndSend("DirectExchange","test.1",isr,new MyMessageConverter()); }else{
String isr = "{\n" + "\t\"dd\":" + i + "}";
amqpTemplate.convertAndSend("DirectExchange","test.2",isr,new MyMessageConverter());
}
System.out.println("第"+i+"次发送");
}
}
}
MyMessageConverter:
//MessagePostProcessor 接口可以对发送请求之前的Message 进行操作,这里我设置了contenttype为json格式
public class MyMessageConverter implements MessagePostProcessor {
@Override
public Message postProcessMessage(Message message) throws AmqpException {
message.getMessageProperties().setContentType(MessageProperties.CONTENT_TYPE_JSON);
return message;
}
}
创建交换机、队列并互相绑定设置路由key
@Component
public class AmqpAdminConfig { @Autowired
public AmqpAdmin amqpAdmin; @Bean
public DirectExchange createDirectExchange(){
DirectExchange directExchange = new DirectExchange("DirectExchange", false, false);
amqpAdmin.declareExchange(directExchange);
return directExchange;
} // @Bean
// public void createFanoutExchange(){
// amqpAdmin.declareExchange(new FanoutExchange("FanoutExchange",false,false));
// } @Bean
public Queue createQueue1(){
Queue queue = new Queue("queue-1", false, false, false);
amqpAdmin.declareQueue(queue);
return queue;
} @Bean
public Queue createQueue2(){
Queue queue = new Queue("queue-2", false, false, false);
amqpAdmin.declareQueue(queue);
return queue;
} @Bean
public void createBinding1(){
Binding bind = BindingBuilder.bind(createQueue1()).to(createDirectExchange()).with("test.1");
amqpAdmin.declareBinding(bind);
}
@Bean
public void createBinding2(){
Binding bind = BindingBuilder.bind(createQueue2()).to(createDirectExchange()).with("test.2");
amqpAdmin.declareBinding(bind);
} }
根据官方文档知道AmqpTemplate 和AmqpAdmin 已经自动配置,可直接注入使用,AmqpTemplate 封装了发送与接收的各种操作,AmqpAdmin 封装了针对交换机和消息队列的各种操作
SpringBoot Rabbitmq发送消息的更多相关文章
- Spring-boot JMS 发送消息慢的问题解决
1:在<ActiveMQ 基于zookeeper的主从(levelDB Master/Slave)搭建以及Spring-boot下使用>(http://www.cnblogs.com/ys ...
- c# RabbitMQ 发送消息
参考地址:<C#使用RabbitMQ> C#操作RabbitMQ需要引用RabbitMQ的DLL,地址是:http://www.rabbitmq.com/releases/rabbitmq ...
- RabbitMQ发送消息成功,但是接受不到消息
commom模块为mq配置模块 分了多模块后消息队列无法自动创建,发现原因竟然是SpringBoot没有扫描到common模块内的配置类. 我们在XxxApplication启动类上添加@Compon ...
- 给RabbitMQ发送消息时,设置请求头Header。
消费者的请求头 生产者设置请求头 由于消费者那里,@Payload是接受的消息体,使用了@Header注解,需要请求头,生产者这边就要设置请求头,然后rabbitTemplate再调用convertA ...
- SpringBoot Rabbitmq接收消息
官网地址:https://docs.spring.io/spring-boot/docs/2.1.3.RELEASE/reference/htmlsingle/#boot-features-amqp ...
- springboot项目整合rabbitMq涉及消息的发送确认,消息的消费确认机制,延时队列的实现
1.引入maven依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...
- springboot整合rabbitMq实现消息延时发送
实现思路:利用mq的ttl设置消息失效时间 当达到设置时间后通过交换机到达死信队列中,消费者端绑定读取死信队列中信息来达到延时发送消息的功能. demo 如下: (1)在pom.xml 中引入rabb ...
- SpringBoot | 第三十八章:基于RabbitMQ实现消息延迟队列方案
前言 前段时间在编写通用的消息通知服务时,由于需要实现类似通知失败时,需要延后几分钟再次进行发送,进行多次尝试后,进入定时发送机制.此机制,在原先对接银联支付时,银联的异步通知也是类似的,在第一次通知 ...
- springboot核心技术(五)-----消息(rabbitmq)
消息 1. 大多应用中,可通过消息服务中间件来提升系统异步通信.扩展解耦能力 2. 消息服务中两个重要概念: 消息代理(message broker)和目的地(destination) 当消息发送者发 ...
随机推荐
- kernighan lin算法
这个算法主要用在网络节点的分割.他的思想是将一个网络节点图分割成两个相等的节点集合.为了连接两个社区的边权最小. step1:随机产生两个节点的集合A和B. step2:计算A和B中的每个节点的int ...
- NodeJS中常见异步接口定义(get、post、jsonp)
越来越多的人在使用nodeJS,作为一门服务端语言,我们不可避免的要写异步接口(ajax和jsonp).再次强调ajax和jsonp是两个概念,但是由于jquery的封装,使这两种异步接口的调用方式, ...
- 第45章 DCMI—OV2640摄像头—零死角玩转STM32-F429系列
第45章 DCMI—OV2640摄像头 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com ...
- lambda表达式的使用
lambda表达式和可遍历的datatable结合使用,把表中某一列中的数据转成字符串,用“|”隔开,代码如下: obj = tableName.AsEnumerable();if(tableName ...
- 简单webservice实现(xFire1.2)
基于xfire实现webservice的实例 首先下载xfire的jar包,并导入项目当中 下载地址:http://xfire.codehaus.org/Download 编写实现类 首先建一个接口把 ...
- kruscal 模板
/* Kruskal模板 */ struct edge{ int from; int to; int next; int value; bool operator<(const edge a) ...
- activemq启动闪退/失败 (Illegal character in hostname at index 5: ws://****:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600)
java.net.URISyntaxException: Illegal character in hostname at index 5: ws://****:61614?maximumConnec ...
- tcp回显客户端发送的数据
客户端: import socket tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) tcp_socket.connect ...
- datatable常用设置
bSort: false, // 是否排序功能 bFilter: false, // 过滤功能 bPaginate: true, // 翻页功能 bInfo: true, // 页脚信息 bProce ...
- 【转载】最长回文字符串(manacher算法)
原文转载自:http://blog.csdn.net/lsjseu/article/details/9990539 偶然看见了人家的博客发现这么一个问题,研究了一下午, 才发现其中的奥妙.Stupid ...