My Profile
Active Members
TodayLast 7 Days
more...
Awards & Gifts
Online Exams
Fresher Jobs
Our fresher job section is exclusively for fresh graduates! Find jobs for freshers in major Indian
cities including Bangalore, Chennai, Hyderabad, Pune or Kochi
Resources
Find educational articles, blogs, discussion threads and other resources.
Colleges
Find details about any college in India or search for courses.
Advertisements
|
Remote Method Invocation(RMI ) in Java
Posted Date: 22 May 2008 Resource Type: Articles/Knowledge Sharing Category: Computer & Technology
|
Posted By: srinivas Member Level: Gold Rating: Points: 1
|
|
|
|
Remote Method Invocation (RMI)
Remote Method Invocation (RMI): Remote Method Invocation (RMI) allows a Java object that executes on one machine to invoke a method of a Java object that executes on another machine. The server receives a request from a client, processes it, and returns a result.
Create a directory with the name sumRmi in D:\ . Create all the java File in the sumRmi.
AddServerIntf.java import java.rmi.*; public interface AddServerIntf extends Remote { double add(double d1, double d2) throws RemoteException; }
AddServerImpl.java
import java.rmi.*; import java.rmi.server.*; public class AddServerImpl extends UnicastRemoteObject implements AddServerIntf { public AddServerImpl() throws RemoteException { } public double add(double d1, double d2) throws RemoteException { return d1 + d2; } }
AddServer.java
import java.net.*; import java.rmi.*; public class AddServer { public static void main(String args[]) { try { AddServerImpl addServerImpl = new AddServerImpl(); Naming.rebind("AddServer", addServerImpl); } catch(Exception e) { System.out.println("Exception: " + e); } } }
AddClient.java
import java.rmi.*; public class AddClient { public static void main(String args[]) { try { String addServerURL = "rmi://" + args[0] + "/AddServer"; AddServerIntf addServerIntf = (AddServerIntf)Naming.lookup(addServerURL); System.out.println("The first number is: " + args[1]); double d1 = Double.valueOf(args[1]).doubleValue(); System.out.println("The second number is: " + args[2]); double d2 = Double.valueOf(args[2]).doubleValue(); System.out.println("The sum is: " + addServerIntf.add(d1, d2)); } catch(Exception e) { System.out.println("Exception: " + e); } } }
Compilation :
Javac AddServerIntf.java Javac AddServerImpl.java Javac AddServer.java Javac AddClient.java
CLASSPATH : Set the classpath to D:\SumRmi in the Environment Variable
Generate Stubs and Skeletons : rmic AddServerImpl
RMI Registry on the Server Machine : start rmiregistry
Note: When this command returns, you should see that a new window has been created. You need to leave this window open until you are done experimenting with the RMI.
Start the Server : java AddServer
Start the Client: java AddClient 127.0.0.1 18 19
Output: The first number is: 18 The second number is: 19 The sum is: 37.0
Notes: AddServerIntf.java: defines the remote interface that is provided by the server. It contains one method that accepts two double arguments and returns their sum.
AddServerImpl.java, implements the remote interface. The implementation of the add () method is straightforward.
AddServer.java, contains the main program for the server machine. Its primary function is to update the RMI registry on that machine.
AddClient.java, implements the client side of this distributed application. AddClient.java requires three command line arguments. The first is the IP address or name of the server machine. The second and third arguments are the two numbers that are to be summed.
Client and Server Process:
client machine Files : Copy AddClient.class, AddServerImpl_Stub.class, and AddServerIntf.class to a directory client machine. server machine Files: Copy AddServerIntf.class, AddServerImpl.class, AddServerImpl_Skel.class, AddServerImpl_Stub.class, and AddServer.class to a directory on the server machine.
References:
http://java.sun.com/developer/codesamples/rmi.html http://java.sun.com/docs/books/tutorial/rmi/index.html http://java.sun.com/docs/books/tutorial/rmi/overview.html http://java.sun.com/developer/codesamples/rmi.html#compute
For more details, visit http://java.sun.com/docs/books/tutorial/rmi/index.html
|
Responses
|
| Author: Shanthi M 24 May 2008 | Member Level: Diamond Points : 2 | I clearly understood about RMI. Thank You
|
|
|