r/javahelp • • May 27 '19

Communication between 2 separately running java applications.

Just as the title implies, how can I share around some data from one java program to another running at the same time? I am trying using sockets but I am only able to establish a connection between a program and a local server but not sure how to connect the 2nd application to it. Edit : thanks everyone! I was using sockets previously so I needed to looking a simple chat example like you you suggested and was able to make 2 java classes one which was the client and the other server which is exactly what I needed!

28 Upvotes

12 comments sorted by

12

u/User1539 May 27 '19

Have one application act as the server, and the other act as the client.

4

u/Berserker74 May 27 '19

Ans how do I do that exactly? Any guides? Thanks!

9

u/User1539 May 27 '19

If you want to use sockets, I recommend looking up a simple chat example, then instead of sending a string of text, send JSON. Then just decide which of your applications will be the server, and which will be client. It doesn't really matter which.

9

u/Wiqkid May 27 '19

There are a few ways to tackle this depending on your needs, take a look at this: https://stackoverflow.com/questions/10942427/how-to-have-2-jvms-talk-to-one-another

8

u/_harro_ May 27 '19

What's the use case?

Depending on what you want to achieve, the solution can/will be different, because there are multiple ways to do this.

- You can indeed use sockets

- you can use simple REST call's

- Common Database for both application

- Messaging mechanism (ActiveMQ for example).

- ...

2

u/erjiin May 27 '19

That should be the highest response.

7

u/doodooz7 May 27 '19

How about through something like memcache or a db? Also maybe a messaging queue? You could make 2 channels and have each instance message each other

4

u/[deleted] May 27 '19

I would look at Java RMI. This makes it really easy for the one application to directly call another application via a Java method e.g. remoteSvc.doOperation(data).

If you want to decouple technologies, then use sockets, REST, DBs etc.

3

u/deathmetal27 Extreme Brewer May 27 '19

I think a common database is the simplest and best approach. Create a database and have both your applications query it for data.

One application can write data to a table and another can read data from the same table.

3

u/nidrach May 27 '19

RMI is pretty straight forward once you get your head around some concepts. Otherwise you could use sockets and Buffers or just a shared database or a REST api or whatever. It depends on what your applications should do.

4

u/bvn13 May 27 '19

It depends on how your applications must be coordinated: synchronously or asynchronously. If they need synchronization you might to choose sockets or http. The latter is simple to implement. If you choose async take a look at message queries: RabbitMQ, ActiveMQ, Kafka. Edit: Database is straightforward in async mode too.

2

u/ohjimmy May 27 '19 edited May 27 '19

Sometimes a stupid easy hack will do the trick. Have one application (A) write to a file. The other application (B) one could run in a tight loop (wake up ever 500 ms and see if the file exists) and poll to see if the file exists. If it does, read it.

To avoid having B read while A is writing, have A write to a temporary location and move the file to the expected location when it is finished writing. Generally the OS's file move operation is pretty atomic, and this should do the trick.

This technique is far from perfect, and isn't recommended for production level code, but is "good enough" for many uses, and a lot simpler than many of the other methods described, especially for someone new to java.