tags:

views:

120

answers:

4

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?

+2  A: 

Good links to start with :

RMI Guide :

http://download.oracle.com/javase/6/docs/technotes/guides/rmi/index.html

http://download.oracle.com/javase/1.4.2/docs/guide/rmi/

RMI Tutorial:

http://download.oracle.com/javase/tutorial/rmi/index.html

YoK
+1 for RMI guide which includes the spec and a FAQ, I think I've got a good handle on the basics now, thanks!
Jason S
+5  A: 

You can start with the official RMI tutorial.


Resources :

On the same topic :

Colin Hebert
+1  A: 

As Yok and Colin said Take a look to the RMI tutorial supported by Oracle (Sun) and by the time you are reading try to code the example codes and test them in an example project.

References

Garis Suero
+5  A: 

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:

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
Richard Fearn
cool, thanks for the details. not sure if I want to use Spring rather than plain RMI (not sure of dependency overhead) but it gives me some ideas.
Jason S