记一次springboot整合rabbitMQ的list序列化问题
问题:平时传一个类的时候都会继承Serializable实现正确传输,这次我把list<Object>直接丢成了message,导致rabbitMQ不能序列化对象。报错
org.springframework.amqp.rabbit.support.ListenerExecutionFailedException: Failed to convert message
下面的整合步骤提供了解决方案:
在发送时,将list对象序列化能json字符串再发生
在接收时,反序列化中,由于java的类型擦除,我使用了TypeReference指定list的类型。当然这里需要保证其中的object是可序列化/反序列化的
springboot整合rabbitMQ的步骤:
1.引入pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2.添加yaml配置
spring:
rabbitmq:
host: 192.168.88.95
username: sunshine
password: sunshine
3.创建生产者消费者类
生产者
@Component
public class Producer {
@Autowired
private RabbitTemplate rabbitTemplate; public void send(String exchange, String routingKey, List<HistoryQuestionByAi> message){
ObjectMapper objectMapper=new ObjectMapper();
try{
String jsonToString=objectMapper.writeValueAsString(message);
rabbitTemplate.convertAndSend(exchange,routingKey,jsonToString);
}catch (Exception e){
throw new RuntimeException("消息发送失败");
} } }
消费者
@Component
@Slf4j
public class Consumer {
@Autowired
ESService esService;
@RabbitListener(queues = "writeInEsQueue")
public void handlerMessage(String message){
log.info("接收到消息:{}",message);
ObjectMapper objectMapper=new ObjectMapper();
try {
List<HistoryQuestionByAi>list=objectMapper.readValue(message, new TypeReference<List<HistoryQuestionByAi>>() {}); esService.addES(list);
}catch (Exception e) {
throw new RuntimeException(e);
}
}
}
配置类
@Configuration
public class RabbitMQConfig {
@Bean
public Queue writeInEsQueue(){
return new Queue("writeInEsQueue");
} @Bean
public Exchange writeInEsExchange(){
return new DirectExchange("writeInEsExchange");
} @Bean
public Binding binding(Queue writeInEsQueue,DirectExchange writeInEsExchange){
return BindingBuilder.bind(writeInEsQueue).to(writeInEsExchange).with("writeInEsKey");
}
}
记一次springboot整合rabbitMQ的list序列化问题的更多相关文章
- springboot学习笔记-6 springboot整合RabbitMQ
一 RabbitMQ的介绍 RabbitMQ是消息中间件的一种,消息中间件即分布式系统中完成消息的发送和接收的基础软件.这些软件有很多,包括ActiveMQ(apache公司的),RocketMQ(阿 ...
- 【SpringBoot系列5】SpringBoot整合RabbitMQ
前言: 因为项目需要用到RabbitMQ,前几天就看了看RabbitMQ的知识,记录下SpringBoot整合RabbitMQ的过程. 给出两个网址: RabbitMQ官方教程:http://www. ...
- SpringBoot系列八:SpringBoot整合消息服务(SpringBoot 整合 ActiveMQ、SpringBoot 整合 RabbitMQ、SpringBoot 整合 Kafka)
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合消息服务 2.具体内容 对于异步消息组件在实际的应用之中会有两类: · JMS:代表作就是 ...
- 一篇学习完rabbitmq基础知识,springboot整合rabbitmq
一 rabbitmq 介绍 MQ全称为Message Queue,即消息队列, RabbitMQ是由erlang语言开发,基于AMQP(Advanced MessageQueue 高级消息队列协议 ...
- 【MQ中间件】RabbitMQ -- SpringBoot整合RabbitMQ(3)
1.前言说明 前面一篇博客中提到了使用原生java代码进行测试RabbitMQ实现多种交换机类型的队列场景.但是在项目中我们一般使用SpringBoot项目,而且RabbitMQ天生对于Spring的 ...
- 功能:SpringBoot整合rabbitmq,长篇幅超详细
SpringBoot整合rabbitMq 一.介绍 消息队列(Message Queue)简称mq,本文将介绍SpringBoot整合rabbitmq的功能使用 队列是一种数据结构,就像排队一样,遵循 ...
- springboot整合rabbitmq实现生产者消息确认、死信交换器、未路由到队列的消息
在上篇文章 springboot 整合 rabbitmq 中,我们实现了springboot 和rabbitmq的简单整合,这篇文章主要是对上篇文章功能的增强,主要完成如下功能. 需求: 生产者在启 ...
- Springboot 整合RabbitMq ,用心看完这一篇就够了
该篇文章内容较多,包括有rabbitMq相关的一些简单理论介绍,provider消息推送实例,consumer消息消费实例,Direct.Topic.Fanout的使用,消息回调.手动确认等. (但是 ...
- RabbitMQ入门到进阶(Spring整合RabbitMQ&SpringBoot整合RabbitMQ)
1.MQ简介 MQ 全称为 Message Queue,是在消息的传输过程中保存消息的容器.多用于分布式系统 之间进行通信. 2.为什么要用 MQ 1.流量消峰 没使用MQ 使用了MQ 2.应用解耦 ...
- SpringBoot 整合 RabbitMQ 实现消息可靠传输
消息的可靠传输是面试必问的问题之一,保证消息的可靠传输主要在生产端开启 comfirm 模式,RabbitMQ 开启持久化,消费端关闭自动 ack 模式. 环境配置 SpringBoot 整合 Rab ...
随机推荐
- Android 13 - Media框架(29)- MediaCodec(四)
关注公众号免费阅读全文,进入音视频开发技术分享群! 上一节我们了解了如何通过 onInputBufferAvailable 和 getInputBuffer 获取到 input buffer inde ...
- Json.Net Deserialize a Collection from BSON
Deserialize a Collection from BSON (newtonsoft.com) This sample sets ReadRootValueAsArray to true so ...
- itest(爱测试) 开源接口测试,敏捷测试管理平台10.0.0GA 发布
一:itest work 简介 itest work 开源敏捷测试管理,包含极简的任务管理,测试管理,缺陷管理,测试环境管理,接口测试,接口Mock,还有压测 ,又有丰富的统计分析,8合1工作站.可按 ...
- c# HttpWebRequest 解决 请求HTTPS慢
其实就几行代码 if (strUrl.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { request.Cred ...
- 第一个java的应用程序
编写java第一个程序 class HelloWorld { public static void main(String[] args){ System.out.print("Hello ...
- ctfshow-超详细通关教程-web(1~8)
快捷目录 web1 web2 web3 web4 web5 web6 web7 web8 1.web签到题 打开网址后出现如下界面. 查看一下网站源码 将Y3Rmc2hvd3s1ZjkxNTc3Yy0 ...
- (编程语言界的丐帮 C#).NET Framework 读取Excel到DataTable
(编程语言界的丐帮 C#).NET Framework 读取Excel到DataTable 生成DataTable到Excel,支持 2007 .xlsx,2003 .xls. nuget 引用 NP ...
- nexus服务启动/关闭命令
nexus服务启动/关闭命令history | grep nexus # 查看服务器上面的历史请求命令ps -ef | grep nexus 查看进程及目录find / -name 'nexus' # ...
- mysql时间字段新增和修改默认时间,删除字段
mysql时间字段新增和修改默认时间,删除字段##新增字段ALTER TABLE tbl_test ADD COLUMN `create_time` DATETIME NULL DEFAULT CUR ...
- iOS开发流程总结(新坑持续更新)
[上线前流程] 将测试环境修改成正式环境 修改版本号,而且build version比version多一位,如version设置为1.0.1,那么build就设置成1.0.1.x:这么做的理由是,当你 ...