[go: up one dir, main page]

0% found this document useful (0 votes)
5 views18 pages

Distributed Computing: Dr. Nazneen Pendhari

The document provides an overview of Remote Method Invocation (RMI) in distributed computing, detailing the requirements and steps to create an RMI application in Java. It outlines the process of creating a remote interface, implementing it, generating stub and skeleton objects, and running both server and client applications. Additionally, it includes code snippets and commands necessary for compiling and executing the RMI example.

Uploaded by

shaikhtaqu123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views18 pages

Distributed Computing: Dr. Nazneen Pendhari

The document provides an overview of Remote Method Invocation (RMI) in distributed computing, detailing the requirements and steps to create an RMI application in Java. It outlines the process of creating a remote interface, implementing it, generating stub and skeleton objects, and running both server and client applications. Additionally, it includes code snippets and commands necessary for compiling and executing the RMI example.

Uploaded by

shaikhtaqu123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Distributed Computing

Dr. Nazneen Pendhari


Remote
Method
Invocation
(RMI)

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.

Remote 1. The application need to locate the remote method


Method 2. It need to provide the communication with the remote
Invocation objects, and
3. The application need to load the class definitions for the
(RMI) objects.

● The RMI application have all these features, so it is called the


distributed application.

5
Java RMI Example

1. Create the remote interface


Provide the implementation of the remote interface
Remote
2.

Compile the implementation class and create the stub and


Method
3.

skeleton objects using the rmic tool


Invocation 4. Start the registry service by rmiregistry tool
(RMI) 5. Create and start the remote application
6. Create and start the client application

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.*;

Method 3. public class AdderRemote extends UnicastRemoteObject


implements Adde
Invocation r{
(RMI) 4. AdderRemote()throws RemoteException{
5. super();
6. }
7. public int add(int x,int y){return x+y;}
8. }
11
Java RMI Example
3) create the stub and skeleton objects using the rmic tool.
Remote
Next step is to create stub and skeleton objects using the rmi
Method ●

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

in the same machine so we are using localhost.


(RMI) ● If we want to access the remote object from another machine,
then change the localhost to the host name (or IP address) where
the remote object is located.

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

You might also like