java - 获取rabbitmq消息的body部分
问题描述
我为rabbit mq设置了一个简单的监听器
@RabbitListener(queues = SECOND_QUEUE)
public void onMessage(Message message) {
LOGGER.info("second queue listener.........");
LOGGER.info(message.toString());
}
这给出了这种格式的消息
(Body:'1460' MessageProperties [headers={}, timestamp=null, messageId=null, userId=null, receivedUserId=null, appId=null, clusterId=null, type=null, correlationId=null, correlationIdString=null, replyTo=null, contentType=application/x-java-serialized-object, contentEncoding=null, contentLength=0, deliveryMode=null, receivedDeliveryMode=PERSISTENT, expiration=null, priority=0, redelivered=false, receivedExchange=, receivedRoutingKey=bottomlesspit, receivedDelay=null, deliveryTag=1, messageCount=0, consumerTag=amq.ctag-PpEjG_BokAg-A4wllCgeqA, consumerQueue=bottomlesspit])
在控制台上。
我也有兴趣在客户端处理消息,我有这个
var onConnect = function() {
client.subscribe("/topic/messages", function(d) {
var str = d.body
var res = str.match(/Body:\'(.+)\'/);
console.log("I control this",res[1]);
});
};
我只想在 java 中获取 body,因为它是我目前唯一感兴趣的部分。
是否有一个已经在 java 和 stomp 上实现的功能,仅用于获取正文部分?
解决方案
message.getBody()
是你需要的 - 它会返回byte[]
,你需要知道你正在使用的消息格式进行转换:
@RabbitListener(queues = SECOND_QUEUE)
public void onMessage(Message message) {
byte[] body = message.getBody();
// do what you need with the body
}
推荐阅读
- java - 其他设备中的“未安装应用程序”
- java - java - 如何在运行时为哈希表分配键和值
- javascript - Div 没有与 CSS 和后退按钮正确对齐
- lotus-notes - 按多值字段分类的 Lotus Notes 视图导出多条记录
- c# - C# 中 Pipeline 和 PowerShell 类的区别
- excel - excel公式,按月求和错误
- angular - 离子填充SVG根据选择动态
- spring-boot - spring boot 悲观锁如何实现,乐观锁好还是悲观锁好?
- python - 您如何恢复或自动排列窗口并启动虚拟环境以使用 Ubuntu 进行开发?
- python - SqlAlchemy 无法在未映射的子类上设置属性