[英]How can I use one Java Object in two separate applications?
I have an application that create a statistics Object and update it through the execution.我有一个创建统计对象并通过执行更新它的应用程序。 The datas stored in that object are for example the date the application encountered the last disconnection, the number of message stored in the DB, etc...存储在该对象中的数据是例如应用程序遇到最后一次断开连接的日期、存储在数据库中的消息数量等......
I would like another app that is capable of for example triggering some alert if some of the datas exceed a certain value.我想要另一个能够在某些数据超过某个值时触发一些警报的应用程序。 Such as sending an email if there is no data stored in the DB for more than 5 minutes.例如,如果超过 5 分钟没有数据存储在数据库中,则发送电子邮件。
How can I accomplish that ?我怎样才能做到这一点? I don't know how to use the same Java Object with two different applications.我不知道如何在两个不同的应用程序中使用相同的 Java 对象。 What are the different methods to do that and what is the best option ?有哪些不同的方法可以做到这一点,最好的选择是什么?
Java does not support sharing the same object between two instances. Java 不支持在两个实例之间共享同一个对象。 There are however many ways to share state between objects.然而,有很多方法可以在对象之间共享状态。
One solution is Hazelcast and certainly does the trick.一种解决方案是 Hazelcast,当然可以解决问题。 It also works when jvm instances are on different machines.当 jvm 实例位于不同的机器上时,它也可以工作。
When the jvm's are on the same machine you could (de)serialize your object in shared memory.当 jvm 位于同一台机器上时,您可以(反)序列化共享内存中的对象。 This is described here: Shared Memory between two JVMs此处描述: 共享内存在两个 JVM 之间
However, I would most likely advise you to use hazelcast because it is simple.但是,我很可能会建议您使用hazelcast,因为它很简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.