saurabh mahajan
saurabh mahajan

Reputation: 41

How to Publish JSON Object on ActiveMQ

I am trying to publish JSON Message(Object) on to the ActiveMQ queue/topic. currently i am converting JSON object into String then publishing it. But i don't want to convert it into String.I don't want to convert it into String instead of that i want to send as it is JSON Object as a Message.

Below is my code

public void sendMessage(final JSONObject msg) {
        logger.info("Producer sends---> " + msg);
        jmsTemplate.send(destination, new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                String s = msg.toString();
                return session.createTextMessage(s);
                // createTextMessage(msg);
            }
        });
    }

Upvotes: 1

Views: 7739

Answers (1)

Petter Nordlander
Petter Nordlander

Reputation: 22279

Using text on the queue is best practice since you will be able to debug a lot easier as well as not being restricted to the exactly same language/framework or even version of the libraries on the applications on both sides of the queue.

If you really want that hard coupling (i.e. when you are using the queue inside a single application and don't need to inspect messages manually on the queues) you can do it:

instead of return session.createTextMessage(s); do return session.createObjectMessage(msg);

One more thing: Be aware that using JMS ObjectMessage may cause security issues if you don't have 100% control of the code posting messages. Therefore this is not allowed in default ActiveMQ settings. You need to enable this in both client and server settings. For reference, see this page: http://activemq.apache.org/objectmessage.html

Upvotes: 2

Related Questions