博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
RabbitMQ代码操作之发消息和序列化机制
阅读量:4329 次
发布时间:2019-06-06

本文共 2138 字,大约阅读时间需要 7 分钟。

几个自动配置类:

1.RabbitAutoConfiguration

2.有自动配置了连接工厂 ConnectionFactory
3.RabbitProperties 封装了RabbitMQ的配置
4.RabiitTemlate:给RabbitMQ发送和接收消息
5.AmqpAdmin:RabbitMQ系统管理功能组件(可以创建exchange,queue,Binding)
6.@EnableRabbit+@RabbitListener 监听消息队列的内容

 

  • 配置文件写法:
spring.rabbitmq.host=192.168.0.113spring.rabbitmq.username=guestspring.rabbitmq.password=guest#端口5672默认可以不写#spring.rabbitmq.virtual-host=  默认/可以不写
  • 测试类:
@SpringBootTestpublic class Springboot002AmqpApplicationTests {    @Autowired    RabbitTemplate  rabbitTemplate;    /*    1.单播(点对点)      public Message(byte[] body, MessageProperties messageProperties) {        this.body = body;        this.messageProperties = messageProperties;    }    * */    @Test    public void contextLoads() {        //交换器,路邮件,消息        //Message需要自己构造一个,定一消息体内容和消息头        //rabbitTemplate.send(exchange,routekey,message);        //转法并发送,Object默认当成消息体,只需要传入要发送的对象,自动序列化保存发送给rabbitmq        //rabbitTemplate.convertAndSend(exchange,routKey,object);        Map 
map = new HashMap<>(); map.put("msg","这是第一个消息"); map.put("data", Arrays.asList("helloWorld","123",true)); //对象被默认序列化后发送出去 //rabbitTemplate.convertAndSend("exchange.direct","springbootTest.news",map); //json发送MyAMQPConfig类配置 rabbitTemplate.convertAndSend("exchange.direct","springbootTest.news",new Book("西游记","吴承恩")); } //接收 @Test public void receive(){ Object o = rabbitTemplate.receiveAndConvert("springbootTest.news"); //打印数据类型 System.out.println(o.getClass()); System.out.println(o); }/** 1.单播* */ @Test public void setOneMsg(){ rabbitTemplate.convertAndSend("exchange.direct","springbootTest",new Book("水浒传","单播")); /* * 2.广播 * */ @Test public void setAllMsg(){ rabbitTemplate.convertAndSend("exchange.fanout","",new Book("红楼梦","曹雪芹")); }
发送消息时如不配置序列化方法则按照java默认序列化机制,则会造成发送编码不符合 解决方法:
json发送MyAMQPConfig类配置
@Configurationpublic class MyAMQPConfig {    @Bean    public MessageConverter messageConverter(){          return new Jackson2JsonMessageConverter();    }}

 

转载于:https://www.cnblogs.com/MagicAsa/p/10826596.html

你可能感兴趣的文章
excel第一次打开报错 向程序发送命令时出错 多种解决办法含终极解决方法
查看>>
响应式web设计之CSS3 Media Queries
查看>>
实验三
查看>>
机器码和字节码
查看>>
环形菜单的实现
查看>>
【解决Chrome浏览器和IE浏览器上传附件兼容的问题 -- Chrome关闭flash后,uploadify插件不可用的解决办法】...
查看>>
34 帧动画
查看>>
二次剩余及欧拉准则
查看>>
Centos 7 Mysql 最大连接数超了问题解决
查看>>
thymeleaf 自定义标签
查看>>
关于WordCount的作业
查看>>
C6748和音频ADC连接时候的TDM以及I2S格式问题
查看>>
UIView的layoutSubviews,initWithFrame,initWithCoder方法
查看>>
STM32+IAP方案 实现网络升级应用固件
查看>>
用74HC165读8个按键状态
查看>>
jpg转bmp(使用libjpeg)
查看>>
linear-gradient常用实现效果
查看>>
sql语言的一大类 DML 数据的操纵语言
查看>>
VMware黑屏解决方法
查看>>
JS中各种跳转解析
查看>>