官方文档: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发送消息的更多相关文章

  1. Spring-boot JMS 发送消息慢的问题解决

    1:在<ActiveMQ 基于zookeeper的主从(levelDB Master/Slave)搭建以及Spring-boot下使用>(http://www.cnblogs.com/ys ...

  2. c# RabbitMQ 发送消息

    参考地址:<C#使用RabbitMQ> C#操作RabbitMQ需要引用RabbitMQ的DLL,地址是:http://www.rabbitmq.com/releases/rabbitmq ...

  3. RabbitMQ发送消息成功,但是接受不到消息

    commom模块为mq配置模块 分了多模块后消息队列无法自动创建,发现原因竟然是SpringBoot没有扫描到common模块内的配置类. 我们在XxxApplication启动类上添加@Compon ...

  4. 给RabbitMQ发送消息时,设置请求头Header。

    消费者的请求头 生产者设置请求头 由于消费者那里,@Payload是接受的消息体,使用了@Header注解,需要请求头,生产者这边就要设置请求头,然后rabbitTemplate再调用convertA ...

  5. SpringBoot Rabbitmq接收消息

    官网地址:https://docs.spring.io/spring-boot/docs/2.1.3.RELEASE/reference/htmlsingle/#boot-features-amqp ...

  6. springboot项目整合rabbitMq涉及消息的发送确认,消息的消费确认机制,延时队列的实现

    1.引入maven依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...

  7. springboot整合rabbitMq实现消息延时发送

    实现思路:利用mq的ttl设置消息失效时间 当达到设置时间后通过交换机到达死信队列中,消费者端绑定读取死信队列中信息来达到延时发送消息的功能. demo 如下: (1)在pom.xml 中引入rabb ...

  8. SpringBoot | 第三十八章:基于RabbitMQ实现消息延迟队列方案

    前言 前段时间在编写通用的消息通知服务时,由于需要实现类似通知失败时,需要延后几分钟再次进行发送,进行多次尝试后,进入定时发送机制.此机制,在原先对接银联支付时,银联的异步通知也是类似的,在第一次通知 ...

  9. springboot核心技术(五)-----消息(rabbitmq)

    消息 1. 大多应用中,可通过消息服务中间件来提升系统异步通信.扩展解耦能力 2. 消息服务中两个重要概念: 消息代理(message broker)和目的地(destination) 当消息发送者发 ...

随机推荐

  1. svn更改地址怎么办

    开发过程中有时会遇到服务器更换地址的情况,比如之前地址是 svn://www.aaa.com 后来换成了 svn://www.bbb.com 这时候怎么办呢?分客户端和服务器端2种情况处理 客户端: ...

  2. AngularJS 控制器其他实例

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  3. python显示灰度图

    import matplotlib import matplotlib.pyplot as plt %matplotlib inline im=plt.imread('../lena.jpg', py ...

  4. linux下jdk的安装配置

    1.下载jdk:地址 选中你选择的版本,下载linux版本对应你系统的32位或64位. 我这里选择的是64位. 2.使用你的ssh直连工具把安装包丢到/usr/local/目录下 3.解压安装jdk ...

  5. C# grid控件用数据库分页后台怎么写?

    C#grid控件使用数据库分页的写法如下: mySystem.GetDataa(gridName.PageIndex *gridName.PageSize + 1, (gridName.PageInd ...

  6. JDK 动态代理 讨债实例

    现弄一个讨债接口 package cn.itcast.g_dongtaidaili; public interface Taozhai { void taozhai(); } 再弄一个讨债实现类 pa ...

  7. 模拟ie9的placeholder

    ie9 的input框没有placeholder属性 啧啧啧~~~ 所以就用span标签来模拟一下 先判断浏览器类型 if(navigator.useAgent.indexOf("MSIE ...

  8. MySQL主从复制读写分离如何提高从库性能-实战

    在做主从读写分离时候,需要注意主从的一些不同参数设置,来提高从库的性能,提高应用读取数据的速度,这样做很有必要的. 做读写分离复制主从参数不同设置如下(需要根据自己应用实际情况来设置): parmet ...

  9. IDEA的使用方法(一)(IDEA基本快捷键)

    一个软件的快捷键显得尤为重要,接下来来讲讲快捷键 CTR+N 搜索类 CTR+SHIT+N 搜索文件 CTR+ALT+空格 代码提示(类似于 ALT+/) ALT+F7 查询在某处使用 CTR+Q 查 ...

  10. Docker自学纪实(六)搭建docker私有仓库

    docker的镜像仓库分两种:一种是从官方公有仓库拉取:还有就是自己搭建私有仓库.官方的镜像仓库是面对整个应用市场的:私有仓库一般用于公司内部,就是公司项目自身所需的镜像.搭建私有仓库有什么好处?私有 ...