使用 XStream 传输未知(可能非常大)数量的 Java 对象

发布于 2024-12-08 21:16:47 字数 425 浏览 1 评论 0原文

我正在尝试使用 XStream 在两个进程(本质上是 JVM)之间传输对象。我发现这个论坛以前也有人问过类似的问题(请参阅以下链接)。

使用 Java 序列化 XML 进行流式传输的最佳方法?

我的要求与上述问题中给出的解决方案之间的基本区别在于,正在流式传输的所有对象在流式传输操作开始时都不可用。我希望发送过程等待新对象并在它们到来时将它们流式传输。因此,此操作可能需要很长时间(两个对象的流传输之间有大量空闲时间)。

另一方面,如果我们一开始就知道所有集合元素,则上面给出的解决方案效果很好。

如果有人以前遇到过这样的要求并能够解决它,请告诉我。

I am attempting to use XStream to stream objects between two processes (which are JVMs essentially). I found a similar question was being asked on this forum before (please refer to following link).

Best approach to serialize XML to stream with Java?

The basic difference between my requirement and the solution given in the above question is that the all objects being streamed are not available at the beginning of the streaming operation. I want the sending process to wait for new objects and stream them over as and when they come. So this operation can potentially take long time (with lots of idle time between streaming of two objects).

On the other hand in the solution given above works great if we know all the collection elements at the beginning.

Please let me know if anyone encountered such requirement before and able to solve it.

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

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

发布评论

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

评论(1

十年九夏 2024-12-15 21:16:47

将问题分为两部分:

  1. 进程之间的通信/连接 通过
  2. 连接序列化每个单独的对象

对于第 1 部分,很难说您是要保持一个连接处于活动状态还是在有更多数据时打开一个新连接。多次打开新连接的成本可能会很高,因此,如果您发现成本太高,请使用一个连接。

对于第 2 部分,将对象放入其自己的文档中似乎比尝试将它们全部放入一个文档中更干净。使用 XStream 可以很简单地实现这一点。如果您需要类似于发送最外层文档的最终结束标记的“完成”概念,请在适当的时候发送自定义“完成”消息。

Break the problem into two parts:

  1. Communication/connectivity between the processes
  2. Serializing each individual object over the connection

For part 1, it's hard to say whether you want to keep one connection alive or open a new connection whenever there's more data. Opening a new connection many, many times can get expensive, so use one connection if you find this cost is too high.

For part 2, it seems cleaner to put object into its own document, rather than trying to fit them all into one. This should be straightforward to implement with XStream. If you need a notion of "done" that similar to sending the final closing tag of an outermost document, then send a custom "done" message when the time is right.

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