Extract Text Content from JMS Message

How to Extract Text Content from JMS Message

If the message body is a text message (Plain text or XML), it can be extracted like the following.

String msgBody = ((TextMessage) message).getText();

The JMS 2.0 API exposes the additional method in the Message Interface.

<T> T getBody(Class<T> c)

If the Message broker or the message source is JMS 2.0 complaint, then we can extract the message body in a much more clean way without object cast as follows.

String msgBody = message.getBody(String.class);

IBM MQ, ActiveMQ are the most popular JMS 2.0 compliant Message brokers.

Happy Coding..!!