互操作序列化消息的最佳方法是什么?

发布于 2024-09-05 00:17:43 字数 762 浏览 9 评论 0原文

我正在考虑对 spring-integration 提供消息序列化支持。这对于各种线路级传输实现保证传送非常有用,而且还允许与其他消息系统(例如通过AMQP)。

出现的根本问题是,在其有效负载和标头中包含 Java 对象的消息应转换为 byte[] 和/或写入流。 Java 自己的序列化显然不会解决这个问题,因为它不具有互操作性。我的偏好是创建一个接口,允许用户为参与序列化的所有对象实现所需的逻辑。

这意味着我不想要求客户端开发人员生成他的域代码,而是为需要它的对象定义一个序列化器。界面会是这样的:

public interface PayloadSerializer<T> {
    byte[] bytesForObject(T source);
    T objectFromBytes(byte[]);
    //similar methods for streaming potentially
}

//add HeaderSerializer, MessageSerializer

这是一个明智的想法吗?完美的界面是什么样的?是否有一种标准的可互操作方法来序列化在这种情况下有意义的对象?

I'm considering message serialization support for spring-integration. This would be useful for various wire level transports to implement guaranteed delivery, but also to allow interoperability with other messaging systems (e.g. through AMQP).

The fundamental problem that arises is that a message containing Java object in it's payload and headers should be converted to a byte[] and/or written to a stream. Java's own serialization is clearly not going to cut it because that is not interoperable. My preference would be to create an interface that allows the user to implement the needed logic for all Objects that take part in serialization.

This means I don't want to require the client developer to generate his domain code, but rather define a serializer for objects that need it. The interfaces would be something like:

public interface PayloadSerializer<T> {
    byte[] bytesForObject(T source);
    T objectFromBytes(byte[]);
    //similar methods for streaming potentially
}

//add HeaderSerializer, MessageSerializer

Is this a sensible idea and what would the perfect interface look like? Is there a standard interoperable way to serialize Objects that would make sense in this context?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦屿孤独相伴 2024-09-12 00:17:43

java 中有一整套生成 XML 的框架,例如 JaxB,...现在其中一些格式可能只是二进制 blob,并且很难在其他平台上使用,因此在购买之前尝试一下是值得的。还有非常易于使用的 XML 序列化器。

JSON 如今非常流行,因为它可以轻松地与浏览器进行互操作,并且比 XML 更易读且更简洁。

当性能很重要时,Protocol Buffers 和 Thrift 很受欢迎。这些是二进制格式,但经过明确指定并在多个平台上得到良好支持。

There is a whole set of frameworks in java which generate XML, like JaxB, ... Now some of these formats might just as well be binary blobs and are difficult to use from other platforms, so it pays to try before you buy. There are also very easy to use XML serializers.

JSON is very popular nowadays because it gives easy interop with the browser and it is readable ad less verbose than XML.

Protocol Buffers and Thrift are popular when performance is important. These are binary formats but well specified and well supported on multiple platforms.

生死何惧 2024-09-12 00:17:43

我会尝试将 java 对象序列化为 XML 表示形式,并将其转换为流 I/O 的字节数组。

I would try to serialize the java objects into a XML representation and convert this into a byte array for stream I/O.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文