Communication between local JVMs
Asked Answered
V

11

26

My question: What approach could/should I take to communicate between two or more JVM instances that are running locally?

Some description of the problem:
I am developing a system for a project that requires separate JVM instances to isolate certain tasks from each other entirely.

In it's running, the 'parent' JVM will create 'child' JVMs that it will expect to execute and then return results to it (in the format of relatively simple POJO classes, or perhaps structured XML data). These results should not be transferred using the SysErr/SysOut/SysIn pipes as the child may already use these as part of its running.

If a child JVM does not respond with results within a certain time, the parent JVM should be able to signal to the child to cease processing, or to kill the child process. Otherwise, the child JVM should exit normally at the end of completing its task.

Research so far:
I am aware there are a number of technologies that may be of use e.g....

  • Using Java's RMI library
  • Using sockets to transfer objects
  • Using distribution libraries such as Cajo, Hessian

...but am interested in hearing what approaches others may consider before pursuing one of these options, or any others.

Thanks for any help or advice on this!

Edits:
Quantity of data to transfer- relatively small, it will mostly be just a handful of POJOs containing strings that will represent the result of the child executing. If any solution would be inefficient on larger amounts of information, this is unlikely to be a problem in my system. The amount being transferred should be pretty static and so this does not have to be scalable.

Latency of transfer- not a critical concern in this case, although if any 'polling' of results is needed this should be able to be fairly frequent without significant overheads, so I can maintain a responsive GUI on top of this at a later time (e.g. progress bar)

Villenage answered 19/2, 2011 at 16:48 Comment(2)
Perhaps you could talk about how much data you need to transfer and how latency critical it is? Unless you have significant performance constraints, whatever you consider to be the simplest solution may be best.Sammer
@Peter Lawrey I have added some extra detail for you, thanks!Villenage
C
5

I'd use KryoNet with local sockets since it specialises heavily in serialisation and is quite lightweight (you also get Remote Method Invocation! I'm using it right now), but disable the socket disconnection timeout.

RMI basically works on the principle that you have a remote type and that the remote type implements an interface. This interface is shared. On your local machine, you bind the interface via the RMI library to code 'injected' in-memory from the RMI library, the result being that you have something that satisfies the interface but is able to communicate with the remote object.

Curule answered 19/2, 2011 at 18:3 Comment(9)
That looks very interesting, and may indeed fulfil my needs. Are there are aspects you find it is lacking it at all, even if these don't currently cause you any issues?Villenage
Some of the issues are to do with registration of networked classes, and race conditions to do with initial registration.. but these can be overcome by making sure that method calls are done in the right order. Also, I've found that it saturates the connection sometimes when connecting (I'm running on a network simulator interface), and some of the errors it gives are confusing and hard to pin down to the actual cause.Curule
Another thing: it's hard to get the connection from which a method is being invoked, but I'm modding kryonet and adding in that functionality. If I just let clients lie about what connection they're coming from, all havoc might break loose.Curule
I just want to say how small source code code that enables RMI is here -- it's essentially 600 lines of code. Hardly anything. But it works really well.Curule
Another thing: it has no support for one-to-many RMI calls (so if you call a remote method, it calls the same method on 100 remote objects).Curule
A good thing is that Kryo is a serialisation library, so it's hugely flexible. You can write custom serialisers for classes and such.Curule
Thanks very much for all of that expansion- it was much more than I was expecting! Interesting that in general this question has attracted a lot of attention, even though I couldn't find many similar questions when searching prior to posting it! For my purposes, I need to be able to identify the clients to know what the result corresponds to but I can trust the client to report this accurately, so I believe this should be trivial?Villenage
Yes, this will be trivial for you -- just send the address or client ID as a field -- basically just add an extra parameter to the method in class and interface :)Curule
Exactly what I was thinking of doing. Luckily, whilst I cannot trust some of the code being run by client (and so must protect my main process against issues that seem difficult to otherwise handle such as OutOfMemory errors), I believe I will be able to isolate that from the communication to the parent :)Villenage
C
6

Not directly an answer to your question, but a suggestion of an alternative. Have you considered OSGI?

It lets you run java projects in complete isolation from each other, within the SAME jvm. The beauty of it is that communication between projects is very easy with services (see Core Specifications PDF page 123). This way there is not "serialization" of any sort being done as the data and calls are all in the same jvm.

Furthermore all your requirements of quality of service (response time etc...) go away - you only have to worry about whether the service is UP or DOWN at the time you want to use it. And for that you have a really nice specification that does that for you called Declarative Services (See Enterprise Spec PDF page 141)

Sorry for the off-topic answer, but I thought some other people might consider this as an alternative.

Update

To answer your question about security, I have never considered such a scenario. I don't believe there is a way to enforce "memory" usage within OSGI.

However there is a way of communicating outside of JVM between different OSGI runtimes. It is called Remote Services (see Enterprise Spec PDF, page 7). They also have nice discussion there of the factors to take into consideration when doing something like that (see 13.1 Fallacies).

Folks at Apache Felix (implementation of OSGI) I think have implementation of this with iPOJO, called Distributed Services with iPOJO (their wrapper to make using services easier). I've never used this - so ignore me if I am wrong.

Courtroom answered 19/2, 2011 at 18:16 Comment(1)
No need for the apologies, this is an interesting suggestion. One of the problems I face is that part of the code executed by the child JVM is untrusted, and so has to be restricted (e.g. through a security manager). As part of this, I need to restrict the memory usage to ensure the parent JVM has sufficient resources remain available. However, from what I can find I cannot restrict the memory usage within a JVM to prevent out of memory exceptions. Does OSGI offer any protection/regulation of this?Villenage
C
5

I'd use KryoNet with local sockets since it specialises heavily in serialisation and is quite lightweight (you also get Remote Method Invocation! I'm using it right now), but disable the socket disconnection timeout.

RMI basically works on the principle that you have a remote type and that the remote type implements an interface. This interface is shared. On your local machine, you bind the interface via the RMI library to code 'injected' in-memory from the RMI library, the result being that you have something that satisfies the interface but is able to communicate with the remote object.

Curule answered 19/2, 2011 at 18:3 Comment(9)
That looks very interesting, and may indeed fulfil my needs. Are there are aspects you find it is lacking it at all, even if these don't currently cause you any issues?Villenage
Some of the issues are to do with registration of networked classes, and race conditions to do with initial registration.. but these can be overcome by making sure that method calls are done in the right order. Also, I've found that it saturates the connection sometimes when connecting (I'm running on a network simulator interface), and some of the errors it gives are confusing and hard to pin down to the actual cause.Curule
Another thing: it's hard to get the connection from which a method is being invoked, but I'm modding kryonet and adding in that functionality. If I just let clients lie about what connection they're coming from, all havoc might break loose.Curule
I just want to say how small source code code that enables RMI is here -- it's essentially 600 lines of code. Hardly anything. But it works really well.Curule
Another thing: it has no support for one-to-many RMI calls (so if you call a remote method, it calls the same method on 100 remote objects).Curule
A good thing is that Kryo is a serialisation library, so it's hugely flexible. You can write custom serialisers for classes and such.Curule
Thanks very much for all of that expansion- it was much more than I was expecting! Interesting that in general this question has attracted a lot of attention, even though I couldn't find many similar questions when searching prior to posting it! For my purposes, I need to be able to identify the clients to know what the result corresponds to but I can trust the client to report this accurately, so I believe this should be trivial?Villenage
Yes, this will be trivial for you -- just send the address or client ID as a field -- basically just add an extra parameter to the method in class and interface :)Curule
Exactly what I was thinking of doing. Luckily, whilst I cannot trust some of the code being run by client (and so must protect my main process against issues that seem difficult to otherwise handle such as OutOfMemory errors), I believe I will be able to isolate that from the communication to the parent :)Villenage
S
5

akka is another option, as well as other java actor frameworks, it provides communication and other goodies derived from the actor model.

Sachiko answered 19/2, 2011 at 21:12 Comment(0)
U
4

If you can't use stdin/stdout, then i'd go with sockets. You need some sort of serialization layer on top of the sockets (as you would with stdin/stdout), and RMI is a very easy to use and pretty effective such layer.

If you used RMI and found the performance wasn't good enough, i'd switch to some more efficient serializer - there are plenty of options.

I wouldn't go anywhere near web services or XML. That seems like a complete waste of time, likely take more effort and deliver less performance than RMI.

Use answered 19/2, 2011 at 17:59 Comment(1)
Thank you, I believe this is the direction I was leaning towards. As the amount of data being transferred is relatively small, I believe I shouldn't face any real performance issues but I will keep a note of your link in case this does become an issue.Villenage
G
3

Not many people seem to like RMI any longer.

Options:

  1. Web Services. e.g. http://cxf.apache.org
  2. JMX. Now, this is really a means of using RMI under the table, but it would work.
  3. Other IPC protocols; you cited Hessian
  4. Roll-your-own using sockets, or even shared memory. (Open a mapped file in the parent, open it again in the child. You'd still need something for synchronization.)

Examples of note are Apache ant (which forks all sorts of Jvms for one purpose or another), Apache maven, and the open source variant of the Tanukisoft daemonization kit.

Personally, I'm very facile with web services, so that's the hammer which which I tend to turn things into nails. A typical JAX-WS+JAX-B or JAX-RS+JAX-B service is very little code with CXF, and manages all the data serialization and deserialization for me.

Guarneri answered 19/2, 2011 at 17:17 Comment(1)
Out of interest, where would your preference lie? I appreciate this might be a little difficult to say when you only have my short description of the project above.Villenage
T
3

It was mentioned above, but i wanted to expand a bit on the JMX suggestion. we actually are doing pretty much exactly what you are planning to do (from what i can glean from your various comments). we landed on using jmx for a variety of reasons, a few of which i'll mention here. for one thing, jmx is all about management, so in general it is a perfect fit for what you want to do (especially if you already plan on having jmx services for other management tasks). any effort you put into jmx interfaces will do double duty as apis you can call using java management tools like jvisualvm. this leads to my next point, which is the most relevant to what you want. the new Attach API in jdk 6 and above is very sweet. it enables you to dynamically discover and communicate with running jvms. this allows, for example, for your "controller" process to crash and restart and re-find all the existing worker processes. this is the makings of a very robust system. it was mentioned above that jmx basically rmi under the hood, however, unlike using rmi directly, you don't need to manage all the connection details (e.g. dealing with unique ports, discoverability, etc). the attach api is a bit of a hidden gem in the jdk, as it isn't very well documented. when i was poking into this stuff initially, i didn't know the name of the api, so figuring how the "magic" in jvisualvm and jconsole worked was very difficult. finally, i came across an article like this one, which shows how to actually use the attach api dynamically in your own program.

Tucker answered 20/2, 2011 at 3:5 Comment(4)
Attach api works only with child process id . once you spawn the child process, you need to get the process id.Argilliferous
@SureshSankar - you don't need to know the pid to attach to the process, but it does help.Tucker
if you do not know child process id, how are you gonna control or invoke methods in child process. let me know how to connect to child process without process id.Argilliferous
@SureshSankar - as i said in my answer, the attach api allows you to dynamically discover other java processes. if you have a way of identifying your child processes, you can find them without knowing their pid (this is how our product works).Tucker
C
2

Although it's designed for potentially remote communication between JVMs, I think you'll find that Netty works extremely well between local JVM instances as well.

It's probably the most performant / robust / widely supported library of its type for Java.

Correy answered 19/2, 2011 at 18:12 Comment(2)
Netty definitely seems to be very highly held, but it seems to me that its speciality is in slightly different problems. Have you ever attempted to use Netty in this way? Also, it seems that it may be slightly heavy-weight for my needs- from your experience, does it take significant investment to learn/use?Villenage
It does take a bit of setting up - though once you get it working it's rock solid. If you're really looking for something more lightweight I'd probably recommend KryoNetCorrey
N
2

A lot is discussed above. But be it sockets, rmi, jms - there is a lof of dirty work involved. I would ratter 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 :)

Nexus answered 9/5, 2013 at 9:23 Comment(0)
H
1

Try out jGroups if the data to be communicated is not huge.

Healey answered 19/2, 2011 at 18:1 Comment(5)
I've bookmarked this so I can have a more thorough look over it, thanks. Do you have experience in using this, and if so were there any real difficulties that you faced?Villenage
not much, I am also evaluating this for a project I am doing. Not much experience for now. You can mail on their mailing list. More over, its used by jBoss for cluster membership.Healey
If your application does not need to be robust and is not going to see significant load, it might be appropriate. I've used JGroups at two jobs-- in both cases it was the status quo and in both cases we migrated to other solutions. I have found it to be a thread hog and a memory hog, and its infinite configurability makes it impossible to sanely configure.Formaldehyde
@Formaldehyde Can you please elaborate your answer. Also, you might answer one part of this question: #4981177 (i dont have any decent answer yet anyway)Healey
if: (1) gc pauses are infrequent and unmeasurably short; (2) you don't mind the infinitely configurable, infinitesimally understandable jgroups configurations; (3) you don't mind the mess of threads and buffers and caches the jgroups runtime spins up within your jvm, then jgroups is a fine choice.Formaldehyde
A
0

As you mentioned you can obviously send the objects over the network but that is a costly thing not to mention start up a separate JVM.

Another approach if you just want to separate your different worlds inside one JVM is to load the classes with different classloaders. ClassA@CL1!=ClassA@CL2 if they are loaded by CL1 and CL2 as sibling classloaders.

To enable communications between classA@CL1 and classA@CL2 you could have three classloaders.

  • CL1 that loads process1
  • CL2 that loads process2 (same classes as in CL1)
  • CL3 that loads communication classes (POJOs and Service).

Now you let CL3 be the parent classloader of CL1 and CL2.

In classes loaded by CL3 you can have a light-weight communication send/receive functionality (send(Pojo)/receive(Pojo)) the POJOs between classes in CL1 and classes in CL2.

In CL3 you expose a static service that enables implementations from CL1 and CL2 register to send and receive the POJOs.

Arianna answered 19/2, 2011 at 17:7 Comment(2)
He asked about JVM's, not class loaders.Guarneri
thanks for the suggestion but as @Guarneri points out, I do really need separate JVMs due a number of concernsVillenage
W
0

How about http://code.google.com/p/protobuf/ It is lightweight.

Wystand answered 19/2, 2011 at 19:40 Comment(1)
I tried Protobuf with sockets, it's incredibly hard to get working properly since you need to be able to redirect the output to the right classes once receiving, but to do that you need to read fields from the buffer. You can use CodedInputStream, but it's not nice. Better to use beans if you can and serialise those.Curule

© 2022 - 2024 — McMap. All rights reserved.