How to exchange objects between Java applications?

I have 2 separate Java applications running at the same time. (Two separate javaw.exe) I need to split an object between them at runtime.

What is the easiest way to achieve this without persistent storage?

+6
java
source share
6 answers

Objects and their instance variables can be shared by threads in a Java program, which is pretty simple. If you need to exchange objects (instance) between two programs without data storage, the next choice will be using Rocket Socket Communication or Java messaging service.

+6
source share

You can use tcp

  • Use the local software port, say, local :. 999
  • Make one application as a server (listening on this port) and another as a client (connect to the server on localhost: 999, but you will use a different port for its own use).
  • The client will serialize your object in the stream.
  • The server is doing de-serialization!

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

+3
source share

You must decide whether you prefer a general and updated state, or simply send a one-time message object.

In the first case, you will have to share the "remote link" to an object. RMI is a good approach.

In the second case, you only need to serialize the object you want to split and send it. You can send it serialized (converted to bytes) via a socket as Ankit said, or even you can use:

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

  • Java Messaging Service (JMS), possibly brute force ...

  • some other creative but simple thing ...

+2
source share

I think Hazelcast is great for this type of situation. It practically does not require installation (moreover, you need to add dependencies to Hazelcast banks). The following code example shows how to configure a generic 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 supports various common data structures, including Map , Queue , List , AtomicLong , IdGenerator , etc. The documentation is good , and in my experience the implementation is solid.

+2
source share

If you cannot save the object forever, you need to somehow transfer it. This can be done either through the network or into some kind of shared memory.

For the first (network) approach, use serialization (java.io.Serializable) and move the object over the socket. This will require recording sockets.

The second approach will require the use and configuration of a third-party library (for example, EHCache ).

+1
source share

Maybe Oracle Coherence ? It works like a memory card distributed between applications.

0
source share

All Articles