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) 当消息发送者发 ...
随机推荐
- @NotEmpty@NotNull和@NotBlank的区别
这几个可以为对象,不只是字符串 1.@NotNull 不能为null,但可以为empty (""," "," ") 2.@NotEmpty ...
- laravel 去掉index.php伪静态
1,首先,让apache服务器支持rewrite 可以在apache配置文件中定义rewrite规则,是全局的,无论哪个应用都实用 //httpd.config Listen 80 RewriteEn ...
- 谈谈JDK8中的字符串拼接
字符串拼接问题应该是每个Java程序员都熟知的事情了,几乎每个Java程序员都读过关于StringBuffer/StringBuilder来拼接字符串. 在大多数的教程中,也许你会看到用+号拼接字符串 ...
- 分享spring、spring boot、spring cloud一些学习资源,从基础知识到项目实战
1.spring注解驱动开发,学习spring boot和spring cloud必备知识 链接: https://pan.baidu.com/s/1xhULzLlpkERhoMi1G5Lgfg 密码 ...
- 神经网络系列学习笔记(一)——神经网络之ANN学习资料汇总
ANN tutorial: http://adventuresinmachinelearning.com/neural-networks-tutorial/ https://www.cs.toront ...
- html颜色实体符号表示汇总
颜色的表示方法有许多种,列如black,#000000,rgb(0,0,0)都表示黑色.这三种表示方法分别为英文,十六进制,rgb格式.拥有下列颜色,足以使你的网页充满生机. 颜色名 十六进制颜色值 ...
- Teaching Is a Fruitful Way to Learn【教学是一种有效的学习方式】
Teaching Is a Fruitful Way to Learn For thousands of years, people have known that the best way to u ...
- Pandas 数据结构Dataframe:基本概念及创建
"二维数组"Dataframe:是一个表格型的数据结构,包含一组有序的列,其列的值类型可以是数值.字符串.布尔值等. Dataframe中的数据以一个或多个二维块存放,不是列表.字 ...
- 笔记-unittest实战
笔记-unittest实战 1. 框架图 2. 用例 编写自己的测试用例类,继承于基类 class ApiTestCase(unittest.TestCase): setUp方法会 ...
- Eclipse字体修改
第一步: 第二步: 第三步: 第四步: 第五步: 第六步: