Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to learn Java RMI quickly [closed]

Tags:

java

ipc

rmi

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

I am assuming RMI is the best way to do this, but I'm not sure, as I know next to 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 the two JVMs with this interface using RMI? What do I have to know about blocking / threading / synchronization to make this work?

like image 201
Jason S Avatar asked Sep 03 '10 17:09

Jason S


2 Answers

One quick way to do this is to use Spring. This doesn't (necessarily) mean using lots of XML configuration: Spring's RMI support classes can be used programmatically.

The two key classes are:

  • RmiServiceExporter to make an object remotely accessible.
  • RmiProxyFactoryBean to access a remote object.

An advantage of doing it this way is that you only need to write an implementation of your interface, and that can then be made available using RmiServiceExporter. Meanwhile, on the client side, using RmiProxyFactoryBean gives you a proxy object that implements the interface. As far as client-side code is concerned, it's working with a 'real' implementation of the interface, but the proxy does the RMI invocations for you. The use of RMI is transparent.

As an example of how quick this can be, I've just written a server and client using your interface.

My implementation of the interface is:

public class ApplicationImpl implements Application {

    private boolean enable;

    @Override
    public void setLoggingEnabled(boolean enable) {
        this.enable = enable;
    }

    @Override
    public boolean isLoggingEnabled() {
        return enable;
    }

}

The server-side code is:

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

The client-side code is:

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 as expected outputs:

false
true
like image 184
Richard Fearn Avatar answered Sep 28 '22 06:09

Richard Fearn


You can start with the official RMI tutorial.


Resources :

  • Introduction to Java RMI
  • Learn Java RMI

On the same topic :

  • Java RMI Resources
like image 26
Colin Hebert Avatar answered Sep 28 '22 05:09

Colin Hebert