Friday, May 09, 2008

Java RMI without name server

You know, Java RMI is cool, but sometimes that pesky name server just gets into the way. One starts the name server by typing something on the sorts of rmiregistry. Well, there are other ways, but that is very common.

Today I decided to tinker a little bit with Java RMI. My idea was to code some sort of channel, that would give me direct access to a remote object, as long as I know its address in the distributed system. I want the client to be able to get the object without having to do a lookup before, by just typing:

RemoteObjInterface roi = ImportChannel.imp(host, port);
roi.remoteMethod();

Notice that there is no type cast on this code. Actually, the type cast happens in ImportChannel, so, I am just sweeping the garbage under the carpet. The server, by its turn, should be very simple too. The code should be like:

RemoteObjImpl ro = new RemoteObjImpl();
ExportChannel ec = new ExportChannel(ro);
ec.export();

The code is available below. I am using the example application from these slides.

  • Server. The server application, that creates a remote object and exports it via the export channel given further below.

  • TimeClient. The client application. It uses an import channel to get a hold of the remote object, and then it calls a remote method on it.

  • Second. The remote object's interface.

  • SecondImpl. The implementation of the remote object.

  • SecondImpl_Stub. This is the stub for the remote object. We need a stub to call a remote method, you know. Of course, we do not really need the source code, just the bytecodes, but I am given the source here just for fun. To produce an stub, one can use rmic. This tool erases the source code, after generating the bytecodes. To keep the source, use the option -keep.

  • ServerThread. The server uses this thread to handle clients looking for the remote object.

  • ExportChannel. This is the interface that a server must use to make an object available to receive remote calls.

  • ImportChannel. This is the interface that the client must use to get a hold of a remote object.


If you download all files above, then running this example application is very easy. You will need two terminals. In the first, initialized the server by typing java Server. It will print the port in which the object is listening. To use the client, go to the other terminal and type java TimeClient port, where port is the port where the server is listening. In this example, I have set the server to start listening at port 20000.

0 Comments:

Post a Comment

<< Home