Distributed Computing: Dr. Nazneen Pendhari
Distributed Computing: Dr. Nazneen Pendhari
2
Remote
Method
Invocation
(RMI)
3
Remote
Method
Invocation
(RMI)
4
Understanding requirements for the distributed applications:
If any application performs these tasks, it can be distributed
application.
5
Java RMI Example
6
Java RMI Example
In this example, all the 6 steps are followed to create and run the
RMI application.
Remote ● The client application need only two files, remote interface and
client application.
Method In the RMI application, both client and server interacts with the
Invocation
●
remote interface.
(RMI) ● The client application invokes methods on the proxy object, RMI
sends the request to the remote JVM.
● The return value is sent back to the proxy object and then to the
client application
7
Java RMI Example
Remote
Method
Invocation
(RMI)
8
Java RMI Example
1) Create the remote interface
● For creating the remote interface, extend the Remote interface
and declare the RemoteException with all the methods of the
remote interface.
Remote ● Here, we are creating a remote interface that extends the Remote
interface.
Method There is only one method named add() and it declares
Invocation
●
RemoteException.
(RMI)
1. import java.rmi.*;
2. public interface Adder extends Remote{
3. public int add(int x,int y)throws RemoteException;
4. }
9
Java RMI Example
2) Provide the implementation of the remote interface
Remote ● For providing the implementation of the Remote interface, we
need to :
Method
Invocation ● Either extend the UnicastRemoteObject class,
(RMI)
● or use the exportObject() method of the
UnicastRemoteObject class
10
Java RMI Example
2) Provide the implementation of the remote interface (contd..)
● In case, we use extend the UnicastRemoteObject class, we must
define a constructor that declares RemoteException.
1. import java.rmi.*;
Remote 2. import java.rmi.server.*;
compiler.
Invocation ● The rmic tool invokes the RMI compiler and creates stub and
(RMI) skeleton objects.
1. rmic AdderRemote
12
Java RMI Example
Remote 4) Start the registry service by the rmiregistry tool
Method Now start the registry service by using the rmiregistry tool. If we
don't specify the port number, it uses a default port number.
Invocation
In this example, we are using the port number 5000.
(RMI)
1. rmiregistry 5000
13
Java RMI Example
5) Create and run the server application
●Now rmi services need to be hosted in a server process. The Naming class
provides methods to get and store the remote object. The Naming class
provides 5 methods.
●In this example, we are binding the remote object by the name sonoo.
●1. import java.rmi.*;
Remote ●2. import java.rmi.registry.*;
Method ●3. public class MyServer{
Invocation ●4. public static void main(String args[]){
(RMI) ●5. try{
●6. Adder stub=new AdderRemote();
●7. Naming.rebind("rmi://localhost:5000/sonoo",stub);
●8. }catch(Exception e){System.out.println(e);}
●9. }
●10. }
14
Java RMI Example
6) Create and run the client application
Remote ● At the client we are getting the stub object by the lookup() method
of the Naming class and invoking the method on this object.
Method
In this example, we are running the server and client applications,
Invocation
●
15
Java RMI Example
6) Create and run the client application (contd..)
● 1. import java.rmi.*;
● 2. public class MyClient{
Remote ● 3. public static void main(String args[]){
Method ● 4. try{
Invocation ● 5. Adder
stub=(Adder)Naming.lookup("rmi://localhost:5000/sonoo");
(RMI)
● 6. System.out.println(stub.add(34,4));
● 7. }catch(Exception e){}
● 8. }
● 9. }
16
For Running the Java RMI Example
1) compile all the java files
javac *.java
2)create stub and skeleton object by rmic tool
Remote rmic AdderRemote
Method 3)start rmi registry in one command prompt
Invocation rmiregistry 5000
(RMI) 4)start the server in another command prompt
java MyServer
5)start the client application in another command prompt
java MyClient
17
Output
Remote
Method
Invocation
(RMI)
18