Showing posts with label Java RMI. Show all posts
Showing posts with label Java RMI. Show all posts

Thursday, November 13, 2008

Java Remote Method Invocation Program

Remote Method Invocation Sample Program

Remote interface:
1. The interface must be public.
2. The interface must extend the interface java.rmi.Remote.
3. Every method in the interface must declare that it throws java.rmi.RemoteException.
Other exceptions may also thrown.

HelloInterface.java

import java.rmi.*;
/** * Remote Interface for the "Hello, world!" example.*/
public interface HelloInterface extends Remote {
/** * Remotely invocable method. * @return the message of the remote object, such as "Hello, world!". * @exception RemoteException if the remote invocation
fails. */
public String say() throws RemoteException;
}


Remote class:

1. It must implement a Remote interface.
2. It should extend the java.rmi.server.UnicastRemoteObject class.Objects of such a class exist in the address space of the server and can be invoked remotely. While there are other ways to define a Remote class, this is the simplest way to ensure that objects of a class can be used as remote objects. See the documentation of the java.rmi.server for more information.
3. It can have methods that are not in its Remote interface. These can only be invoked locally.

Hello.java:
import java.rmi.*;import java.rmi.server.*;
/** * Remote Class for the "Hello, world!" example. */
public class Hello extends UnicastRemoteObject implements HelloInterface {
private String message;
/** * Construct a remote object * @param msg the message of the remote object, such as "Hello, world!". * @exception RemoteException if the object handle cannot be constructed. */
public Hello (String msg) throws RemoteException {
message = msg;
}
/** * Implementation of the remotely invocable method. * @return the message of the remote object, such as "Hello, world!". * @exception RemoteException if the remote invocation fails. */
public String say() throws RemoteException
{
return message;
}
}


All of the Remote interfaces and classes should be compiled using javac. Once this has been completed, the stubs and skeletons for the Remote interfaces should be compiled by using the rmic stub compiler. The stub and skeleton of the example Remote interface are compiled with the command:

rmic Hello

The only problem one might encounter with this command is that rmic might not be able to find the files Hello.class and HelloInterface.class even though they are in the same directory where rmic is being executed. If this happens to you, then try setting the CLASSPATH environment variable to the current directory, as in the following command:

setenv CLASSPATH .

If your CLASSPATH variable already has some directories in it, then you might want to add the current directory to the others.

Remote class and its corresponding Remote interface. We call these Hello and HelloInterface, respectively. The Server requires the definition of both the Remote class and the Remote interface, but the Client only uses the Remote interface. Remote interface represents the type of an object handle, while the Remote class represents the type of an object. If a remote object is being used remotely, its type must be declared to be the type of the Remote interface, not the type of the Remote class.

Note: All remote objects must extend UnicastRemoteObject, which provides functionality that is needed to make objectsavailable for remote machines.

RMI Client program:

Client itself is just a Java program. It need not be part of a Remote or Serializable class, although it will use Remote and Serializable classes. A remote method invocation can return a remote object as its return value, but one must have a remote object in order to perform a remote method invocation. So to obtain a remote object one must already have one. Accordingly, there must be a separate mechanism for obtaining the first remote object. The Object Registry fulfills this requirement. It allows one to obtain a remote object using only the name of the remote object.

The name of a remote object includes the following information:

1. The Internet name (or address) of the machine that is running the Object Registry with which the remote object is being registered. If the Object Registry is running on the same machine as the one that is making the request, then the name of the machine can be omitted.
2. The port to which the Object Registry is listening. If the Object Registry is listening to
the default port, 1099, this does not have to be included in the name.
3. The local name of the remote object within the Object Registry.


/** * Client program for the "Hello, world!" example. * @param argv The command
line arguments which are ignored. */
public static void main (String[] argv)
{
try
{
HelloInterface hello = (HelloInterface) Naming.lookup ("//RmiRegistryPath/Hello");
System.out.println (hello.say());
}
catch (Exception e)
{ System.out.println ("HelloClient exception: " + e);
}
}

The Naming.lookup method obtains an object handle from the Object Registry running on ortles.ccs.neu.edu and listening to the default port. Note that the result of Naming.lookup must be cast to the type of the Remote interface. The remote method invocation in the example Client is hello.say(). It returns a String which is then printed. A remote method invocation can return a String object because String is a Serializable class.

The code for the Client can be placed in any convenient class. In the example Client, it was placed in a class HelloClient that contains only the program above.

RMI Server:

Server itself is just a Java program. It need not be a Remote or Serializable class, although it will use them. The Server some responsibilities:

1. If class definitions for Serializable classes need to be downloaded from another machine,
then the security policy of your program must be modified. Java provides a security manager class called RMISecurityManager for this purpose. The RMISecurityManager defines a security policy that allows the downloading of Serializable classes from another machine. The "Hello, World!" example does not need such downloads, since the only Serializable class it uses String. As a result it isn't necessary to modify the security policy for the example program. If your program defines Serializable classes that need to be downloaded to another machine, then insert the System.setSecurityManager (new RMISecurityManager ()); as the first statement in the main program below. If this does not work for your program, then you should consult the Security section below.

2. At least one remote object must be registered with the Object Registry. The statement for this is: Naming.rebind (objectName, object); where object is the remote object being registered, and objectName is the String that names the remote object.


/** * Server program for the "Hello, world!" example. * @param argv The command line arguments which are ignored. */
public static void main (String[] argv)
{
try{
Naming.rebind ("Hello", new Hello ("Hello,world!"));
System.out.println ("Hello Server is ready.");
}
catch (Exception e)
{
System.out.println ("Hello Server failed: " + e);
} }


The rmiregistry Object Registry only accepts requests to bind and unbind objects running on the same machine, so it is never necessary to specify the name of the machine when one is egistering an object. The code for the Server can be placed in any convenient class. In the example Server, it was placed in a class HelloServer that contains only the program above.

Running the client and server:

Start the rmiregistry
To start the registry, Windows users should do the following (assuming that your java\bin directory is in the current path):-
start rmiregistry

To start the registry, Unix users should do the following:-
rmiregistry &

Compile the server
Compile the server, and use the rmic tool to create stub files.

Start the server
From the directory in which the classes are located, type the following:-
java ServerProgram

Start the client
You can run the client locally, or from a different machine. In either case, you'll need to specify the hostname of the machine where you are running the server. If you're running it locally, use localhost as the hostname.
java ClientProgram

Read more...

Remote Method Invocation (RMI)

Java Remote Method Invocation is a mechanism for communicating between two machines running Java Virtual Machines. RMI allows one object to invoke a method on an another object existing in other location. The other location could be on the same machine or a different one. The RMI mechanism is basically an object-oriented RPC mechanism. Remote Method Invocation (RMI) facilitates object function calls between Java Virtual Machines (JVMs). JVMs can be located on different computers - even one JVM can invoke methods belonging to an object stored in another JVM.
How RMI differs from CORBA?

CORBA is another object-oriented RPC mechanism. CORBA differs from Java RMI in a number of ways:

1. CORBA is a language-independent standard.
2. CORBA includes many other mechanisms in its standard (standard for TP monitors)
none of which are part of Java RMI.
3. There is also no notion of an "object request broker" in Java RMI.

Java RMI has recently been evolving toward becoming more compatible with CORBA. In particular, there is now a form of RMI called RMI/IIOP ("RMI over IIOP") that uses the Internet Inter-ORB Protocol (IIOP) of CORBA as the underlying protocol for RMI communication.
How RMI Works ?

Consider Java code on machine A needs a service or a method, respectively, of objB (remote Java object) on machine B it starts a remote method invocation. It does this the same way as invoking a local Java object's method.The whole communication behind the remote invocation is done by the RMI mechanism.

When referencing a remote object residing on machine B from code being on machine A there are always two intermediate objects actually handling the communication: a stub object and a skeleton object. When a remote method invocation comes up the following tasks are handled by these two objects:
The stub object (on machine A) has to
* build an information block thats consists of
o an identifier of the remote object to be used,
o an operation number describing the method to be called and
o Marshalled parameters (method parameters have to be encoded
into a format suitable for
transporting them across the net)
* send this information to the server
The skeleton object (on machine B) will

* to unmarshal the parameters,
* to call the desired method on the real object lying on the
server,
* to capture the return value or exception of the call on the
server,
* to marshal this value,
* to send a package consisting of the value in the marshalled
form back to the stub on the
client, machine A.

The stub object unmarshals the return value or exception from the server. This value becomes the return value of the remote method invocation. Or, if the remote method threw an exception, the stub (object) rethrows it in the process space of the caller.
There are three components that participate in supporting remote method invocation.

1. The Client is the process that is invoking a method on a remote object.

2. The Server is the process that owns the remote object. The remote object is an ordinary
object in the address space of the server.

3. The Object Registry is a name server that relates objects with names. Objects are registered
with the Object Registry. Once an object has been registered, one can use the Object
Registry to obtain access to a remote object using the name of the object.

The object registry process has to be started at first. It observes a specific port for bindings between logical names and real objects. Afterwards the server process is started on the same machine. It gives the binding information to the registry process by sending them to the corresponding port. Then everything has be done on server side. Now any client process can be started. It will send lookups to the port of the registry process. Now a connection via a stub object and a skeleton object becomes established.

There are two kinds of classes that can be used in Java RMI.

Remote class is one whose instances can be used remotely. An object of such a class can be referenced in two different ways:

a. Within the address space where the object was constructed, the object is an ordinary object
which can be used like any other object.

b. Within other address spaces, the object can be referenced using an object handle. While there
are limitations on how one can use an object handle compared to an object, for the most part
one can use object handles in the way as an ordinary object.
For simplicity, an instance of a Remote class will be called a remote object.

Serializable class is one whose instances can be copied from one address space to another. An instance of a Serializable class will be called a serializable object. In other words, a serializable object is one that can be marshaled. This concept has no connection to the concept of serializability in database management systems.

If a serializable object is passed as a parameter (or return value) of a remote method invocation, then the value of the object will be copied from one address space to the other. By contrast if a remote object is passed as a parameter (or return value), then the object handle will be copied from one address space to the other.
RMI OverView:

• RMI is an object-oriented implementation of the Remote Procedure Call model; it is an API for
Java programs only
• Using RMI, an object server exports a remote object and registers it with a directory service.
The object provides remote methods, which can be invoked in client programs
• A remote object is declared with a remote interface
• The remote interface is implemented by the remote object
• An object client accesses the object by invoking the remote methods associated with the objects

Read more...
Blog Widget by LinkWithin

JS-Kit Comments

  © Blogger template Newspaper III by Ourblogtemplates.com 2008

Back to TOP