java 如何在java应用程序之间共享对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3244755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 00:58:11  来源:igfitidea点击:

How to share object between java applications?

javaobject-sharing

提问by Vishal

I have 2 separate Java Applications running at a time. (Two separate javaw.exe) I need to share an object between them while they are running.

我一次运行 2 个独立的 Java 应用程序。(两个独立的 javaw.exe)我需要在它们运行时在它们之间共享一个对象。

What is the simplest way to achieve this without having any permanent storage?

在没有任何永久存储的情况下实现这一目标的最简单方法是什么?

采纳答案by Vishal

Objects and their instance variables can be shared between threads in a Java program, which is pretty simple task.
If you require to share objects (instance of it) between two programs, with out data storage, next choise would be using RMI Socket Communicationor Java messaging service.

对象及其实例变量可以在 Java 程序中的线程之间共享,这是一项非常简单的任务。
如果您需要在两个程序之间共享对象(它的实例),而没有数据存储,那么下一个选择是使用RMI 套接字通信或 Java 消息传递服务。

回答by wassgren

I think that Hazelcastworks fine for this type of situation. It practically requires no setup (more than that you need to add the dependencies to the Hazelcast jars). The following code sample shows how to setup a shared Map.

我认为Hazelcast适用于这种情况。它实际上不需要设置(更多的是您需要将依赖项添加到 Hazelcast jars)。以下代码示例展示了如何设置共享Map.

// Code in process 1
Config cfg = new Config();
HazelcastInstance instance = Hazelcast.newHazelcastInstance(cfg);
Map<Integer, String> sharedData = instance.getMap("shared");
sharedData.put(1, "This is shared data");

// Code in process 2
Config cfg = new Config();
HazelcastInstance instance = Hazelcast.newHazelcastInstance(cfg);
Map<Integer, String> sharedData = instance.getMap("shared");
String theSharedString = sharedData.get(1);

Hazelcast support various shared data structures including Map, Queue, List, AtomicLong, IdGeneratoretc. The documentation is goodand in my experience the implementation is solid.

Hazelcast支持各种共享数据结构包括MapQueueListAtomicLongIdGenerator的文档是好的,在我的经验中实现是固体。

回答by ankitjaininfo

You can use TCP

你可以使用 TCP

  1. Use a local software port, say localhost:999.
  2. Make one application as server (listening on this port) and other as client (will connect to server at localhost:999, but will use different port for it's own use).
  3. Client will serialize your object to stream.
  4. Server does de-serialize!
  1. 使用本地软件端口,比如 localhost:999。
  2. 将一个应用程序设为服务器(在此端口上侦听),将另一个应用程序设为客户端(将连接到 localhost:999 处的服务器,但将使用不同的端口供自己使用)。
  3. 客户端将您的对象序列化为流。
  4. 服务器确实反序列化了!

Example: http://www.java-samples.com/showtutorial.php?tutorialid=1167

示例:http: //www.java-samples.com/showtutorial.php?tutorialid=1167

回答by mindas

If you can't store the object permanently, you need to transfer it somehow. This can be done either via network or some sort of shared memory.

如果您不能永久存储该对象,则需要以某种方式传输它。这可以通过网络或某种共享内存来完成。

For first (network) approach, use serialization (java.io.Serializable) and transfer the object over socket. This will require writing socket listeners.

对于第一种(网络)方法,使用序列化(java.io.Serializable)并通过套接字传输对象。这将需要编写套接字侦听器。

Second approach will require using and configuring a third party library (e.g. EHCache).

第二种方法需要使用和配置第三方库(例如EHCache)。

回答by helios

You must decide if you prefer shared and updated state, or simply send an one-time-message-object.

您必须决定是更喜欢共享和更新状态,还是只是发送一次性消息对象。

In the first case you would have to share a "remote reference" to some object. RMIis a good approach.

在第一种情况下,您必须共享对某个对象的“远程引用”。RMI是一个很好的方法。

In the second case you only need to serialize the object you want to share and send it. You can send it serialized (converted to byes) over a socket as Ankit saidor even you can use:

在第二种情况下,您只需要序列化要共享的对象并发送它。您可以像Ankit 所说的那样通过套接字发送序列化(转换为字节),甚至可以使用:

  • RMI :) The sender connects to a RMI registered receiver, invokes a method with the mesasge object as a param and forgets about the RMI object

  • Java Messaging Service (JMS), maybe an overkill...

  • some other creative but simple thing...

  • RMI :) 发送者连接到 RMI 注册的接收者,调用一个以消息对象作为参数的方法,并忘记了 RMI 对象

  • Java Messaging Service (JMS),也许有点矫枉过正……

  • 其他一些有创意但简单的事情......

回答by Noel M

Perhaps Oracle Coherence? That works like an in memory map shared across applications.

也许是Oracle Coherence?这就像跨应用程序共享的内存映射一样。