Sunday, February 17, 2013

Simple RMI Tutorial

I have tried to make this RMI tutorial as short as possible, so that you may get started creating your own RMI applications as soon as possible.

The Callee


1. Fist of all you will have to create a project, e.g. rmi-callee.

2. Inside foo you will need to define a remote interface which the invoker may use.

For this example, we have a method greet in the interface Greetable which will reside in the default package (directly within src/):

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

public interface Greetable implements Remote {
    String greet() throws RemoteException;
}

3. The next step is to actually implement the interface in our class, again in the default package:

import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

class Greeter extends UnicastRemoteObject implements Greetable {

    public Greeter() throws RemoteException { super(); }

    @Override
    public String greet() throws RemoteException { return "Hi"; }

    public static void main(String[] args) throws Exception {

        // Bind an instance to rmiregistry
        Naming.bind("rmi://localhost/Greeter", new Greeter());
    }
}


Now that you have your class exposing some functionality, you may want to check if it works. From the command line create a script that will run your class named run.sh. Note: if you are using Windows, you might want to download MinGW to make your life a bit simpler.

4. First, create the JAR file containing the interface. PS: You should to be in the root of your project to do this:
cd rmi-callee/
jar cvf g.jar -C bin/ Greetable.class

5. Create the script to run the program:
touch run.sh
chmod +x run.sh

6. Inside run.sh:
java -cp bin/\
     -Djava.rmi.server.codebase=file:your/path/rmi-callee/g.jar\
     Greeter

7. Start rmiregistry as a background process:
rmiregistry &

8. Start the program:
./run.sh

It should now be running. If you get an exception, you are not doing it right. Be sure you have followed every step thoroughly, and are running each command from the right directory.

The Caller


Now you would like to create a caller which invokes the callee.

1. Create another project, e.g. rmi-caller.

2. Manually copy the Greetable interface from the first project into this one.

3. In the default package, create a class Main:
import java.rmi.Naming;

public class Main {

    public static void main(String[] args) throws Exception {
        Greetable g = (Greetable) Naming.lookup("rmi://localhost/Greeter");
        System.out.println(g.greet());
    }
}

4. Create the script to run the program:
cd rmi-caller/
touch run.sh
chmod +x run.sh

5. Inside run.sh:
#!/usr/bin/sh
java -cp bin/ Main

6. Run the example:
./run.sh

It should now invoke the callee if it is running, and output Hi. If you get an exception, you are not doing it right. Be sure you have followed every step thoroughly, and are running each command from the right directory.

Summary and code

I hope this tutorial will help you develop RMI systems. You can find the source code to rmi-callee and rmi-caller below:

No comments:

Post a Comment