I am a newbie to Apache ActiveMQ. From Producer I am sending following content:
{"senderPhNumber":"9986085716","sendingTime":"2015-07-20T22:11:24","spCode":"000001","customerName":"Vinod"}
Code from Producer
String text = messageStr;
TextMessage message = session.createTextMessage(text);
// Tell the producer to send the message
System.out.println("Sent message: " + text );
producer.send(message);
From Consumer, the message is of type ActiveMqMessage
. Consumer implements MessageListener
and inside onMessage()
I have the below code:
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();
}
}
}
If I print the message, it gets printed as below
Received: 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 = topic://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, compressed = 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, arrival = 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=localhost, orignalMessageId=ID:AtulGupta-PC-50417-1437403787762-3:1:1:1:1, originBrokerURL=tcp://AtulGupta-PC:61616}, readOnlyProperties = true, readOnlyBody = true, droppable = false, jmsXGroupFirstForConsumer = false}
How to retrieve the actual message content ?
I know this is old but here's what I do (essentially). The point is to cast to ActiveMQTextMessage
- not 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();
}
Have you tried casting the message to a TextMessage and then get the message using getText() API.
You can refer to the code below in the link: http://activemq.apache.org/hello-world.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With