首页 > 解决方案 > 通过 ActiveMQ 代理插件获取 AMQ 消息

问题描述

我已经使用BrokerFilter类实现了一个自定义 ActiveMQ 代理插件。我已经覆盖了下面显示的发送方法。

public void send(final ProducerBrokerExchange producerExchange, final Message messageSend) throws Exception {

     logger.info("Message  : " + messageSend);
     // Returns the JMS Type of Mesage
     logger.info("Message Type  : " +  messageSend.getType());

     // Lookig for method to get the message text
        ?

}

第一行日志显示了对象中的消息文本,但似乎没有可用的方法来获取消息文本。

    INFO | Message : ActiveMQTextMessage {commandId = 5, responseRequired = false, messageId = ID:192.168.10.6-63132-1613444356003-4:1:1:1:1, originalDestination = null, originalTransactionId = null, producerId = ID:192.168.10.6-63132-1613444356003-4:1:1:1, destination = topic://reporting, transactionId = null, expiration = 0, timestamp = 1613444364819, arrival = 0, brokerInTime = 0, brokerOutTime = 0, correlationId = , replyTo = null, persistent = false, type = NewAgent, priority = 0, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = null, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false, jmsXGroupFirstForConsumer = false, **text = Hello** }

    INFO | getType : New

有人可以指导我使用适当的类或方法来获取/拦截消息文本吗?我的目标是获取消息文本/正文并将其存储在 redis 中。

标签: javaactivemq

解决方案


您需要弄清楚它是什么类型的消息并将其转换为从消息中获取文本(或其他内容)。尝试类似:

public void send(final ProducerBrokerExchange producerExchange, final Message messageSend) throws Exception {

  logger.info("Message  : " + messageSend);
  // Returns the JMS Type of Mesage
  logger.info("Message Type  : " +  messageSend.getType());
  logger.info("Message Class : " + messageSend.getClass().toString());
  // Lookig for method to get the message text
  
  if (messageSend instanceof ActiveMQTextMessage) {
    ActiveMQTextMessage txtMsg = (ActiveMQTextMessage)messageSend;
    Logger.info("Message Text : " + txtMsg.getText();
  }

}

短信文档。


推荐阅读