我是Apache ActiveMQ的新手。来自制片人我发送以下内容:
{"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 = false, 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,destination = 话题://ActiveMQ.Advisory.MessageDelivered.Topic.atul,transactionId = null,expiration = 0,timestamp = 0,arrival = 0,brokerInTime = 1437403796470,brokerOutTime = 1437403796470,correlationId = null, replyTo = null,persistent = false,type = Advisory,priority = 0, groupID = null,groupSequence = 0,targetConsumerId = null,压缩 = false,userID = null,content = null,marshalledProperties = org.apache.activemq.util.ByteSequence@46347456,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,destination = topic:// atul,transactionId = null,expiration = 0,timestamp = 1437403796468,到达= 0,brokerInTime = 1437403796470, brokerOutTime = 0,correlationId = null,replyTo = null,persistent = false,type = null,priority = 4,groupID = null,groupSequence = 0, targetConsumerId = null,compressed = false,userID = null,content = null,marshalledProperties = null,dataStructure = null, redeliveryCounter = 0,size = 0,properties = null,readOnlyProperties = false,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-1437403787762-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,droppable = false,jmsXGroupFirstForConsumer = 假}
如何检索实际的邮件内容?
答案 0 :(得分:3)
我知道这已经过时了,但这就是我的所作所为(基本上)。重点是转换为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();
}
答案 1 :(得分:1)
您是否尝试将消息转换为TextMessage,然后使用getText()API获取消息。
您可以参考以下链接中的代码: http://activemq.apache.org/hello-world.html