首页 > 解决方案 > 获取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 上实现的功能,仅用于获取正文部分?

标签: javarabbitmqspring-amqp

解决方案


message.getBody()是你需要的 - 它会返回byte[],你需要知道你正在使用的消息格式进行转换:

@RabbitListener(queues = SECOND_QUEUE)
public void onMessage(Message message) {
    byte[] body = message.getBody();
    // do what you need with the body
}

推荐阅读