Remote Method Invocation
Download
Report
Transcript Remote Method Invocation
Remote Method Invocation
• RMI
• JAVA-TO-JAVA
• Object running on one JVM can invoke
methods on an object running on another
JVM
• RMI provides communication between
remote Java programs
Overview
• Sever Creates Remote Objects
• Server Makes References to Them
Accessible
• Server Waits on Remote Method Calls
• Client get References to Remote Objects
• Invokes Methods on Them
• Distributed Objects
Distributed Objects
• Locate Remote Objects
– Register
– Pass and Return References
• Communicate with Remote Objects
(Transparent to Programmer)
• Load Bytecode and transmit data over
network
Dynamic Code Loading
• Download code of an objects class if class
not defined in receiver’s virtual machine
• RMI passes objects by true type
• Change behavior of application dynamically
• Compute Example
Remote Interfaces, Objects, and
Methods
• Distrubed application made up of interfaces
and classes
• Some implementations reside on remote
machines
• Objects with methods on remote VM’s are
remote objects
• Object is remote by implementing remote
interface
Remote Interfaces
• Extends java.rmi.Remote
• Each remote method must declare
java.rmi.RemoteException
• Remote objects are passed by reference
between virtual machines, not a copy
Stubs
• Remote reference is a stub
• Local representative or proxy
• Caller invokes method on stub which
carries out method call on remote object
• Stub implements the same set of remote
interfaces as remote object
• Only those methods defined in remote
interface available to caller
Steps
• Design and Implement Components of
Distributed Application
• Compile Sources and Generate Stubs and
Skeletons
• Make Classes Network Accessible
• Start the Application
Details
• Object becomes remote by implementing a
remote interface
• java.rmi.Remote
• Methods java.rmi.RemoteException in
throws clause
• Stub is local proxy for remote object.
Generated by rmic compiler
• Skeleton interfaces object and object
manager on Server
Details, contd.
• Define Remote Interface: specifies remote
methods
• Implement Remote Objects: Implement the
remote interface
• Implement Clients
• Put classes in Web accessible directory:
remote interfaces, stubs, other download
classes
• Start registry, start application
Example
• Generic Compute Engine
• What it computes is not defined when
engine is written
• Take advantage of central powerful machine
• Dynamic
package compute;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Compute extends Remote {
Object executeTask(Task t) throws RemoteException;
}
package compute;
import java.io.Serializable;
public interface Task extends Serializable {
Object execute();
}
Interface Details
• Compute allows jobs to be submitted to the
engine
• Task defines how the compute engine
executes the given task. Passed by value
between VM’s
• Return value is an Object
package engine;
import java.rmi.*;
import java.rmi.server.*;
import compute.*;
public class ComputeEngine extends UnicastRemoteObject
implements Compute
{
public ComputeEngine() throws RemoteException {
super();
}
public Object executeTask(Task t) {
return t.execute();
}
public static void main(String[] args) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
String name = "//host/Compute";
try {
Compute engine = new ComputeEngine();
Naming.rebind(name, engine);
System.out.println("ComputeEngine bound");
} catch (Exception e) {
System.err.println("ComputeEngine exception: " + e.getMessage());
e.printStackTrace();
}
}
}
Implementing a Remote Interface
• Declare remote interfaces
• Define constructor for Remote object
• Provide implementations for remote
methods
• Create and install security manager
• create instance of remote object
• register remote object with registry
• UnicastRemoteObject: Point to point
communications with socket transport.
Constructor exports object for calls.
• RMISecurityManager as restrictive as
Applet
• Create instance and bind to registry.
Instance is of type Compute. Name is host
where registry is.
Register
• Front-end for binding (registering) and
looking up remote object is
java.rmi.Naming interface
• Remote VM’s can look up objects by name
in RMI registry.
• Name should inlcude name of host where
registry and remote object run, and a name
that identifies remote object in registry.
• Bind remote object to registry
• URL type format for host and name
• Default port is 1099
• //es229d01.ee.memphis.edu:1234/Compute
• RMI runtime substitutes reference to
stub for remote object reference. Remote
object implementations never leave JVM
where created.
•RMI keeps ComputeEngine running
Passing Objects in RMI
• Arguments and return values to methods
• Can be primitive, remote, or serializable
• Not Serializable: Does the information in
the object make sense only with a single
address space?
• Remote objects passed by reference (stub)
• Local objects passed by copy (value)
Stubs
• When client invokes remote method, it
actually calls a regular method on a local
JVM.
• This method is encapsulate in a stub class
(surrogate)
• Stub encodes parameters (marshalling)
• Sends information over network to server
Skeleton
• Makes sense out of information in packet
from stub (Unmarshals)
• Passes it to actual object on local JVM
• Packages response and sends it back to
client
• Stubs can be on local machine or loaded
across network similar to applet
Marshalled Parameters
and Return Values
Client
Method
Invocation
Stub
Skeleton
Server
Method
Invocation
Client
• Defines task to be done by
ComputeEngine
package client;
import java.rmi.*;
import java.math.*;
import compute.*;
public class ComputePi {
public static void main(String args[]) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(new RMISecurityManager());
}
try {
String name = "//" + args[0] + "/Compute";
Compute comp = (Compute) Naming.lookup(name);
Pi task = new Pi(Integer.parseInt(args[1]));
BigDecimal pi = (BigDecimal) (comp.executeTask(task));
System.out.println(pi);
} catch (Exception e) {
System.err.println("ComputePi exception: " + e.getMessage());
e.printStackTrace();
}
}
}
package client;
import compute.*;
import java.math.*;
public class Pi implements Task {
/**
* Construct a task to calculate pi to the specified
* precision.
*/
public Pi(int digits) {
this.digits = digits;
}
/**
* Calculate pi.
*/
public Object execute() {
return computePi(digits);
}
public static BigDecimal computePi(int digits) {
int scale = digits + 5;
BigDecimal arctan1_5 = arctan(5, scale);
BigDecimal arctan1_239 = arctan(239, scale);
BigDecimal pi = arctan1_5.multiply(FOUR).subtract(
arctan1_239).multiply(FOUR);
return pi.setScale(digits,
BigDecimal.ROUND_HALF_UP);
}
}
Running the Example
• Pi class downloaded to server at runtime
• ComputeEngine stub downloaded to client
at runtime
• Make jar file with compute package
(interfaces)
• Distribute to developers
• Put classes in network accessible directory
• RMI uses URL protocols to download code
• Classpath should include location of jar files
and source files
• Compile source (javac), generate stub (rmic)
• Details
• Running