简体   繁体   中英

Convert JSON message to javax.jms.ObjectMessage in ActiveMq

I have an ActimeMQ consumer which expects a message in javax.jms.ObjectMessage format. This message pojo has 5 string elements.

Now I am trying to write a message producer for this consumer in NodeJs. I am using stompit module

My current NodeJs code is

stompit.connect(connectOptions, function(error, client) {

if (error) {
    console.log('connect error ' + error.message);
    return;
} else {
    console.log("connected");
}

var sendHeaders = {
    'destination': '/queue/test',

    'transformation': 'jms-object-json'
};
var msg = new Object();
msg.val1 = "12";
msg.val2 = "test";
msg.val3 = "1";
msg.val4 = "1";
msg.val5 = "Y";
var frame = client.send(sendHeaders);
frame.write(JSON.stringify(msg));
frame.end();

}); Java consumer is able to get the message but throws the exception

org.apache.activemq.command.ActiveMQTextMessage cannot be cast to javax.jms.ObjectMessage

I have read this page from activeMQ which says that

Currently, ActiveMQ comes with a transformer that can transform XML/JSON text to Java objects, but you can add your own transformers as well

I didn't quite understand this part on how to convert data.

I have added xstream-1.4.10.jar and jettison-1.3.8.jar in apache-activemq-5.15.0\\lib and restarted the ActiveMq server. But still I get the error in the consumer. Also in the ActiveMQ console -> Queues -> message properties, it shows transformation-error

Please let me know how I can convert this ActiveMQTextMessage type to javax.jms.ObjectMessage before it reaches the consumer

There isn't a transformer in ActiveMQ that will convert any random JSON string into and ObjectMessages, you'd have to write you own to handle whatever format you are sending. The converter in ActiveMQ will convert some basic types that Map from the JSON but it's tricky and not necessarily reliable. You are better off handling the TextMessage and doing something meaningful with the JSON yourself.

ActiveMQTextMessage and ObjectMessage are different , they can't cast to each other.

From ActiveMQTextMessage , you can get the true message content as a String, then you have to trans it to a json object yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM