JMS / ActiveMQ:发送带有对象作为类成员的对象 [英] JMS / ActiveMQ: Sending an object with objects as class members
问题描述
我正在使用ActiveMQ(带有Spring)将消息发送到远程OSGi-Container。
这很好用,但是有一个问题。
I'm using ActiveMQ (with Spring) for sending messages to remote OSGi-Container. This works very fine, but there is one issue.
我有两个实现Serializable的类。一个类是另一个类的类成员,例如:
I got two classes implementing Serializable. One class is the class member of the other class, like this:
public class Member implements Serializble {
private int someValue;
private static final long serialVersionUID = -4329617004242031635L;
... }
public class Parent implements Serializable {
private static final long serialVersionUID = -667242031635L;
private double otherValue;
private Member;
}
因此,当发送Parent实例时,Parent的成员为null 。
So, when is send a Parent instance, the Member of the Parent is null.
希望您了解我的问题是什么:)
Hope you understand what my problem is :)
编辑:有趣的问题:我有一个Java。我类中的util.date已正确序列化,但这是唯一的事情,所有Doubles均为空
funny issue: I got a java.util.date in my class which is serialized correctly, but this is the only thing, all Doubles etc are null
推荐答案
序列化对象字节消息中的消息有点难处理。
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.
这篇关于JMS / ActiveMQ:发送带有对象作为类成员的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!