How to create an RMI application

This sample will guide you through the steps to create a simple RMI application

Steps to creation of an RMI application:

The short version

1) Create an interface. (in this case, the interface is SimpleRMIInterface.java).
2) Create a class that implements the interface. (in this case, SimpleRMIImpl.java).
3) Create a server that creates an instance of this class.
4) Create a client that connects to the server object using Naming.lookup().
5) Set the compile options on the RMI implementation file.
6) Compile these classes.
7) Start the RMI registry.
8) Start the server class.
9) Run the client program.
The long version
  1. Create an interface.
    The interface created for this example is SimpleRMIInterface.java. It contains only one method; the method takes no arguments and returns an object of type java.util.Date. Note two things about this interface: 1) it extends the java.rmi.Remote interface (all interfaces used in RMI must do this). 2) the method throws a java.rmi.RemoteException (every method in a remote object's interface must specify this exception in its "throws" clause; this exception is a superclass of all RMI exceptions that can be thrown. See the JDK 1.1 final docs (in the java.rmi section) for a complete list of exceptions that can be thrown.
  2. Create a class that implements the interface.
    In this example, the implementation is found in SimpleRMIImpl.java. This class must extend java.rmi.UnicastRemoteObject and must implement the interface you created in step 1. In my example, the only method that needs to be implemented is getDate(), which returns the current date and time on the system. Note 2 things about the constructor: 1) the call to super(). 2) the call to Naming.rebind(name, this). This call informs the RMI registry that this object is available with the name given in "String name". Other than that, this object simply implements all the methods declared in the interface.
  3. Create a server that creates an instance of the "impl" class.
    In this example, the server class is SimpleRMIServer.java. In this case, the server is pretty simple. It does 2 things: 1) Installs a new RMISecurityManager (Note that RMI uses a different security manager from the security manager used for applets). 2) Creates an instance of the SimpleRMIImpl class, and gives it the name "SimpleRMIImpl instance". The SimpleRMIImpl object takes care of registering the object with the RMI registry. After this code is run, the object will be available to remote clients as "rmi://
  4. Create a client that connects to the server object using Naming.lookup().
    In this example, the client class is SimpleRMIClient.java. The client first installs a new RMI Security Manager (see previous step), then uses the static method Naming.lookup() to get a reference to the remote object. Note that the client is using the interface to hold the reference and make method calls. You should make sure you've created your interface before you try to build the client, or you'll get "class not found" errors when you try to compile your client.
  5. Set the compile options on the RMI implementation file.
    In the IDE, Right-click the SimpleRMIImpl.java file in the navigator pane and choose "Java source properties." In the properties dialog, check "Generate RMI stub / skeleton." and click OK.
  6. Compile these classes.
  7. Start the RMI registry.
    OK. You're done with development at this point; you've built all the code you need to run this example. Now you're setting up the environment so that you can run it. rmiregistry is a program that comes with the JDK 1.1 final; you can find it in the "bin" directory of your JDK installation. From within JBuilder, choose Tools | RMIRegistry. The next time you look at the tools menu, you will notice there is a check next to this menu option. This is your clue that the registry is running. Selecting it again will close the RMIRegistry.

    Alternatively, you can start the RMIRegistry from the command line under Windows 95 or NT. To do this, type which will cause the RMI registry to be started in its own DOS window. The RMI registry must be started before you can start your server.
  8. Start your RMI server program.
    Run the SimpleRMIServer from the project. This starts the server running. As we discussed earlier, the server then creates an instance of SimpleRMIImpl and makes it known to the RMI server as "SimpleRMIImpl instance".
  9. Run your client program.
    Run SimpleRMIClient. The program will ask the RMI registry for a reference to "SimpleRMIImpl instance". After it has this reference, the client can invoke any methods declared in the SimpleRMIInterface interface as if the object were a local object.

After the RMI application has been created, you probably want to deploy the client apart from the server. To accomplish this you can use the Deployment Wizard to setup separate directories for the client files and for the server files.

  1. Create a directory for the client - create a directory in any location of your liking and let us name it RMIClient
  2. Create a directory for the server - let us name it RMIServer
  3. Setup the client deployment: bring up the Deployment Wizard, under the Wizards menu, and set the Delivery Options to 'No Archive'. Select SimpleRMIClient.java, SimpleRMIInterface.java, and SimpleRMIImpl_Stub.java from the file list. Set the Archive Output Path to the client directory. If SimpleRMIImpl_Stub.java is not show on the Deployment Wizard file list, then you can also copy it manually from the borland\samples\rmi\simple directory under the output root directory into the borland\samples\rmi\simple under your client directory, RMIClient.
  4. Setup for the server deployment: repeat the previous step but select these files: SimpleRMIServer.java, SimpleRMIInterface.java, SimpleRMIImpl.java, SimpleRMIImpl_Skel.java, and SimpleRMIImpl_Stub.java. Put those files into your server directory, which we have named RMIServer. If SimpleRMIImpl_Stub.java and SimpleRMIImpl_Skel.java are not show on the Deployment Wizard file list, then you can also copy it manually from the borland\samples\rmi\simple directory under the output root directory into the borland\samples\rmi\simple directory under your server directory, RMIServer.

Now, once you have separate setup for the client and the server, the client can be run from a different computer than the server. If you take a look at SimpleRMIClient.java you will notice that the client application takes a single argument which should be a computer name, with the default of the name of the local computer, if it is not supplied. The direction below shows how to have the server running on the current machine and have client running from a different machine that are attached together through a TCP/IP network.
  1. Copy the client directory to a different machine that has either JBuilder or JDK 1.1 (or above) installed properly to run java application.
  2. Start the rmiregistry, as described above, on the current machine
  3. Start the server application: bring up the DOS Window and change directory to your server directory, RMIServer. Run [jbuilder_install_dir]\setvars [jbuilder_install_dir]. This should setup the environment variables to run java. Run java borland.samples.rmi.simple.SimpleRMIServer.java
  4. Start the client on the client machine. Go to the client machine, bring up a DOS Window, change directory to the client directory, and type java borland.samples.rmi.simple.SimpleRMIClient [server_hostname].