iamjerryyeung

Saturday, May 06, 2006

rmi without registry

http://www.st.informatik.tu-darmstadt.de/static/staff/Haupt/rmi.html

Using Java RMI without rmiregistry
When I had to write a kind of small remote control for an application, I came across this. Normally, you have to start rmiregistry in order to let other machines access remote functionality. This was not applicable in my system, so I googled for some solution and finally came across something that worked out. Here is a step-by-step list of things to do.

Create an interface for the service.
Implement the service. Note that the implementation of the service is also the server application in this example.
Implement the client.
Compile and run.

The example consists of three small packages, namely simplermi containing the service interface, simplermi.server containing the server implementation, and simplermi.client containing the client.

If you have any objections or remarks, feel free to notify me.


--------------------------------------------------------------------------------
Create an Interface for the Service [Top]

The service used as an example is quite simple. It consists of a sole method named request() that returns a String.

package simplermi;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface SimpleServiceIfc extends Remote {
String request() throws RemoteException;
}


--------------------------------------------------------------------------------
Implement the Service [Top]

The service implementation is, in this example, also the server. To deploy the service, the application has to be started. All deployment action takes place in main().

package simplermi.server;

import java.rmi.AlreadyBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;

import simplermi.SimpleServiceIfc;

public class SimpleService extends UnicastRemoteObject
implements SimpleServiceIfc {
public SimpleService(int port) throws RemoteException {
super(port);
}

public static void main(String[] args)
throws AlreadyBoundException, RemoteException {
// Create a RMI registry listening on port 4242.
Registry localRegistry = LocateRegistry.createRegistry(4242);
// Create an instance of the service listening on the same port.
SimpleServiceIfc service = new SimpleService(4242);
// Deploy the service.
localRegistry.bind("SimpleService", service);

// loop forever
while(true);
}

/**
* The service routine.
*/
public String request() throws RemoteException {
return "Hello, world!";
}
}


--------------------------------------------------------------------------------
Implement the Client [Top]

The client just performs a lookup in the registry via the registry port, retrieves a service reference and calls the service routine.

package simplermi.client;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import simplermi.SimpleServiceIfc;

public class SimpleClient {
public static void main(String[] args)
throws MalformedURLException, NotBoundException, RemoteException {
SimpleServiceIfc remoteService =
(SimpleServiceIfc) Naming.lookup("//localhost:4242/SimpleService");
System.out.println("Service returns: " + remoteService.request());
}
}


--------------------------------------------------------------------------------
Compile and Run [Top]

Here are the steps that are necessary to actually execute the simple example. I assume the source files are stored in the appropriate directories (according to the package structure) and that the current working directory is the one containing the root of the package hierarchy, simplermi/.

Compile the sources:
$ javac simplermi/*.java
$ javac simplermi/server/*.java
$ javac simplermi/client/*.java
Generate RMI stubs:
$ rmic -v1.2 simplermi.server.SimpleService
Start the service (in background):
$ java simplermi.server.SimpleService &
Run the client:
$ java simplermi.client.SimpleC

0 Comments:

Post a Comment

<< Home