You are on page 1of 3

RMI

The RMI (Remote Method Invocation) is an API that provides a mechanism to create distributed
application in java. The RMI allows an object to invoke methods on an object running in another JVM.

6 steps to write the RMI program.

1. Create the remote interface


2. Provide the implementation of the remote interface
3. Compile the implementation class and create the stub and skeleton objects using the rmic tool
4. Start the registry service by rmiregistry tool
5. Create and start the remote application
6. Create and start the client application

How to create an RMI system


Steps to creation of an RMI system:

1) Create an interface.

An interface is similar to a pure virtual class in C++; it defines the methods (and the arguments
to the methods) that will be available in a class that implements it; however, it doesn't actually
implement any of the methods.

Note 2 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.


This class must extend java.rmi.UnicastRemoteObject and must implement the interface you
created in step 1.
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 implementation class

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 implementation class, and gives it the name "name". The
name object takes care of registering the object with the RMI registry.
4) Create a client that connects to the server object using Naming.lookup()
5) Compile all Java source files.
6) Create stub and skeleton object by rmic tool (Only if you have a very old version of Java
do you have to do this: prepare the skeleton and stub files.)
To do this, from DOS prompt: for Example: rmic InterfaceImpl] Run the RMI interface
compiler on the .class file of the implementation class (in this case, you'd say "rmic
myRMIImpl").
7) Start the RMI registry in one command prompt

rmiregistry portnumber for example rmiregistry 5000


8) Start the server class in another command prompt

E:g java MyServer

9) Create a permissions file. (More dangerous in the sense that it leaves your machine open to
certain net-based attacks during the lifetime of the implementation thread.) For simplicity's sake,
keep your permissions file in the same directory as your .class files.
Permit.policy

grant{

permission java.net.SocketPermission”*:1024-65535”,”connect, accept,resolve”;


permission java.net.SocketPermission”*:1-1023”,”connect, resolve”;

}
java -Djava.security.policy=”Permit.policy” Client

10) Run the client program ("java myRMIClient").

You might also like