SlideShare a Scribd company logo
1 of 23
REMOTE METHOD INVOCATION
BY
S.Veni, M.Sc.,
Assistant Professor
REMOTE METHOD INVOCATION
 The RMI (Remote Method Invocation) is an API that provides a
mechanism to create distributed application in java. The RMI allows an
object to invoke methods on an object running in another JVM.
 The RMI provides remote communication between the applications
using two objects stub and skeleton.
UNDERSTANDINNG STUB AND
SKELETON
 RMI uses stub and skeleton object for communication with the remote
object.
 A remote object is an object whose method can be invoked from
another JVM. Let's understand the stub and skeleton objects:
STUB
 The stub is an object, acts as a gateway for the client side. All the
outgoing requests are routed through it. It resides at the client side and
represents the remote object. When the caller invokes method on the
stub object, it does the following tasks:
 It initiates a connection with remote Virtual Machine (JVM),
 It writes and transmits (marshals) the parameters to the remote Virtual
Machine (JVM),
 It waits for the result
 It reads (unmarshals) the return value or exception, and
 It finally, returns the value to the caller.
SKELETON
 The skeleton is an object, acts as a gateway for the server side object.
All the incoming requests are routed through it. When the skeleton
receives the incoming request, it does the following tasks:
 It reads the parameter for the remote method
 It invokes the method on the actual remote object, and
 It writes and transmits (marshals) the result to the caller.
Using RMI to invoke a method of a remote object
UNDERSTANDING REQUIREMENTS FOR
THE DISTRIBUTED APPLICATIONS
 If any application performs these tasks, it can be distributed application.
 .The application need to locate the remote method
 It need to provide the communication with the remote objects, and
 The application need to load the class definitions for the objects.
 The RMI application have all these features, so it is called the
distributed application.
JAVA RMI EXAMPLE
 The is given the 6 steps to write the RMI program.
 Create the remote interface
 Provide the implementation of the remote interface
 Compile the implementation class and create the stub and skeleton
objects using the rmic tool
 Start the registry service by rmiregistry tool
 Create and start the remote application
 Create and start the client application
JAVA RMI EXAMPLE
 In this example, we have followed all the 6 steps to create and run the
rmi application. The client application need only two files, remote
interface and client application. In the rmi application, both client and
server interacts with the remote interface. 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.
CREATE REMOTE INTERFACE
 For creating the remote interface, extend the Remote interface and
declare the RemoteException with all the methods of the remote
interface. Here, we are creating a remote interface that extends the
Remote interface. There is only one method named add() and it declares
RemoteException
import java.rmi.*;
public interface Adder extends Remote{
public int add(int x,int y)throws RemoteException;
}
PROVIDE THE IMPLEMENTATION OF THE REMOTE
INTERFACE
 Now provide the implementation of the remote interface.
 For providing the implementation of the Remote interface, we need to
 Either extend the UnicastRemoteObject class,
 or use the exportObject() method of the UnicastRemoteObject class
 In case, you extend the UnicastRemoteObject class, you must define a constructor that declares
RemoteException.
import java.rmi.*;
import java.rmi.server.*;
public class AdderRemote extends UnicastRemoteObject implements Adder{
AdderRemote()throws RemoteException{
super();
}
public int add(int x,int y){return x+y;}
}
CREATE THE STUB AND SKELETON OBJECTS USING
THE RMIC TOOL.
 Next step is to create stub and skeleton objects using the rmi compiler.
The rmic tool invokes the RMI compiler and creates stub and skeleton
objects.
rmic AdderRemote
START THE REGISTRY SERVICE BY
THE RMIREGISTRY TOOL
 Now start the registry service by using the rmiregistry tool. If you don't
specify the port number, it uses a default port number. In this example,
we are using the port number 5000.
rmiregistry 5000
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.
 It returns the reference of the remote object.
 It binds the remote object with the given
name.
 It destroys the remote object which is bound
with the given name.
 It binds the remote object to the new name.
 It returns an array of the names of the
remote objects bound in the registry.
 public static java.rmi.Remote
lookup(java.lang.String) throws
java.rmi.NotBoundException,
java.net.MalformedURLException,
java.rmi.RemoteException;
 Public static void bind(java.lang.String,
java.rmi.Remote) throws
java.rmi.AlreadyBoundException,
java.net.MalformedURLException,
java.rmi.RemoteException;
 public static void
unbind(java.lang.String) throws
java.rmi.RemoteException,
java.rmi.NotBoundException,
java.net.MalformedURLException;
 public static void
rebind(java.lang.String,
java.rmi.Remote) throws
java.rmi.RemoteException,
java.net.MalformedURLException;
 public static java.lang.String[]
list(java.lang.String) throws
java.rmi.RemoteException,
java.net.MalformedURLException;
In this example, we are binding the remote object by the name so on.
import java.rmi.*;
import java.rmi.registry.*;
public class MyServer{
public static void main(String args[]){
try{
Adder stub=new AdderRemote();
Naming.rebind("rmi://localhost:5000/sonoo",stub);
}catch(Exception e){System.out.println(e);}
}
}
CREATE AND RUN THE CLIENT
APPLICATION
 At the client we are getting the stub object by the lookup() method of the Naming class and
invoking the method on this object.
 In this example, we are running the server and client applications, in the same machine so
we are using localhost.
 If you want to access the remote object from another machine, change the localhost to the
host name (or IP address) where the remote object is located.
import java.rmi.*;
public class MyClient{
public static void main(String args[]){
try{
Adder stub=(Adder)Naming.lookup("rmi://localhost:5000/sonoo");
System.out.println(stub.add(34,4));
}catch(Exception e){}
}
}
For running this rmi example,
1) compile all the java files
javac *.java
2)create stub and skeleton object by rmic tool
rmic AdderRemote
3)start rmi registry in one command prompt
rmiregistry 5000
4)start the server in another command prompt
java MyServer
5)start the client application in another command prompt
java MyClient
Remote method invocation
Remote method invocation
Remote method invocation

More Related Content

What's hot

Chapter 8 - Exceptions and Assertions Edit summary
Chapter 8 - Exceptions and Assertions  Edit summaryChapter 8 - Exceptions and Assertions  Edit summary
Chapter 8 - Exceptions and Assertions Edit summaryEduardo Bergavera
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)nirajmandaliya
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaMonika Mishra
 
Chapter 9 Interface
Chapter 9 InterfaceChapter 9 Interface
Chapter 9 InterfaceOUM SAOKOSAL
 
Built in classes in java
Built in classes in javaBuilt in classes in java
Built in classes in javaMahmoud Ali
 
Core java notes with examples
Core java notes with examplesCore java notes with examples
Core java notes with examplesbindur87
 
Clean Code�Chapter 3. (1)
Clean Code�Chapter 3. (1)Clean Code�Chapter 3. (1)
Clean Code�Chapter 3. (1)Felix Chen
 
Generics and collections in Java
Generics and collections in JavaGenerics and collections in Java
Generics and collections in JavaGurpreet singh
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Uzair Salman
 
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building BlocksOCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building Blocksİbrahim Kürce
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singhdheeraj_cse
 
Java se 8 fundamentals
Java se 8 fundamentalsJava se 8 fundamentals
Java se 8 fundamentalsmegharajk
 
OCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & StatementsOCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & Statementsİbrahim Kürce
 
java: basics, user input, data type, constructor
java:  basics, user input, data type, constructorjava:  basics, user input, data type, constructor
java: basics, user input, data type, constructorShivam Singhal
 

What's hot (20)

Chapter 8 - Exceptions and Assertions Edit summary
Chapter 8 - Exceptions and Assertions  Edit summaryChapter 8 - Exceptions and Assertions  Edit summary
Chapter 8 - Exceptions and Assertions Edit summary
 
Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)Object oriented concepts & programming (2620003)
Object oriented concepts & programming (2620003)
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Java interface
Java interfaceJava interface
Java interface
 
Chapter 9 Interface
Chapter 9 InterfaceChapter 9 Interface
Chapter 9 Interface
 
Built in classes in java
Built in classes in javaBuilt in classes in java
Built in classes in java
 
Core java notes with examples
Core java notes with examplesCore java notes with examples
Core java notes with examples
 
Rmi
RmiRmi
Rmi
 
Clean Code�Chapter 3. (1)
Clean Code�Chapter 3. (1)Clean Code�Chapter 3. (1)
Clean Code�Chapter 3. (1)
 
Generics and collections in Java
Generics and collections in JavaGenerics and collections in Java
Generics and collections in Java
 
Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes Object Oriented Programming using JAVA Notes
Object Oriented Programming using JAVA Notes
 
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building BlocksOCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
 
Easy mock
Easy mockEasy mock
Easy mock
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
 
Java se 8 fundamentals
Java se 8 fundamentalsJava se 8 fundamentals
Java se 8 fundamentals
 
Java basic
Java basicJava basic
Java basic
 
OCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & StatementsOCA Java SE 8 Exam Chapter 2 Operators & Statements
OCA Java SE 8 Exam Chapter 2 Operators & Statements
 
java: basics, user input, data type, constructor
java:  basics, user input, data type, constructorjava:  basics, user input, data type, constructor
java: basics, user input, data type, constructor
 
Opps
OppsOpps
Opps
 

Similar to Remote method invocation

Similar to Remote method invocation (20)

ADB Lab Manual.docx
ADB Lab Manual.docxADB Lab Manual.docx
ADB Lab Manual.docx
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
 
Remote method invocatiom
Remote method invocatiomRemote method invocatiom
Remote method invocatiom
 
Java rmi
Java rmiJava rmi
Java rmi
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
 
Java RMI
Java RMIJava RMI
Java RMI
 
Remote method invocation
Remote method invocationRemote method invocation
Remote method invocation
 
Rmi ppt
Rmi pptRmi ppt
Rmi ppt
 
RMI (Remote Method Invocation)
RMI (Remote Method Invocation)RMI (Remote Method Invocation)
RMI (Remote Method Invocation)
 
#4 (Remote Method Invocation)
#4 (Remote Method Invocation)#4 (Remote Method Invocation)
#4 (Remote Method Invocation)
 
Remote Method Invocation in JAVA
Remote Method Invocation in JAVARemote Method Invocation in JAVA
Remote Method Invocation in JAVA
 
Rmi
RmiRmi
Rmi
 
Rmi
RmiRmi
Rmi
 
Module 3 remote method invocation-2
Module 3   remote method  invocation-2Module 3   remote method  invocation-2
Module 3 remote method invocation-2
 
Rmi
RmiRmi
Rmi
 
Rmi3
Rmi3Rmi3
Rmi3
 
Remote Method Invocation (Java RMI)
Remote Method Invocation (Java RMI)Remote Method Invocation (Java RMI)
Remote Method Invocation (Java RMI)
 
Distributed Objects and JAVA
Distributed Objects and JAVADistributed Objects and JAVA
Distributed Objects and JAVA
 
Java rmi tutorial
Java rmi tutorialJava rmi tutorial
Java rmi tutorial
 
Oracle docs rmi applications
Oracle docs rmi applicationsOracle docs rmi applications
Oracle docs rmi applications
 

Recently uploaded

Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 

Recently uploaded (20)

Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 

Remote method invocation

  • 1. REMOTE METHOD INVOCATION BY S.Veni, M.Sc., Assistant Professor
  • 2. REMOTE METHOD INVOCATION  The RMI (Remote Method Invocation) is an API that provides a mechanism to create distributed application in java. The RMI allows an object to invoke methods on an object running in another JVM.  The RMI provides remote communication between the applications using two objects stub and skeleton.
  • 3. UNDERSTANDINNG STUB AND SKELETON  RMI uses stub and skeleton object for communication with the remote object.  A remote object is an object whose method can be invoked from another JVM. Let's understand the stub and skeleton objects:
  • 4. STUB  The stub is an object, acts as a gateway for the client side. All the outgoing requests are routed through it. It resides at the client side and represents the remote object. When the caller invokes method on the stub object, it does the following tasks:  It initiates a connection with remote Virtual Machine (JVM),  It writes and transmits (marshals) the parameters to the remote Virtual Machine (JVM),  It waits for the result  It reads (unmarshals) the return value or exception, and  It finally, returns the value to the caller.
  • 5. SKELETON  The skeleton is an object, acts as a gateway for the server side object. All the incoming requests are routed through it. When the skeleton receives the incoming request, it does the following tasks:  It reads the parameter for the remote method  It invokes the method on the actual remote object, and  It writes and transmits (marshals) the result to the caller.
  • 6.
  • 7. Using RMI to invoke a method of a remote object
  • 8. UNDERSTANDING REQUIREMENTS FOR THE DISTRIBUTED APPLICATIONS  If any application performs these tasks, it can be distributed application.  .The application need to locate the remote method  It need to provide the communication with the remote objects, and  The application need to load the class definitions for the objects.  The RMI application have all these features, so it is called the distributed application.
  • 9. JAVA RMI EXAMPLE  The is given the 6 steps to write the RMI program.  Create the remote interface  Provide the implementation of the remote interface  Compile the implementation class and create the stub and skeleton objects using the rmic tool  Start the registry service by rmiregistry tool  Create and start the remote application  Create and start the client application
  • 10. JAVA RMI EXAMPLE  In this example, we have followed all the 6 steps to create and run the rmi application. The client application need only two files, remote interface and client application. In the rmi application, both client and server interacts with the remote interface. 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.
  • 11.
  • 12. CREATE REMOTE INTERFACE  For creating the remote interface, extend the Remote interface and declare the RemoteException with all the methods of the remote interface. Here, we are creating a remote interface that extends the Remote interface. There is only one method named add() and it declares RemoteException import java.rmi.*; public interface Adder extends Remote{ public int add(int x,int y)throws RemoteException; }
  • 13. PROVIDE THE IMPLEMENTATION OF THE REMOTE INTERFACE  Now provide the implementation of the remote interface.  For providing the implementation of the Remote interface, we need to  Either extend the UnicastRemoteObject class,  or use the exportObject() method of the UnicastRemoteObject class  In case, you extend the UnicastRemoteObject class, you must define a constructor that declares RemoteException. import java.rmi.*; import java.rmi.server.*; public class AdderRemote extends UnicastRemoteObject implements Adder{ AdderRemote()throws RemoteException{ super(); } public int add(int x,int y){return x+y;} }
  • 14. CREATE THE STUB AND SKELETON OBJECTS USING THE RMIC TOOL.  Next step is to create stub and skeleton objects using the rmi compiler. The rmic tool invokes the RMI compiler and creates stub and skeleton objects. rmic AdderRemote
  • 15. START THE REGISTRY SERVICE BY THE RMIREGISTRY TOOL  Now start the registry service by using the rmiregistry tool. If you don't specify the port number, it uses a default port number. In this example, we are using the port number 5000. rmiregistry 5000
  • 16. 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.
  • 17.  It returns the reference of the remote object.  It binds the remote object with the given name.  It destroys the remote object which is bound with the given name.  It binds the remote object to the new name.  It returns an array of the names of the remote objects bound in the registry.  public static java.rmi.Remote lookup(java.lang.String) throws java.rmi.NotBoundException, java.net.MalformedURLException, java.rmi.RemoteException;  Public static void bind(java.lang.String, java.rmi.Remote) throws java.rmi.AlreadyBoundException, java.net.MalformedURLException, java.rmi.RemoteException;  public static void unbind(java.lang.String) throws java.rmi.RemoteException, java.rmi.NotBoundException, java.net.MalformedURLException;  public static void rebind(java.lang.String, java.rmi.Remote) throws java.rmi.RemoteException, java.net.MalformedURLException;  public static java.lang.String[] list(java.lang.String) throws java.rmi.RemoteException, java.net.MalformedURLException;
  • 18. In this example, we are binding the remote object by the name so on. import java.rmi.*; import java.rmi.registry.*; public class MyServer{ public static void main(String args[]){ try{ Adder stub=new AdderRemote(); Naming.rebind("rmi://localhost:5000/sonoo",stub); }catch(Exception e){System.out.println(e);} } }
  • 19. CREATE AND RUN THE CLIENT APPLICATION  At the client we are getting the stub object by the lookup() method of the Naming class and invoking the method on this object.  In this example, we are running the server and client applications, in the same machine so we are using localhost.  If you want to access the remote object from another machine, change the localhost to the host name (or IP address) where the remote object is located. import java.rmi.*; public class MyClient{ public static void main(String args[]){ try{ Adder stub=(Adder)Naming.lookup("rmi://localhost:5000/sonoo"); System.out.println(stub.add(34,4)); }catch(Exception e){} } }
  • 20. For running this rmi example, 1) compile all the java files javac *.java 2)create stub and skeleton object by rmic tool rmic AdderRemote 3)start rmi registry in one command prompt rmiregistry 5000 4)start the server in another command prompt java MyServer 5)start the client application in another command prompt java MyClient