Download Remote Method Invocation The Java Remote Method Invocation

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Remote Method Invocation


The Java Remote Method Invocation (RMI) system allows an object running in one
Java Virtual Machine (JVM) to invoke methods on an object running in another

programs written in the Java
programming language
RMI Applications

RMI applications are often comprised of two separate programs: a server and a client.


accessible, and waits for clients to invoke methods on these remote objects.

a remote reference to one or more remote objects in
the server and then invokes methods on them.

client communicate
and pass information back and forth.

Such an application is sometimes referred to as a distributed object application.
Distributed Object Applications
1. Locate remote objects:
 An application can register its remote objects with RMI's simple naming facility,
the Rmi registry

the application
operation
can pass and return remote object references as part of its normal
2. Communicate with remote objects
3. Load class bytecodes for objects that are passed around:
RMI Architecture:\
It consist of three layers



The stubs / skeletons
When a client invokes a remote method request, it starts at the top with the stub on
Remote Object Characteristics
An object becomes
remote by implementing a remote interface which has the following
characteristics.
Each method of the interface declares java.rmi
addition to any application-specific exceptions.
Stubs for RMI

RMI treats a remote object differently from a non remote object when the object is
passed from one virtual machine to another.

Rather than making a copy of the implementation object in the receiving virtual
machine, RMI passes a remote stub for a remote object.

The stub acts as the local representative, or proxy, for the remote object and
basically is, to the caller, the remote reference.

The caller invokes a method on the local stub, which is responsible for carrying out
the method call On the remote object.

A stub for a remote
object implements the same set of remote interfaces that the
remote object implements.
This allows a stub to be cast to any of the interfaces that the remote object implements.
Marshalling & UnMarshalling
It is the process of assembling and disassembling parameters to and from
remote objects and methods
Remote Reference Layer
This is responsible for maintaining an independent reference protocol that is not
specific to any stub or skeleton.
The RRL deals with the lower level transport interface and is responsible for
providing a stream to the stub / skeleton layer.
The communication between client and server side components is handled by
Transport layer.
Steps in Creating an application that accesses remote objects
1. Define Interfaces for the remote classes.
2. Create and compile implementation classes for the remote classes.
3. Create stub and skeleton classes using rmic .
4. Create and compile a server application.
5. Start the RMI registry and the server application.
6. Create and compile a client program to access the remote objects.
7. Test the Clients.
java.rmi

functionality of Java RMI system .
  Every remote class must be described by an interface.
  The Client - side stubs implement these interfaces.

The stub classes are created using the rmic utility.

The rmic has the following requirements .


The interfaces must be public


The interfaces must extend the Remote interface
Each method must throw RemoteException
The stub and implementation code must reside in a package.
Classes
A Remote class is one whose instances can be accessed remotely. On the computer where it
is defined, instances of this class can be accessed just like any other object. On other
computers, the remote object can be accessed via object handles.
Define an interface that declares remote methods.
The first file AddServerIntf.java defines the remote interface: it includes one method that
accepts two double arguments and returns their sum. All remote interfaces must extend the
interface Remote, that defines no methods: its purpose is simply to indicate that an interface
uses remote methods. All remote methods should throw a RemoteException
AddInterface1.java
import java.rmi.*;
public interface AddInterface1 extends Remote
{
public String say1() throws RemoteException;
public int Add1(int first,int second, int thirdno)throws RemoteException;
}
Implement the remote interface and the server
The second source file AddServerImpl.java implements the remote interface:
AddImple.java
import java.rmi.*;
import java.rmi.server.*;
public class AddImp1 extends UnicastRemoteObject implements AddInterface1
{
private String message; // Strings are serializable public
AddImp1(String msg) throws RemoteException
{ message = msg;
}
public String say1() throws RemoteException
{ return message; }
public int Add1(int first, int second, int third)
{
int sum=first+second+third;
return sum;
}
}
All remote objects must extend UnicastRemoteObject which provide the functionally that is
needed to make objects available from remote machines. The third source file
AddServer1.java contains the main program for the server machine. Its primary function is to
update the RMI registry on that machine. This is done by using the rebind() method of the
Naming class (it is in java.rmi API). That method associates a name with an object reference.
AddServer1.java
import java.rmi.*;
import javax.rmi.*;
import javax.naming.*;
class AddServer1
{
public static void main (String[] args)
{
try
{
Naming.rebind("rmi://localhost/AddServer1",new AddImp1("Hello,
world!")); System.out.println("Add Server is ready.");
}
catch (Exception e)
{
System.out.println("Add Server failed: " + e);
}
}
}
Develop a client (an application or an applet) that uses the remote interface. The fourth
source file AddClient1.java implements the client side of this distributed application. This
program requires 3 command line arguments: the IP address or name of the remote server,
and two numbers that are to be summed.
The application forms an URL string using the rmi protocol, the first command line argument
and the name "AddServer" and the same client can make the remote invocation on the second
server named ―AddServer1‖ that is used by naming registry of the server. The program calls
the method lookup() of theNaming class to find a reference to a remote object. All remote
method invocations can then be directed to this object. The client program illustrates the
remote call by using the method add1(firstno,secondno) and add2(firstno,secondno,thirdno)
that will be invoked on the remote server machine from the local client machine where the
client runs.
AddClient1.java
import java.rmi.*;
import javax.rmi.*;
import javax.naming.*;
//import java.io.*;
import java.util.*; class
AddClient1
{
public static void main (String[] args) {
AddInterface1 add2;
AddInterface add1;
String name1 =
"rmi://localhost/AddServer"; String name2
= "rmi://localhost/AddServer1"; try {
add1 = (AddInterface)Naming.lookup(name1);
add2 = (AddInterface1)Naming.lookup(name2);
//DataInputStream dis=new DataInputStream(new
InputStream(System.in)); Scanner read=new Scanner(System.in);
System.out.println("Enter three integers to add:");
//int firstno=Integer.parseInt(in.readLine());
//int secondno=Integer.parseInt(in.readLine());
int firstno=read.nextInt();
int secondno=read.nextInt();
int thirdno=read.nextInt();
System.out.println(add1.say());
System.out.println(add2.say1());
System.out.println("The addition of two numbers is:"+add1.Add(firstno,secondno));
System.out.println("The addition of three numbers
is:"+add2.Add1(firstno,secondno,thirdno));
}
catch (Exception e)
{
System.out.println("AddClient exception: " + e);
}
}
}
OUTPUT:
Execution Steps:
Step-1: compile AddInterface1.java
Step-2: compile AddImpl1.java and AddImple.java Step-3: start rmiregistry
Step-4: Compile and run AddServer.java and AddServer1.java Step-5: Compile and run
AddClient1.java