How to share object between java applications?

前端 未结 6 1752
暖寄归人
暖寄归人 2020-12-11 03:53

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.

What is the simplest

6条回答
  •  执笔经年
    2020-12-11 04:31

    I think that Hazelcast works 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.

    // Code in process 1
    Config cfg = new Config();
    HazelcastInstance instance = Hazelcast.newHazelcastInstance(cfg);
    Map 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 sharedData = instance.getMap("shared");
    String theSharedString = sharedData.get(1);
    

    Hazelcast support various shared data structures including Map, Queue, List, AtomicLong, IdGenerator etc. The documentation is good and in my experience the implementation is solid.

提交回复
热议问题