且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JMS / ActiveMQ:发送带有对象作为类成员的对象

更新时间:2023-02-09 14:45:40

序列化对象字节消息中的消息有点难处理。

Serialized objects in byte messages are a bit hard to deal with.

我会使用对象消息,如阿克塞尔·威尔格特(Aksel Willgert)所建议的那样,或者只是将其带入更松散的耦合格式,例如作为序列化的XML。我的快速解决方案是使用XStream以更宽松的耦合方式访问XML,或从XML接收XML,这是此处的快速指南: XStream

I would go with object messages, as Aksel Willgert suggested, or simply take it to some more loosley coupled format, such as serialzied XML. I quick solution would be to use XStream to go to/from XML in a more loosely coupled fashion, a quick guide here: XStream

更新,以及此处的一些代码(需要将xstream-.jar添加到您的项目中)

Update, and some code here (need to add xstream-.jar to your project)

// for all, instanciate XStream
XStream xstream = new XStream(new StaxDriver());

// Producer side:
TextMessage message = session.createTextMessage(xstream.toXML(mp));
producer.send(message);


// consumer side:
TextMessage tmsg = (TextMessage)msg;
Parent par = (Parent)xstream.fromXML(tmsg.getText());

par.getMember() // etc should work just fine.