java 将字符串转换为 javax.jms.Message

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7231609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:13:33  来源:igfitidea点击:

Convert String to javax.jms.Message

javastringjms

提问by Joe

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?

我正在开发一个 JMS 应用程序。我面临着需要将 XML 转换为 javax.jms.Message 对象的场景。有什么办法吗?

回答by skaffman

You can use createTextMessageon javax.jms.Session, e.g.

您可以使用createTextMessageon javax.jms.Session,例如

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

回答by Ryan Stewart

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.

对于纯 JMS API,请参阅 skaffman 的回答。如果您碰巧混合使用了 Spring,那么发送 JMS 消息就会变得非常简单。只需调用JmsTemplate.convertAndSend()。将任何字符串传递给它,它会自动将其包装成一个 TextMessage。使用 Spring几乎任何 JMS 交互都容易得多

回答by jFrenetic

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

它实际上可能取决于您的 JMS 提供程序。我们使用 IBM MQ 作为消息传递提供者,我记得我们是这样做的:

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

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

但我不确定这是否是正确的方法。