black
black

Reputation: 357

How do I make 2 Java applications talk with each other?

I have 2 Java applications. First I may edit as much as I wish, but I will compile it to machine code later on. Second one I am not able to edit, but I may write an addon for it. I need to make that addon be able to talk with first application. Generally simply send strings to each other. Input and Output streams of a process is not an option for me. I am thinking of using a tcp socket client/server or a file which will act as a buffer. But both ways seem a liitle bit ugly to me, could anyone propose me a better idea?

Upvotes: 2

Views: 2006

Answers (3)

Jatin
Jatin

Reputation: 31754

It depends on what kind of data you wish to transfer.

If it is only Strings, then: if number of process = 2 and if you are sure of it, then stdin &8 stdout is the best way forward. You can create a Process using ProcessBuilder and then get the streams to communicate. The other process can just to System.out to transfer message. This is preferred to Socket, because you dont have to handle graceful closing of socket etc. (In case it fails and the port is not un-binded successfully, it can be a big trouble)

if number of process > 2 and less than say 10, you can probably use Sockets and communicate through Socket. This should work well, though extra effort goes in gracefully managing sockets.

if number of process is Large, then JMS should be used. It does a lot of things which you dont need to handle. Too big a task if the number of processes are less.

So in your case, process is the best way forward.

If the data you wish to transfer, can even be Objects. RMI can be used given the number of processes are less. If more, use JMS again.

Edit: Now for all the above, there is a lot of dirty work involved. For a change, if you are looking at something new & exciting, I would advice akka. It is a actor based model which communicate with each other using Messages.

The beauty is, the actors can be on same JVM or another (very little config) and akka takes care the rest for you. I haven't seen a more cleaner way than doing this :)

Upvotes: 5

Satheesh Cheveri
Satheesh Cheveri

Reputation: 3679

Another approach is having DB table to store your data, one process can insert and other process can read it when ever required. When you are using JMS, there is likeliness of loosing data, But storing in db would be failsafe and future proof.

Upvotes: 0

i-developer
i-developer

Reputation: 416

What about to use JMS ?

You can use according to your needs, either the Publish/Sunbscribe or Point-to-Point Models.

Upvotes: 1

Related Questions