Thursday, 26 April 2007

Remote Method Invocation has Landed!

I have finally nailed (ok not not nailed, but more pinned down with a sturdy pin) Remote Method Invocation(RMI)

RMI

Unfortunatelly, the RMI lecture came straight after a class test and was thus promptly slept through by most students. Going back over the lecture slides, which are not meant to be read but worked through in class, were not much help. After much confusion, trying to work it out, going back to the Head First
Java book
I finally tried the java website, and hey presto, found exactly what was needed, a tutorial
by jGuru.

To summorise how neat and simple RMI is (for the simple stuff anyway!)

The remote object you want to be available to a different JVM must implement an interface.

That interface:

  • is public
  • implements Remote
  • each method signature defined must throw a RemoteException
  • and for programming ease import java.rmi.*;


The remote object:

  • again be public
  • implement the interface above and extend UnicastRemoteObject
  • supply a no arg constructor which throws RemoteObject and calls super()
  • implement all the methods defined in the interface it is implementing


Now getting that remote object "out there", which will be done by a server object, is so fantastically easy, once you see it in practise.

The server object:

  • does not implement or extend anything
  • places all the work in try/catch statements
  • imports java.rmi.*;
  • makes an instance of the remote object to be made available to the world
  • declares it to the world by doing such: Naming.rebing("rmi://hostname:port/madeupname", RemoteObject Instance);
  • has a main method for starting the server


The client, or the listener which wants to use the remote object as if it were on its heap:


  • Gets a copy of the class for the object it will use as a stub/ if one is not on that system
  • RemoteObject r = (RemoteObject)Naming.lookup("URL same as above/madeupname");
  • you can then use r as if it were on your system!


Of course this won't make much sense without looking at some actuall examples, but it
is so straightforward that, like a lecturer said, you could put this all on a post stamp!

No comments: