Friday, May 14, 2010

Find Sum of N Numbers Using RMI | RMI Airthematic Operation

To write a program for sum of n numbers using RMI(Remote Method Invocation.
Algorithm and step by step procedure RMI
Serialization and Data marshaling
To transfer objects, the RMI API uses the Serialization API to wrap (marshal) and unwrap (unmarshal) the objects. To marshal an object, the Serialization API converts the object to a stream of bytes, and to unmarshal an object, the Serialization API converts a stream of bytes into an object..
Steps:
• Define the remote interface
• Implement the server
• Implement the client
• Compile the source files
• Start the Java RMI registry, server, and client
The files needed for this program are:
• MyInterface.java - a remote interface
• MyServer.java - a remote object implementation that implements the remote interface
• MyClient.java - a simple client that invokes a method of the remote interface
1. Define the remote interface
A remote object is an instance of a class that implements a remote interface. A remote interface extends the interface java.rmi.Remote and declares a set of remote methods. Each remote method must declare java.rmi.RemoteException (or a superclass of RemoteException) in its throws clause, in addition to any application-specific exceptions.
2. Implement the server
• Create a remote object
• Register the remote object with a Java RMI registry
The implementation class MyServer implements the remote interface MyInterface, providing an implementation for the remote method.
3. Implement the client
The client program obtains a stub for the registry on the server's host, looks up the remote object's stub by name in the registry.
Next, the client invokes the remote method lookup on the registry stub to obtain the stub for the remote object from the server's registry.
Finally, the client invokes the method on the remote object's stub, which causes the following actions to happen:
• The client-side runtime opens a connection to the server using the host and port information in the remote object's stub and then serializes the call data.
• The server-side runtime accepts the incoming call, dispatches the call to the remote object, and serializes the result to the client.
• The client-side runtime receives, deserializes, and returns the result to the caller.
4. Compile the source files
The source files for this example can be compiled as follows:
javac MyInterface.java
javac MyServer.java
javac MyClient.java
5. Generate Stubs and Skeletons
6. Start the Java RMI registry, server, and client
To run this example, you will need to do the following:
• Start the Java RMI registry
• Start the server
• Run the client
Start the Java RMI registry
Before start the RMI Registry we should be set path of the registry, type the following command
Set path=c:\jdk1.5\bin
start rmiregistry
Source code Programming Remote method invocation
a) Server Program
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
public class MyServer extends UnicastRemoteObject implements MyInterface
{
public MyServer() throws RemoteException
{}
public int SumOfSeries(int n) throws RemoteException
{
int res;
res=(n*(n+1))/2;
return res;
}
public static void main(String args[]) throws Exception
{
MyServer ms=new MyServer();
Naming.bind("rmi:///Peace",ms);
System.out.println("Server Ready!!!");
}
}
b) Client Program
import java.rmi.*;
import java.io.*;
public class MyClient
{
public static void main(String args[])throws Exception
{
try
{
MyInterface obj=(MyInterface)Naming.lookup("rmi:///Peace");
int res,a,b,c;
DataInputStream din=new DataInputStream(System.in);
System.out.println("Enter the number n to find the Sum:");
a=Integer.parseInt(din.readLine());
System.out.println("The Sum of"+a+" is"+obj.SumOfSeries(a));
}
catch(Exception e)
{
System.out.println("Error in input!!!");
}
}
}
c) Interface Program
import java.rmi.*;
interface MyInterface extends Remote
{
public int SumOfSeries(int n)throws RemoteException;
}

0 comments:

Flag counter

free counters