Distributed Computing - Remote Method Invocation %28RMI%29

download Distributed Computing - Remote Method Invocation %28RMI%29

of 22

Transcript of Distributed Computing - Remote Method Invocation %28RMI%29

  • 8/8/2019 Distributed Computing - Remote Method Invocation %28RMI%29

    1/22

    Distributed Computing ViewRemote Method Invocation

  • 8/8/2019 Distributed Computing - Remote Method Invocation %28RMI%29

    2/22

    T opics Remote MethodInvocation

    Distributed System and Distributed Processes

    RMI ArchitectureRemote Interface and Remote ObjectImplementationCreate Publishing ObjectPublish Remote Object

  • 8/8/2019 Distributed Computing - Remote Method Invocation %28RMI%29

    3/22

    Distributed System and DistributedProcesses

    Distributed systems are easy to understandPerhaps to spread performance loadPerhaps to centralized processing

    Distributed systems are often difficult to designT here is more possibility of bad design affectingothersCan destroy the performance of a network

    Distributed system are an extension of OODOOD requires a task to be executed by the objectmost suitedDistributed systems allow that object to execute onthe most appropriate machine

  • 8/8/2019 Distributed Computing - Remote Method Invocation %28RMI%29

    4/22

    Page for diagrams

  • 8/8/2019 Distributed Computing - Remote Method Invocation %28RMI%29

    5/22

    L inking Distributed Objects

    Distributed systems need to appear seamlessUser should not need to be aware of distribution

    Just wants access to functionality

    Developer should not need to know implementationdetails

    Does not care what language objects were developed inDoes not care what platform objects are hosted on

  • 8/8/2019 Distributed Computing - Remote Method Invocation %28RMI%29

    6/22

    Page for diagrams

  • 8/8/2019 Distributed Computing - Remote Method Invocation %28RMI%29

    7/22

    Remote Method Invocation Architecture

    Java supports the distributed object paradigm inseveral ways

    RMI architecture is aimed at Java-to-Javacommunication

    Can also be used to communicate with other sources

    Made up of three basic layersStubs (object proxy) and skeletons (object adapter)T ransport L ayer (e.g. T CP)Remote Reference L ayer

  • 8/8/2019 Distributed Computing - Remote Method Invocation %28RMI%29

    8/22

    Page for diagrams

  • 8/8/2019 Distributed Computing - Remote Method Invocation %28RMI%29

    9/22

    Remote Method Invocation Architecture

    T he T ransport L ayer Responsible for creation and maintenance of connections

    Runs connections over sockets by default

    Can use custom sockets; e,g, SSL

    T he Remote Reference L ayer Responsible for management of remote objects

    Connects clients to remote objects

    Responsible for invocation of remote object methodsAn independent reference protocolProvides a transport stream to Stub/Skeleton L ayer

  • 8/8/2019 Distributed Computing - Remote Method Invocation %28RMI%29

    10/22

    Remote Method Invocation Architecture

    T he Stub L ayer Proxy representing interface to remote object for client

    Defines complete interface of remote implementation

    Appears to a client application as a local objectCommunicates with skeleton via Remote Reference L ayer

    T he Skeleton L ayer L ocal interface for remote object

    Interface between remote implementation and RemoteReference L ayer Communicates with stub via Remote Refernce L ayer Marshal any return values or exceptions

  • 8/8/2019 Distributed Computing - Remote Method Invocation %28RMI%29

    11/22

    Main Components in RMIT he basic RMI package comprises several classes and interfaces

    RemoteBase interface for the functionality of the remote object

    RemoteObjectT he base class of all remote objects

    RemoteServer Extends RemoteObject to provide basis for all remote servers

    UnicastRemoteObjectSupports point-to-point connection over T CP/IP

    RemoteExceptionBase class of exceptions for remote and distributed processing

    Remark:Import java.rmi.* --- Client and server codeImport java.rmi.server.* --- Server code only

  • 8/8/2019 Distributed Computing - Remote Method Invocation %28RMI%29

    12/22

    Declare the Remote Interface

    A remote interface defines a set of servicesMust conform to certain rules

    PublicExtend the Remote interface, directly or by inheritanceEvery method must throw RemoteException

    Defines the set of services that a client can expectCode:

    import java.rmi.*; public interface Speech extends Remote{

    public String sayHello() throws RemoteException;}

  • 8/8/2019 Distributed Computing - Remote Method Invocation %28RMI%29

    13/22

    Create a Remote Implementation (Impl)T he actual functionality is provided by implementationclassesT he classes implement the remote interface(s)

    Normal extends UnicastRemoteObjectCarries out some supporting processesMust implement appropriate interface(s)

    Remote implementation classes are instantiated on theserver

    An object instance providing services of a remote interface(Speech)Connected remotely to clients requiring those services

  • 8/8/2019 Distributed Computing - Remote Method Invocation %28RMI%29

    14/22

    Create a Remote Implementation (Impl)

    Code:

    import java.rmi.*;Import java.rmi.server.*;

    public class SpeechImpl extends UnicastRemoteObject implementsSpeech{ public SpeechImpl() throws RemoteException {}; public String sayHello() throws RemoteException{

    Return Hello;

    }}

    Must import RMIServer package

    Create a remote class that provides the Speech services

  • 8/8/2019 Distributed Computing - Remote Method Invocation %28RMI%29

    15/22

    Create a Remote Implementation (Impl)T he stubs and skeletons are created by the rmic tool

    Supplied with the Java JDK rmic SpeechImpl

    rmic creates a stub for use by the clientSpeechImpl_Stub.class

    rmic creates a skeleton for use by the remote objectSpeechImpl_Skel.class

    Inheriting from UnicastRemoteObjectConstructor will export the class

    Effectively make the stub availableT he class redefines several java.lang.Object methods

    .hashCode() , .equals() , and .toString()T hese are redefined to be used in a distributes environment

  • 8/8/2019 Distributed Computing - Remote Method Invocation %28RMI%29

    16/22

    Create the Publishing Process

    A publishing process exposes the remote object for useCreate an instance of object (remote object)Publishes the object via RMI Registry

    May simply be main method of implementation classMay be a separate Java class to carry out work

    T he RMI Registry is responsible for remote object publication

    Hold details linking names to instantiated objectsRelates to the static Naming classExpose bind (_) and rebind (_) methods

    L isten on a socket port for incoming request1099 by default

  • 8/8/2019 Distributed Computing - Remote Method Invocation %28RMI%29

    17/22

    Create a Publishing Process

    Code:

    import java.rmi.*; public class Pub lisher {

    public static void main(String args[]){

    try{SpeechImpl si = new SpeechImpl();

    Naming.rebind( T alker, si);}catch (Exception e){

    e.printStack T race();}

    }}

    Create an instance of the implementationclass

    Bind the remoteobject into theRMI Registry

  • 8/8/2019 Distributed Computing - Remote Method Invocation %28RMI%29

    18/22

    Start RMI Registry and Server T he Registry code is found in java/bin

    rmi regist ry utility

    start rmiregistry

    java Publisher

  • 8/8/2019 Distributed Computing - Remote Method Invocation %28RMI%29

    19/22

    Create the Client Application

    A client process needs to interact with the remote object for serviceL ocate an instance of remote object via the RMI Registry

    Only interested in finding any object that supplies servicesOnly has the name used when object was publishedDoes not know, or need to know the class used

    May also load an RMI Security Manager Controls what is allowed to be downloadedSimilar to control over applet downloading

    T he client program gets a reference (not object) to the remote objectReturned from a call to the Naming.lookup(_) method

    Reference is of type Remote that can be cast as appropriateActually a reference to the downloaded stub

  • 8/8/2019 Distributed Computing - Remote Method Invocation %28RMI%29

    20/22

    Page for diagrams

  • 8/8/2019 Distributed Computing - Remote Method Invocation %28RMI%29

    21/22

    Create a Publishing Process

    Code:

    import java.rmi.*; public class T heClient{

    public static void main(String args[]){

    try{Speech sp;System.setSecurityManager(new

    RMISecurityManager());sp=(Speech) Naming.lookup(rmi://loaclhost/ T alker);System.out.println(sp.sayHello());

    }catch (Exception e){

    e.printStack T race();}

    }}

    Create an objectvariable of theremote interface

    Use the remote methods

    Ask the Registryfor an instance of the remote object

  • 8/8/2019 Distributed Computing - Remote Method Invocation %28RMI%29

    22/22

    Steps in Setting up a RMI distributedSystem

    Setting up a distributed system using RMI comprises the followingsteps:

    Declare the remote interface (Speech)Create the class that implements the remote interface (SpeechImpl)Create the stubs and skeletons (from rmic)

    Export the class that implements the remote interfaceCreate the publishing application (Publisher)Start the RMI RegistryRun the publishing applicationCreate the client program

    T est, debug, releaseYou may need to repeat the testing and debugging a couple of times