How to share object between java applications?
Asked Answered
H

6

9

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 way to achieve this without having any permanent storage?

Heliogravure answered 14/7, 2010 at 9:12 Comment(1)
B
8

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 Communication or Java messaging service.

Barbican answered 14/7, 2010 at 9:35 Comment(0)
Z
4

You can use 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!

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

Zachariah answered 14/7, 2010 at 9:22 Comment(1)
of course it won't. Then there should be something like shared memory. A shared memory example: unserializableone.blogspot.com/2006/10/…Zachariah
T
4

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<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, IdGenerator etc. The documentation is good and in my experience the implementation is solid.

Tickler answered 1/1, 2015 at 23:23 Comment(1)
hazelcast is quite easy to setup. however be careful of the underlying limitations (like clusters over 150 members, or data consistency)Saga
T
3

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. RMI is a good approach.

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 said or even you can use:

  • 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...

Tulatulip answered 14/7, 2010 at 9:48 Comment(0)
K
2

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.

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

Kamseen answered 14/7, 2010 at 9:23 Comment(0)
P
0

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

Phial answered 14/7, 2010 at 9:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.