简体   繁体   中英

Convert String to javax.jms.Message

I'm working on a JMS application. I'm facing a scenario where I need to convert an XML in to javax.jms.Message object. Is there any way to do it?

You can use createTextMessage on javax.jms.Session , eg

String xml = ...
Session session = ...
Message message = session.createTextMessage(xml);

For pure JMS API, see skaffman's answer. If you happen to have Spring in the mix, it makes sending JMS messages really simple. Just call JmsTemplate.convertAndSend() . Pass it any String, and it will automatically wrap it up into a TextMessage. Pretty much any JMS interaction is much easier with Spring .

It actually might depend on your JMS provider. We used IBM MQ as messaging provider, and I remember that we did it like this:

com.ibm.jms.JMSTextMessage textMsg = new com.ibm.jms.JMSTextMessage();
textMsg.setText(yourText);

But I'm not sure if it's the correct way.

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