How to quickly learn Java RMI

I have a Java application that I’ve been working on for a year or two. I would like to create a very simple set (with the ability to add complexity later) of the interfaces that I can use to control my Java application from another JVM (e.g. MATLAB).

I guess RMI is the best way to do this, but I'm not sure, since I know almost nothing about it.

What is the best way to learn RMI quickly?

Let's say I want to use an interface like this:

interface Application { public void setLoggingEnabled(boolean enable); public boolean isLoggingEnabled(); } 

How could I implement a bridge between two JVMs with this interface using RMI? What do I need to know about lock / thread / sync to do this?

+7
java ipc rmi
source share
4 answers

One quick way to do this is to use Spring. This does not mean (required) using a lot of XML configuration: Spring RMI support classes can be used programmatically.

Two key classes:

The advantage of this method is that you only need to write an implementation of your interface, and then you can make it available using RmiServiceExporter . Meanwhile, on the client side, using RmiProxyFactoryBean gives you a proxy object that implements the interface. As for the client-side code, it works with a β€œreal” interface implementation, but the proxy server makes RMI calls for you. Using RMI is transparent.

As an example of how fast this can be, I just wrote a server and client using your interface.

My interface implementation:

 public class ApplicationImpl implements Application { private boolean enable; @Override public void setLoggingEnabled(boolean enable) { this.enable = enable; } @Override public boolean isLoggingEnabled() { return enable; } } 

Server Side Code:

 RmiServiceExporter exporter = new RmiServiceExporter(); exporter.setService(new ApplicationImpl()); exporter.setServiceInterface(Application.class); exporter.setServiceName("application"); exporter.afterPropertiesSet(); 

Client Code:

 RmiProxyFactoryBean pfb = new RmiProxyFactoryBean(); pfb.setServiceInterface(Application.class); pfb.setServiceUrl("rmi://localhost/application"); pfb.afterPropertiesSet(); Application app = (Application) pfb.getObject(); System.out.println(app.isLoggingEnabled()); app.setLoggingEnabled(true); System.out.println(app.isLoggingEnabled()); 

which is expected to produce:

 false true 
+10
source share

You can start with the official RMI tutorial .


Resources:

In the same topic:

  • Java RMI Resources
+6
source share

As Yok and Colin said. Take a look at the RMI tutorial supported by Oracle (Sun), and by the time you read, try to code the sample codes and test them in the sample project.

References

+1
source share

All Articles