SlideShare ist ein Scribd-Unternehmen logo
1 von 21
Remote Method Invocation
What is RMI?
• Networking taken to higher level.
• Distributed application
• Very powerful
• The design goal for the RMI architecture was
  to create a Java distributed object model that
  integrates naturally into the Java programming
  language and the local object model.
JAVA RMI Architecture

•   Interface – The heart of RMI
•   RMI Architecture Layers
•   Stub and Skeleton Layers
•   Remote Reference Layer
•   Transport Layer
Interface – the heart of RMI
Concept
To keep the definition of the behavior and implementation of behavior
   of an object separate from each other.
Why this separation?
To make the application distributed.
What is Client’s concern?
To know what are the services provided by the server. That means
   definition of the object behavior.
What is server’s concern?
To perform the service requested by the client. That means the
   implementation of the service .
What is the role of Interface?
Interface defines the behavior of the remote service.
Who defines the implementation?
Classes define the implementation. Server class.
Interface – the heart of RMI



Client Program    Server Program
Interface           Implementation
Interface – the heart of RMI
• So it is clear that we               <<Interface >>
  have to write an                          Service
  interface.
• The interface is part of
  the client side.
• The interface consists      Client                    Server
  of the definition of the   Service                    Service
                                                    Implementation
  services provided by        Proxy
  the server.
• The interface is also                 RMI Magic
  called service proxy.
RMI Architecture Layers
Stub and Skeleton Layer

• Lies just beneath the view of the Java developer.
• uses the Proxy design pattern .
• In the Proxy pattern, an object in one context is
  represented by another (the proxy) in a separate
  context.
• The proxy knows how to forward method calls
  between the participating objects.
• the stub class plays the role of the proxy.
• A skeleton is a helper class that understands how
  to communicate with the stub across the RMI
  link.
Stub and Skeleton Layer
Remote Reference Layer


• defines and supports the invocation semantics
  of the RMI connection.
• provides a RemoteRef object that represents
  the link to the remote service implementation
  object.
• stub objects use the invoke() method in
  RemoteRef to forward the method call
Transport Layer

• makes the connection
  between JVMs
• All connections are
  stream-based network
  connections that use
  TCP/IP.
• The RMI transport layer
  is designed to make a
  connection between
  clients and server
Naming Remote Objects
• "How does a client find an RMI remote
  service? "
• by using a naming or directory service.
• On a host machine, a server program creates a
  remote service by first creating a local object
  that implements that service.
• Next, it exports that object to RMI.
• After exporting, the server registers the object
  in the RMI Registry under a public name.
• RMI creates a listening service that waits for
  clients to connect and request the service.
Naming Remote Objects
• On the client side, the RMI Registry is
  accessed through the static class Naming.
• It provides the method lookup() that a client
  uses to query a registry.
• The method lookup() accepts a URL that
  specifies the server host name and the name
  of the desired service.
• The method returns a remote reference to the
  service object.
• The URL takes the form:
          rmi://<host_name> [:<name_service_port>]
  /<service_name>
Using RMI
A working RMI system is composed of several parts.
• Interface definitions for the remote services
• Implementations of the remote services
• Stub and Skeleton files
• A server to host the remote services
• An RMI Naming service that allows clients to find
  the remote services
• A class file provider (an HTTP or FTP server)
• A client program that needs the remote services
Steps for Building RMI System
1. Write and compile Java code for interfaces
2. Write and compile Java code for implementation
   classes
3. Generate Stub and Skeleton class files from the
   implementation classes
4. Write Java code for a remote service host
   program
5. Develop Java code for RMI client program
6. Install and run RMI system
CODE for Calculator RMI
       System
Interfaces

    public interface Calculator extends java.rmi.Remote
{
    public long add(long a, long b) throws java.rmi.RemoteException;

    public long sub(long a, long b) throws java.rmi.RemoteException;

    public long mul(long a, long b) throws java.rmi.RemoteException;

     public long div(long a, long b) throws java.rmi.RemoteException;
}

The Calculator interface defines all of the remote features offered by the service
Copy this file to your directory and compile it with the Java compiler.
Implementation
public class CalculatorImpl extends java.rmi.server.UnicastRemoteObject
   implements Calculator
 { public CalculatorImpl() throws java.rmi.RemoteException {
  super(); }

    public long add(long a, long b) throws java.rmi.RemoteException {
     return a + b; }

    public long sub(long a, long b) throws java.rmi.RemoteException {
     return a - b; }

     public long mul(long a, long b) throws java.rmi.RemoteException {
      return a * b; }

    public long div(long a, long b) throws java.rmi.RemoteException {
    return a / b; }
}
Stubs and Skeletons

• use the RMI compiler, rmic, to generate the
  stub and skeleton files
• The compiler runs on the remote service
  implementation class file
• >rmic CalculatorImpl
• After you run rmic you should find the file
  Calculator_Stub.class
Host Server
import java.rmi.Naming;
public class CalculatorServer {
  public CalculatorServer() {
 try {
  Calculator c = new CalculatorImpl();
  Naming.rebind("rmi://localhost:1099/CalculatorS
  ervice", c); }
 catch (Exception e) { System.out.println("Trouble: "
  + e); } }

 public static void main(String args[]) {
   new CalculatorServer(); } }
Client
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
public class CalculatorClient {
public static void main(String[] args) {
try {
       Calculator c = (Calculator) Naming.lookup( "rmi://localhost /CalculatorService");
    System.out.println( c.sub(4, 3) );
      System.out.println( c.add(4, 5) );
      System.out.println( c.mul(3, 6) );
      System.out.println( c.div(9, 3) ); }
catch (MalformedURLException murle) {
 System.out.println(); System.out.println( "MalformedURLException");
    System.out.println(murle); }
catch (RemoteException re) {
  System.out.println(); System.out.println( "RemoteException");
    System.out.println(re); }
catch (NotBoundException nbe) {
System.out.println();
Running the RMI System
• start three consoles, one for the server, one for the
  client, and one for the RMIRegistry.
• Start with the Registry. You must be in the directory
  that contains the classes you have written. From
  there, enter the following:
       rmiregistry
• In the second console start the server hosting the
  CalculatorService, and enter the following:
        >java CalculatorServer
• start the client program.
       >java CalculatorClient

Weitere ähnliche Inhalte

Was ist angesagt?

Distributed Programming using RMI
Distributed Programming using RMIDistributed Programming using RMI
Distributed Programming using RMI
backdoor
 
remote method invocation
remote method invocationremote method invocation
remote method invocation
Ravi Theja
 

Was ist angesagt? (20)

Rmi ppt
Rmi pptRmi ppt
Rmi ppt
 
Rmi
RmiRmi
Rmi
 
Java rmi
Java rmiJava rmi
Java rmi
 
Rmi
RmiRmi
Rmi
 
Java rmi tutorial
Java rmi tutorialJava rmi tutorial
Java rmi tutorial
 
Java RMI
Java RMIJava RMI
Java RMI
 
Rmi presentation
Rmi presentationRmi presentation
Rmi presentation
 
Java RMI Detailed Tutorial
Java RMI Detailed TutorialJava RMI Detailed Tutorial
Java RMI Detailed Tutorial
 
Remote Method Innovation (RMI) In JAVA
Remote Method Innovation (RMI) In JAVARemote Method Innovation (RMI) In JAVA
Remote Method Innovation (RMI) In JAVA
 
Remote Method Invocation (Java RMI)
Remote Method Invocation (Java RMI)Remote Method Invocation (Java RMI)
Remote Method Invocation (Java RMI)
 
Introduction To Rmi
Introduction To RmiIntroduction To Rmi
Introduction To Rmi
 
Rmi
RmiRmi
Rmi
 
Distributed Programming using RMI
Distributed Programming using RMIDistributed Programming using RMI
Distributed Programming using RMI
 
Java RMI Presentation
Java RMI PresentationJava RMI Presentation
Java RMI Presentation
 
Remote Method Invocation in JAVA
Remote Method Invocation in JAVARemote Method Invocation in JAVA
Remote Method Invocation in JAVA
 
Rmi ppt-2003
Rmi ppt-2003Rmi ppt-2003
Rmi ppt-2003
 
Java RMI(Remote Method Invocation)
Java RMI(Remote Method Invocation)Java RMI(Remote Method Invocation)
Java RMI(Remote Method Invocation)
 
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
 

Ähnlich wie DS

Distributed Programming using RMI
 Distributed Programming using RMI Distributed Programming using RMI
Distributed Programming using RMI
backdoor
 
Distributed Objects and JAVA
Distributed Objects and JAVADistributed Objects and JAVA
Distributed Objects and JAVA
elliando dias
 
remote method invocation
remote method invocationremote method invocation
remote method invocation
Arun Nair
 

Ähnlich wie DS (20)

Rmi
RmiRmi
Rmi
 
Remote method invocatiom
Remote method invocatiomRemote method invocatiom
Remote method invocatiom
 
Rmi
RmiRmi
Rmi
 
Remote Method Invocation, Advanced programming
Remote Method Invocation, Advanced programmingRemote Method Invocation, Advanced programming
Remote Method Invocation, Advanced programming
 
Java RMI
Java RMIJava RMI
Java RMI
 
Distributed Programming using RMI
 Distributed Programming using RMI Distributed Programming using RMI
Distributed Programming using RMI
 
RMI (Remote Method Invocation)
RMI (Remote Method Invocation)RMI (Remote Method Invocation)
RMI (Remote Method Invocation)
 
Distributed Objects and JAVA
Distributed Objects and JAVADistributed Objects and JAVA
Distributed Objects and JAVA
 
Remote Method Invocation
Remote Method InvocationRemote Method Invocation
Remote Method Invocation
 
Rmi3
Rmi3Rmi3
Rmi3
 
Module 3 remote method invocation-2
Module 3   remote method  invocation-2Module 3   remote method  invocation-2
Module 3 remote method invocation-2
 
17rmi
17rmi17rmi
17rmi
 
Javarmi 130925082348-phpapp01
Javarmi 130925082348-phpapp01Javarmi 130925082348-phpapp01
Javarmi 130925082348-phpapp01
 
Distributed objects
Distributed objectsDistributed objects
Distributed objects
 
ADB Lab Manual.docx
ADB Lab Manual.docxADB Lab Manual.docx
ADB Lab Manual.docx
 
Oracle docs rmi applications
Oracle docs rmi applicationsOracle docs rmi applications
Oracle docs rmi applications
 
Rmi
RmiRmi
Rmi
 
Rmi
RmiRmi
Rmi
 
Basic java
Basic java Basic java
Basic java
 
remote method invocation
remote method invocationremote method invocation
remote method invocation
 

Kürzlich hochgeladen

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Kürzlich hochgeladen (20)

Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 

DS

  • 1. Remote Method Invocation What is RMI? • Networking taken to higher level. • Distributed application • Very powerful • The design goal for the RMI architecture was to create a Java distributed object model that integrates naturally into the Java programming language and the local object model.
  • 2. JAVA RMI Architecture • Interface – The heart of RMI • RMI Architecture Layers • Stub and Skeleton Layers • Remote Reference Layer • Transport Layer
  • 3. Interface – the heart of RMI Concept To keep the definition of the behavior and implementation of behavior of an object separate from each other. Why this separation? To make the application distributed. What is Client’s concern? To know what are the services provided by the server. That means definition of the object behavior. What is server’s concern? To perform the service requested by the client. That means the implementation of the service . What is the role of Interface? Interface defines the behavior of the remote service. Who defines the implementation? Classes define the implementation. Server class.
  • 4. Interface – the heart of RMI Client Program Server Program Interface Implementation
  • 5. Interface – the heart of RMI • So it is clear that we <<Interface >> have to write an Service interface. • The interface is part of the client side. • The interface consists Client Server of the definition of the Service Service Implementation services provided by Proxy the server. • The interface is also RMI Magic called service proxy.
  • 7. Stub and Skeleton Layer • Lies just beneath the view of the Java developer. • uses the Proxy design pattern . • In the Proxy pattern, an object in one context is represented by another (the proxy) in a separate context. • The proxy knows how to forward method calls between the participating objects. • the stub class plays the role of the proxy. • A skeleton is a helper class that understands how to communicate with the stub across the RMI link.
  • 9. Remote Reference Layer • defines and supports the invocation semantics of the RMI connection. • provides a RemoteRef object that represents the link to the remote service implementation object. • stub objects use the invoke() method in RemoteRef to forward the method call
  • 10. Transport Layer • makes the connection between JVMs • All connections are stream-based network connections that use TCP/IP. • The RMI transport layer is designed to make a connection between clients and server
  • 11. Naming Remote Objects • "How does a client find an RMI remote service? " • by using a naming or directory service. • On a host machine, a server program creates a remote service by first creating a local object that implements that service. • Next, it exports that object to RMI. • After exporting, the server registers the object in the RMI Registry under a public name. • RMI creates a listening service that waits for clients to connect and request the service.
  • 12. Naming Remote Objects • On the client side, the RMI Registry is accessed through the static class Naming. • It provides the method lookup() that a client uses to query a registry. • The method lookup() accepts a URL that specifies the server host name and the name of the desired service. • The method returns a remote reference to the service object. • The URL takes the form: rmi://<host_name> [:<name_service_port>] /<service_name>
  • 13. Using RMI A working RMI system is composed of several parts. • Interface definitions for the remote services • Implementations of the remote services • Stub and Skeleton files • A server to host the remote services • An RMI Naming service that allows clients to find the remote services • A class file provider (an HTTP or FTP server) • A client program that needs the remote services
  • 14. Steps for Building RMI System 1. Write and compile Java code for interfaces 2. Write and compile Java code for implementation classes 3. Generate Stub and Skeleton class files from the implementation classes 4. Write Java code for a remote service host program 5. Develop Java code for RMI client program 6. Install and run RMI system
  • 15. CODE for Calculator RMI System
  • 16. Interfaces public interface Calculator extends java.rmi.Remote { public long add(long a, long b) throws java.rmi.RemoteException; public long sub(long a, long b) throws java.rmi.RemoteException; public long mul(long a, long b) throws java.rmi.RemoteException; public long div(long a, long b) throws java.rmi.RemoteException; } The Calculator interface defines all of the remote features offered by the service Copy this file to your directory and compile it with the Java compiler.
  • 17. Implementation public class CalculatorImpl extends java.rmi.server.UnicastRemoteObject implements Calculator { public CalculatorImpl() throws java.rmi.RemoteException { super(); } public long add(long a, long b) throws java.rmi.RemoteException { return a + b; } public long sub(long a, long b) throws java.rmi.RemoteException { return a - b; } public long mul(long a, long b) throws java.rmi.RemoteException { return a * b; } public long div(long a, long b) throws java.rmi.RemoteException { return a / b; } }
  • 18. Stubs and Skeletons • use the RMI compiler, rmic, to generate the stub and skeleton files • The compiler runs on the remote service implementation class file • >rmic CalculatorImpl • After you run rmic you should find the file Calculator_Stub.class
  • 19. Host Server import java.rmi.Naming; public class CalculatorServer { public CalculatorServer() { try { Calculator c = new CalculatorImpl(); Naming.rebind("rmi://localhost:1099/CalculatorS ervice", c); } catch (Exception e) { System.out.println("Trouble: " + e); } } public static void main(String args[]) { new CalculatorServer(); } }
  • 20. Client import java.rmi.Naming; import java.rmi.RemoteException; import java.net.MalformedURLException; import java.rmi.NotBoundException; public class CalculatorClient { public static void main(String[] args) { try { Calculator c = (Calculator) Naming.lookup( "rmi://localhost /CalculatorService"); System.out.println( c.sub(4, 3) ); System.out.println( c.add(4, 5) ); System.out.println( c.mul(3, 6) ); System.out.println( c.div(9, 3) ); } catch (MalformedURLException murle) { System.out.println(); System.out.println( "MalformedURLException"); System.out.println(murle); } catch (RemoteException re) { System.out.println(); System.out.println( "RemoteException"); System.out.println(re); } catch (NotBoundException nbe) { System.out.println();
  • 21. Running the RMI System • start three consoles, one for the server, one for the client, and one for the RMIRegistry. • Start with the Registry. You must be in the directory that contains the classes you have written. From there, enter the following: rmiregistry • In the second console start the server hosting the CalculatorService, and enter the following: >java CalculatorServer • start the client program. >java CalculatorClient