开发者

Convert String to javax.jms.Message

开发者 https://www.devze.com 2023-04-01 01:18 出处:网络
开发者_开发百科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

开发者_开发百科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, e.g.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号