Remote Method Invocation(RMI)Concept
A java object runs within a Java Virtual Machine
(JVM), Likewise J2EE applications runs within a JVM;
however objects used by a j2ee application do not need
to run on the same JVM as the J2EE application. This is
because a J2ee application and its components can
invoke objects located on a different JVM by using the
Java Remote Invocation system.
RMI is used for remote communication between Java
application and components, both of which must be
written in the Java programming language.
• RMI is used to connect together a client and a server.
• A client is an application or component that requires the services of
an object to fulfill a request.
• A server creates an object and makes the object available to clients.
• A client contacts the server to reference and invoke the object by
• A client locates a remote object by either using the RMI naming
registry or by passing a string that reference the remote object .
•In either case, RMI returns a reference to the remote object, which
is then invoked by the client as if the object was on the local JVM.
There are three steps necessary to make an object
available to remote clients.
1. to design an object,
2. to compile the object,
3. to make the object accessible to remote clients over the
Besides defining the business logic of an object, the
developer must define a remote interface for the object,
which identifies the methods that are available to remote
clients. Likewise those methods must be defined in the
object. In addition to methods that can be invoked by client ,
the developer must also define other methods that supports
the processing of the client-invoked methods. These are
referred to as server methods, while methods invoked by a
client are called client methods.
Compilation of the object is a two-step process that
begins by compiling the object using the javac
compiler. The object must contain implementation of
the remote interface, and server and client methods.
Once the object is compiled, you must create a stub
for the object. This is done by calling the RMI
compiler called rmic.
The compiled object is then made accessible over
the network by loading the object to a server. Make
sure that RMI remote object registry is running;
otherwise, a remote client will be unable to access
the object. To start the RMI registry on windows NT,
type the following at the command line:
The sever side of a remote object invocation consists of a remote
interface and methods.
The remote interface is at the center of every remote object because the
remote interface defines how the client views the object. Methods provide
the business logic that fulfills a client’s request whenever the client
remotely invokes the method.
Public interface myApplication extends Remote
String myMethod() throws RemoteException;
It illustrates the remote interface definition and makes one method called
myMethod() available to clients. This method doesn’t requires an argument
and returns a String object, There would be many additional methods
appearing in a remote interface in a real-world J2EE application.
Implementing the remote Interface
public class myApplicationServer extends
UnicasteRemoteObject implements myApplication
public myApplicationserver() throws RemoteException
public String myMethod()
returns “I’m here to server.n”;
public static void main(String  args)
The program begins by importing the necessary packages that are
required to implement RMI and implements the myApplication remote
interface defined in the above.
Next the program defines the constructor and myMethod, which is the
only method contained in the object. This method simply returns the
String “I’m here to serve.n”. You can modify the method as
necessary based on the business rules that are implemented by the
The program proceeds to create and install a security manager. A
security manager serves as a firewall and grants or rejects
downloaded code access to the local file system and similar
privileged operations. RMI requires that server-side applications
install a security manager. In this example the getSecurityManager()
method associates the server-side program with the securityManager
Then the server-side program creates an instance of
myApplication and makes the remote object myMethod available to
remote clients by calling the rebind() method.
The rebind() method registers the remote object with the RMI
remote object registry or with another naming service. The object’s
name is “//host/myApplication” where host is the name or IP
address of the server.
The Client-Side program calls the
remote object defined in last program
which returns a String object that the
client-side program displays.
A real-world application requires the
remote object to perform complicated
operations that simply return a String
This example begins by creating a security manager
using the same method as described previously in the
Next, the lookup() method is used to locate the remote
object, which is called myApplication. The lookup()
method returns a reference to the object called myApp.
And the program calls the myMethod() method to invoke
the myMethod of the remote object. The myMethod
method returns a String object that is passed to the
println() method, which displays the contents of the
String object on the screen.
Any exceptions that are thrown while the client-side
program runs are trapped by the catch() block. The
catch() block calls the getMessage() method to retrieve
the error message that is associated with the exception.
The error message is then displayed on the Screen.
Before running the client, you need to
Compile the source code.
Compile the server class using the rmic compiler, which produces the
skeleton and the stub classes.
Start the rmiregistry.
Start the server.
Run the client.
You’ll need to define the security properties as command-line
arguments when running the client program.
java –Djava.security.policy=c:/javacode/src/policy myApplicationServer
Here is the command-line necessary to run the server program:
The Djava.rmi.server.codebase argument identifies the location of the
class Files. One or three forward slashes must precede the path
depending on the operating environment. The Djava.security.policy
argument identifies the location of the policy file.
A particular slide catching your eye?
Clipping is a handy way to collect important slides you want to go back to later.