2015-07-20 395 views
2

我是Apache ActiveMQ的新手。從生產者我發送以下內容:如何從ActiveMqMessage獲取消息文本

{"senderPhNumber":"9986085716","sendingTime":"2015-07-20T22:11:24","spCode":"000001","customerName":"Vinod"} 

代碼從生產者

String text = messageStr; 
TextMessage message = session.createTextMessage(text); 

// Tell the producer to send the message 
System.out.println("Sent message: " + text); 
producer.send(message); 

從消費,該消息是ActiveMqMessage類型。消費者實現MessageListener和內部onMessage()我有以下代碼:

public void onMessage(Message msg) { 
    if (msg instanceof ActiveMQMessage){ 
     System.out.println("Inside If"); 
     try { 
      ActiveMQMessage aMsg = (ActiveMQMessage)msg; 

      System.out.println(" Inside Listener ..." + aMsg); 

      ProducerInfo prod = (ProducerInfo) aMsg.getDataStructure(); 
      consumer.close(); 
      session.close(); 
      connection.close(); 
     } catch (JMSException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

如果我打印消息,它被打印爲如下

收稿:ActiveMQMessage {commandId = 0,responseRequired =假, MESSAGEID = ID:AtulGupta-PC-50395-1437403689355-1:1:0:0:13, originalDestination = null,originalTransactionId = null,producerId = ID:AtulGupta-PC-50395-1437403689355-1:1:0:0 ,目的地= 主題://ActiveMQ.Advisory.MessageDelivered.Topic.atul,交易ID = 空,到期= 0,時間戳= 0,到達= 0,brokerInTime = 1437403796470,brokerOutTime = 1437403796470,的correlationID = NULL, 的replyTo = NULL,持久=假,類型=諮詢,優先權= 0, 組ID = null,groupSequence = 0,targetConsumerId = null,壓縮 = false,userID = null,content = null,marshalledProperties = org.apache.activemq.[email protected],dataStructure = ActiveMQTextMessage {commandId = 5,responseRequired = false, messageId = ID:AtulGupta-PC-50417-1437403787762-3:1:1:1:1, originalDestination = null,originalTransactionId = null,producerId = ID:AtulGupta-PC-50417-1437403787762-3:1:1 :1,目的地= 主題:// atul,transactionI d = null,expiration = 0,timestamp = 1437403796468,arrival = 0,brokerInTime = 1437403796470, brokerOutTime = 0,correlationId = null,replyTo = null,persistent = false,type = null,priority = 4,groupID = null ,groupSequence = 0, targetConsumerId = NULL,壓縮=假,用戶ID = NULL,含量= 空,marshalledProperties =空,數據結構= NULL, redeliveryCounter = 0,大小= 0,屬性= NULL,readOnlyProperties =假,readOnlyBody = false,droppable = false,jmsXGroupFirstForConsumer = false,text = null},redeliveryCounter = 0,size = 0,properties = {originBrokerId = ID:AtulGupta-PC-50395-1437403689355-0:1, orignalDestination = ID: AtulGupta-PC-50417-1437 403787762-3:1:1:1:1, originBrokerName =本地主機, orignalMessageId = ID:AtulGupta-PC-50417-1437403787762-3:1:1:1:1, originBrokerURL = TCP:// AtulGupta-PC :61616},readOnlyProperties = TRUE, readOnlyBody = true時,可放開=假,jmsXGroupFirstForConsumer = 假}

如何檢索實際消息內容?

回答

0

您是否嘗試將消息轉換爲TextMessage,然後使用getText()API獲取消息。

你可以參考下面的代碼中的鏈接: http://activemq.apache.org/hello-world.html

+0

我試過了,但是當我嘗試(消息instanceof TextMessage)返回false。 –

+0

您可以發佈您在消費方如何接收消息的代碼片段。它應該按照你所說的去做。 –

1

我知道這是舊的,但這裏是我做的(基本上)。重點是投到ActiveMQTextMessage - 不是ActiveMQMessage

@Override 
public void onMessage(Message message) { 
    if (message instanceof ActiveMQTextMessage) { 
     ActiveMQTextMessage textMessage = (ActiveMQTextMessage) message; 
     try { 
      handleMessage(textMessage); 
     } catch (Exception e) { 
      LOG.error("ActiveMQTextMessage handling failed", e); 
     } 
    } else { 
     LOG.error("Message is not a text message " + message.toString()); 
    } 
} 

public static void handleMessage(ActiveMQTextMessage message) throws JMSException { 
    try { 
     String json = message.getText(); 
     DoSomethingWithJSON(json); 
    } catch (Exception e) { 
     System.out.println("Could not extract data to log from TextMessage"); 
     throw e; 
    } 

    mongoClient.close(); 
}