You are on page 1of 22

Distributed Computing View

Remote Method Invocation


Topics – Remote Method
Invocation
 Distributed System and Distributed Processes
 RMI Architecture
 Remote Interface and Remote Object
Implementation
 Create Publishing Object
 Publish Remote Object
Distributed System and Distributed
Processes
 Distributed systems are easy to understand
 Perhaps to spread performance load
 Perhaps to centralized processing
 Distributed systems are often difficult to design
 There is more possibility of bad design affecting others
 Can destroy the performance of a network
 Distributed system are an extension of OOD
 OOD requires a task to be executed by the object most
suited
 Distributed systems allow that object to execute on the
most appropriate machine
Page for diagrams
Linking Distributed Objects
 Distributed systems need to appear seamless
 User should not need to be aware of distribution
 Just wants access to functionality
 Developer should not need to know implementation
details
 Does not care what language objects were developed in
 Does not care what platform objects are hosted on
Page for diagrams
Remote Method Invocation Architecture
 Java supports the distributed object paradigm in
several ways
 RMI architecture is aimed at Java-to-Java
communication
 Can also be used to communicate with other sources
 Made up of three basic layers
 Stubs (object proxy) and skeletons (object adapter)
 Transport Layer (e.g. TCP)
 Remote Reference Layer
Page for diagrams
Remote Method Invocation Architecture
 The Transport Layer
 Responsible for creation and maintenance of
connections
 Runs connections over sockets by default
 Can use custom sockets; e,g, SSL
 The Remote Reference Layer
 Responsible for management of remote objects
 Connects clients to remote objects
 Responsible for invocation of remote object methods
 An independent reference protocol
 Provides a transport stream to Stub/Skeleton Layer
Remote Method Invocation Architecture
 The Stub Layer
 Proxy representing interface to remote object for client
 Defines complete interface of remote implementation
 Appears to a client application as a local object
 Communicates with skeleton via Remote Reference Layer
 The Skeleton Layer
 Local interface for remote object
 Interface between remote implementation and Remote Referen
ce Layer
 Communicates with stub via Remote Refernce Layer
 Marshal any return values or exceptions
Main Components in RMI
 The basic RMI package comprises several classes and interfaces
 Remote
 Base interface for the functionality of the remote object
 RemoteObject
 The base class of all remote objects
 RemoteServer
 Extends RemoteObject to provide basis for all remote servers
 UnicastRemoteObject
 Supports point-to-point connection over TCP/IP
 RemoteException
 Base class of exceptions for remote and distributed processing
 Remark:
 Import java.rmi.* --- Client and server code
 Import java.rmi.server.* --- Server code only
Declare the Remote Interface

 A remote interface defines a set of services


 Must conform to certain rules
 Public

 Extend the Remote interface, directly or by inheritance

 Every method must throw RemoteException

 Defines the set of services that a client can expect


 Code:
import java.rmi.*;
public interface Speech extends Remote{
public String sayHello() throws RemoteException;
}
Create a Remote Implementation (Impl)
 The actual functionality is provided by implementation cl
asses
 The classes implement the remote interface(s)
 Normal extends UnicastRemoteObject
 Carries out some supporting processes
 Must implement appropriate interface(s)
 Remote implementation classes are instantiated on the ser
ver
 An object instance providing services of a remote interface (Spe
ech)
 Connected remotely to clients requiring those services
Create a Remote Implementation (Impl)
Must import RMI Create a remote class that
 Code: Server package provides the Speech services

import java.rmi.*;
Import java.rmi.server.*;
public class SpeechImpl extends UnicastRemoteObject implements
Speech{
public SpeechImpl() throws RemoteException {};
public String sayHello() throws RemoteException{
Return “Hello”;
}
}
Create a Remote Implementation (Impl)
 The stubs and skeletons are created by the rmic tool
 Supplied with the Java JDK
 rmic SpeechImpl
 rmic creates a stub for use by the client
 SpeechImpl_Stub.class
 rmic creates a skeleton for use by the remote object
 SpeechImpl_Skel.class
 Inheriting from UnicastRemoteObject
 Constructor will export the class
 Effectively make the stub available
 The class redefines several java.lang.Object methods
 .hashCode() , .equals() , and .toString()
 These are redefined to be used in a distributes environment
Create the Publishing Process
 A publishing process exposes the remote object for use
 Create an instance of object (remote object)
 Publishes the object via RMI Registry
 May simply be main method of implementation class
 May be a separate Java class to carry out work
 The RMI Registry is responsible for remote object
publication
 Hold details linking names to instantiated objects
 Relates to the static Naming class
 Expose bind (_) and rebind (_) methods
 Listen on a socket port for incoming request
 1099 by default
Create a Publishing Process
 Code:
Create an instance of
import java.rmi.*; the implementation
public class Publisher{ class
public static void main(String args[]){
try{
SpeechImpl si = new SpeechImpl();
Naming.rebind(“Talker”, si);
}
catch (Exception e){
e.printStackTrace();
} Bind the remote
} object into the
} RMI Registry
Start RMI Registry and Server
 The Registry code is found in java/bin
 rmiregistry utility

start rmiregistry
java Publisher
Create the Client Application
 A client process needs to interact with the remote object for service
 Locate an instance of remote object via the RMI Registry
 Only interested in finding any object that supplies services
 Only has the name used when object was published
 Does not know, or need to know the class used
 May also load an RMI Security Manager
 Controls what is allowed to be downloaded
 Similar to control over applet downloading
 The client program gets a reference (not object) to the remote object
 Returned from a call to the Naming.lookup(_) method
 Reference is of type Remote that can be cast as appropriate
 Actually a reference to the downloaded stub
Page for diagrams
Create a Publishing Process
 Code:

import java.rmi.*;
public class TheClient{ Create an object
public static void main(String args[]){ variable of the
try{ remote interface
Speech sp;
System.setSecurityManager(new RMISecurityManager());Ask the Registry
sp=(Speech) Naming.lookup(“rmi://loaclhost/Talker”); for an instance of
System.out.println(sp.sayHello()); the remote object
}
catch (Exception e){
e.printStackTrace();
}
}
}

Use the remote methods


Steps in Setting up a RMI distributed
System
 Setting up a distributed system using RMI comprises the following st
eps:
 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 interface
 Create the publishing application (Publisher)
 Start the RMI Registry
 Run the publishing application
 Create the client program
 Test, debug, release
 You may need to repeat the testing and debugging a couple of times

You might also like