Chapter 5 Remote Method Invocation (RMI)
Chapter 5 Remote Method Invocation (RMI)
1
Remote Method Invocation (RMI)
• Covered in Java programming
• Distributed objects in Java
2
Overview of RMI
• RPC
4
RMI Architecture
5
RMI Architecture
• The stub and skeleton layer in RMI
– Stub
• A placeholder object which offers the same interface as the
server 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.
6
RMI Architecture …
• Skeleton
• The skeleton takes the calls of the stub
• It processes them
• It forwards the call to the server object
• It waits for the result
• It sends the result back to the stub
– 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.
• NB: In the Java 2 SDK, an stub protocol was introduced
that eliminates the need for skeletons.
7
RMI Architecture
• The reference layer in RMI
– It finds the respective communication partners
– It includes the name service, the registry
• The transport layer in RMI
– It manages connections
– It handles communication
– It must not be confused with the network transport
layer (e.g. TCP/IP)
8
RMI Packages and Classes
• Package java.rmi
– Provides the RMI package
• Interface java.rmi.Remote
– Identifies interfaces whose methods may be invoked from a non-
local virtual machine
– Any object that is a remote object must directly or indirectly
implement this interface
– Only those methods specified in a "remote interface“ (an interface
that extends java.rmi.Remote) are available remotely
• Class java.rmi.RemoteException
– Communication-related exceptions that may occur during the
execution of a remote method call
– Each method of a remote interface (an interface that extends
java.rmi.Remote) must list RemoteException in its throws clause 9
RMI Packages and Classes
• Package java.rmi.server
– Provides classes and interfaces for supporting the server side of
RMI
• Class java.rmi.server.RemoteObject
– Distributed objects do not inherit directly from Object
– They inherit from RemoteObject
– RemoteObject implements the Object behavior for
remote objects (e.g. methods hashCode, equals, and
toString are reimplemented)
• Class java.rmi.server.UnicastRemoteObject
– Provides support for point-to-point active object references
(invocations, parameters, and results)
– Uses TCP streams
10
Steps to Create RMI Applications
The following is set of six steps given to write the
RMI program
1. Create the remote interface
2. Provide the implementation of the remote interface
3. Compile the implementation class and create the
stub and skeleton objects using the rmic tool
4. Start the registry service by rmiregistry tool
5. Create and start the remote application
6. Create and start the client application
11
General RMI Architecture
Remote Machine
bind
RMI Server
Registry
skeleton
stub
RMI Client
Local Machine
12
The Naming class provides methods to get and store the remote
object. The Naming class provides 5 methods.
public static java.rmi.Remote lookup(java.lang.String) throws It returns the reference
java.rmi.NotBoundException, java.net.MalformedURLException, of the remote object.
java.rmi.RemoteException;
17
Steps …
• Implement the server program – Create and install a Security
Manager
– The first task of the server program is to create and install a security
manager
– This protects access to system resources from untrusted downloaded
code running within the Java virtual machine
– A security manager determines whether downloaded code has access
to the local file system or can perform any other privileged
operations
– If an RMI program does not install a security manager, RMI will not
download classes (other than from the local class path) for objects
received as arguments or return values of remote method invocations
– This restriction ensures that the operations performed by downloaded
code are subject to a security policy
18
Steps …
• Implement the client program:
– Create and install a security manager
– Get a remote object reference
– Perform remote method invocations on the remote
object
• Compile source files
– The remote interface
– The remote object implementation class
– The server program class
– The client program class
19
Steps …
• Run server-side and client-side programs:
• Server-side
– Start RMI registry
– Start the server
• Client-side
– Start the client
20
Example
• Step 1:
– Define the interface
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Hello extends Remote
{
String sayHello() throws
RemoteException;
}
21
Step 2:
– Develop the remote object by implementing the remote
interface
– The server is a simple unicast remote server.
– Create server by extending
java.rmi.server.UnicastRemoteObject.
– The server uses the RMISecurityManager to protect its
resources while engaging in remote communication
22
public class Server extends UnicastRemoteObject
implements Hello {
23
• Should give implementation for the interface
method
24
Combining all together …….
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.*;
import java.security.*;
public class Server extends UnicastRemoteObject implements
Hello {
public Server() throws RemoteException {}
public String sayHello() {
return "Hello, world!";
}
25
public static void main(String args[]) {
try {
Server obj = new Server();
//System.setSecurityManager(new SecurityManager());