Remote Method Invocation

1 / 20
About This Presentation
Title:

Remote Method Invocation

Description:

Outline how to develop an RMI program. What is the purpose of the registry? ... Have to agree format of data packets. Have to pack and unpack data ... –

Number of Views:32
Avg rating:3.0/5.0
Slides: 21
Provided by: chris520
Category:

less

Transcript and Presenter's Notes

Title: Remote Method Invocation


1
Remote Method Invocation
  • Revision of
  • Problems of distributed systems
  • Socket use
  • If you understand this, you will be able to
  • Explain how RMI operates
  • List RMI advantages disadvantages
  • Outline how to develop an RMI program
  • What is the purpose of the registry?

2
Why Sockets are a pain
  • Program structured around communication
  • send() receive() called explicitly
  • Have to agree format of data packets
  • Have to pack and unpack data
  • Dont handle finding a service

3
What is RMI?
  • Remote Method Invocation
  • Purpose
  • Provides communication between distributed
    programs
  • Communication looks like method calls to local
    objects
  • Hides the fact that the system is distributed
  • Why not just use sockets?
  • Sockets are a flexible, basic communication
    mechanism
  • But program must encode, decode handle messages
  • Tedious and potentially error-prone.

4
Goals
  • Make remote objects look like local objects.
  • Make any unavoidable differences clear.
  • Produce reliable, distributed applications.
  • Preserve Java type-safety and security.

Stub
Remote object
local client
remote Virtual Machine
Registry
Request object
Register object
5
Disadvantages
  • Local and remote objects must use Java
  • Overhead greater than sockets
  • For simple applications
  • But for complex applications
  • RMI management may reduce overhead over
    programmer-managed sockets

6
Key Concept The Interface
  • Like a class whose methods have no body
  • Interface variables can refer to any object that
    implements the interface.
  • public interface Contract
  • boolean buyBook(int cost)
  • public class Seller implements Contract
  • boolean buyBook(int cost) / implementation /
  • Contract bookshop
  • bookshop new Seller()
  • bookshop.buyBook(21)

7
The Interface
Main program
Object implementing interface
Factory object delivering implementation
Contract defined by
Interface
8
Stubs and Marshalling 1
Remote Object
remote Virtual Machine
stub
local client
  • A stub has the same interfaces as the remote
    object
  • When a stub's method is called, it
  • connects to the VM containing the remote object.
  • marshals (sends) parameters to the remote VM
  • waits for the result of the method call
  • unmarshals (reads) the return value or exception
  • returns the value to the caller
  • rmic compiler generates stubs from the interface

9
RMI Distributed Problems
  • Locate remote objects
  • rmiregistry contains registered remote objects
  • Remote objects can be passed to/returned by
    methods
  • Communicate with remote objects
  • Details of communication handled by RMI
  • Communication is a standard Java method
    invocation.
  • Pass objects as parameters or return values
  • RMI can transfer full object code data
  • Be notified of errors
  • Remote methods throw exceptions for RMI errors

10
Typical RMI Application
  • The Server
  • Create several remote objects
  • Make references to those remote objects
    accessible
  • Publish in the RMI registry
  • Wait for clients to invoke methods on those
    objects.
  • The Client
  • Request remote references to remote objects
  • Invoke methods on them.
  • RMI
  • Provides the mechanism for exchanging
    information.

11
Building an RMI system
  • Server
  • Define remote class methods by a public interface
  • The remote interface extends java.rmi.Remote
  • Methods throw java.rmi.RemoteException
  • Write the object implementation server classes

12
Implementing Object Server
  • A remote object implementation class must
  • Implement at least one remote interface.
  • Define the constructor for the remote object.
  • Write the remote method implementations.
  • A "server" class has a main method that
  • Creates and install a security manager
  • Creates a remote object implementation
  • Binds the instance to a name in the rmiregistry.

13
Example
  • Remote Interface
  • public interface RMIOXOInterface extends Remote
  • public String getCell(int r, int c) throws
    RemoteException
  • public void setCell(String val, int r, int c)
  • throws RemoteException

14
Remote Object and Server
  • public class RMIOXOImplement
  • extends UnicastRemoteObject
  • implements RMIOXOInterface
  • public RMIOXOImplement()
  • throws java.rmi.RemoteException

15
Main routine
  • public static void main(String args)
  • // Create and install a security manager
  • if (System.getSecurityManager() null)
  • System.setSecurityManager(new
    RMISecurityManager())
  • try
  • RMIOXOImplement obj new RMIOXOImplement ()
  • // Link this object to the name "RemoteOXO"
  • Naming.rebind("RemoteOXO", obj)
  • catch (Exception e) / example ignores errors
    /
  • // wait until time to finish

16
Interface Methods
  • // Implement the methods required by the
    interface
  • public String getCell(int r, int c) throws
    RemoteException
  • public void setCell(String val, int r, int c)
    throws RemoteException

17
Client using the remote object
  • RMIOXOInterface obj null
  • try
  • obj (RMIOXOInterface)Naming.lookup("RemoteOXO")
  • catch( RemoteException ex)
  • System.out.println("Exception accessing
    RemoteOXO")
  • // use the object just like an ordinary object
  • obj.setCell(X,1,1) // grab the
    centre cell for X

18
Problems
  • Thread safety
  • remote object must be safe when remote method
    calls made concurrently.
  • Lost Objects
  • A remote reference may refer to dead object.
  • Using such a reference will generate a
    RemoteException.
  • Firewalls
  • The RMI transport layer normally uses direct
    sockets
  • Can be blocked by firewall
  • RMI provides HTTP-based mechanisms to enable a
    client behind a firewall to invoke a method on a
    remote object.

19
Versioning
  • A particular problem for distributed systems
  • All systems evolve
  • Normal programs check support during
    installation.
  • Distributed systems
  • Use components from different vendors
  • Components run on different machines
  • Distribution of revisions takes time and is
    voluntary
  • Problems if methods provided by an interface
    change

20
Summary
  • RMI call methods on remote objects
  • Just like using local objects
  • Get a reference to the object via registry
  • Can always get RemoteException
  • Hides data communication
  • May have more overhead than sockets

21
The Naming Class
  • Registry
  • Link names to server objects
  • java.rmi.Naming class
  • Simplifies access to any remote object registry
  • Methods for storing getting links to objects

22
Methods for handling Names
  • bind associates a name with a remote object
  • rebind modifies the object associated with a name
  • lookup object by name, getting a reference
  • unbind removes the association
  • list obtains a list of available names
  • Throws AccessException
  • If you dont have rights to use the method
  • e.g.
  • only clients on the registrys host can bind or
    unbind.

23
Problems
  • A running applet has loaded some classes when the
    web server is updated with a newer version.
  • The remaining classes could be inconsistent.
  • A running application uses RMI call to return an
    object for a different version of the class.

24
Local and Remote Objects
  • Similarities
  • A remote object reference can be
  • passed as an argument
  • returned as a result
  • Can cast a remote object to its remote interfaces
  • The instanceof operator checks if a remote object
    supports an interface

25
Local and Remote Objects
  • Differences
  • Interact with remote interfaces,
  • Not the implementation classes of those
    interfaces.
  • Local arguments to, and results from, a remote
    method call are passed by copy rather than by
    reference
  • references to objects are only useful in a single
    VM.
  • A remote object is passed by reference, not by
    copying the actual remote implementation.
  • Handle extra exceptions when calling a remote
    method.

26
Compiling and Running RMI
  • Use rmic to generate stubs
  • Start the RMI registry
  • Start the server (registers the object)
  • Run the client (obtains uses the object)

Remote Object
local client
stub
remote Virtual Machine
Registry
27
Common Features
  • The Name parameter
  • //hostport/name
  • host is location of the desired registry (remote
    or local)
  • port is the port where the registry accepts calls
  • name is a simple string to be looked up or bound.
  • The default port is 1099, the "well-known" port
    that RMI's registry, rmiregistry, uses.
  • AccessException is thrown unless the caller has
    permission to execute the specific operation.
  • e.g.
  • only clients on the registrys host can bind or
    unbind.

28
Object Activation
  • RMI supports long-lived persistent objects.
  • Objects cannot remain active forever
  • An active object uses resources
  • RMI allows objects to persist to long term memory
    - disk
  • Clients may store persistent references to
    objects so that communication can be
    re-established after a system crash.
  • Object activation supports persistent references
    to objects and activating objects when needed.

29
RMI Error Handling
  • "at most once" semantics for remote calls.
  • A call to an activatable remote object is sent at
    most once.
  • If a RemoteException is thrown, the remote method
    executed no more than once (possibly not at all).
Write a Comment
User Comments (0)
About PowerShow.com