Download rmi-hqbriefing

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Java™ RMI Overview
Ann Wollrath
Senior Staff Engineer
Sun Microsystems, Inc.
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Overview
• Introduction
• Architecture
• Example
• Conclusion
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Introduction
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Usual RPC Assumptions
• Multiple machine instruction sets
• Multiple implementation languages
• Multiple object models
– or no object model at all
• Protocol agreement has to be up front
– perhaps at compile time
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Java Changes Those
Assumptions
• Java bytecodes give uniform platform
• Assume that Java is everywhere
• Environment safe to import code
• Language-centric approach
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Java RMI
• Enables method invocation between
objects in different Java VMs
• Uses pure Java interfaces
– no new interface definition language
• Allows passing Java objects
– preserves data encapsulation
– supports polymorphism
– dynamically loads classes
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Advantages of RMI
• Capitalizes on the Java object model
• Minimizes complexity
• Preserves safety of Java runtime
• Recognizes distribution differences
– partial failure
– latency
– no global knowledge
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Remote Objects are Java
Objects
• Remote objects extend Java object
model
– Remote interfaces to reference objects
– All serializable objects can be parameters and
return values
- Remote object references
- Java core class objects (e.g., Hashtable), AWT
Objects (e.g., Button), JavaBeans, user defined
objects
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Remote Objects are Java
Objects (cont’d)
– Stub objects have the same remote interfaces
as the remote object
- casting allowed via standard Java cast operators
- instanceof to check type of interface
– Exceptions to report communication failure
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
RMI Application Models
• Pure RMI solution
– Client/server
– Peer-to-peer
– Agents
• Three-tier
– RMI --> JDBC --> database
• Connect to legacy systems (via JNI)
– RMI --> Java wrapper --> existing system
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Distributed Object
Applications
• Typically, these applications need to:
– Locate remote objects
– Communicate with remote objects
– Load class bytecodes for objects that are
passed in remote method calls
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Locating Remote Objects
registry
RMI
RMI
client
server
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Remote Communication
registry
RMI
client
RMI
RMI
server
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Loading Classes
registry
RMI
client
RMI
RMI
server
URL protocol
URL protocol
URL protocol
web server
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
web server
Architecture
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Java RMI API
• java.rmi
– client API, naming, exceptions
• java.rmi.registry
– registry interface, factory, lookup
• java.rmi.server
– remote object implementation classes
• java.rmi.activation
– supports activatable remote objects
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Components of RMI System
• RMI runtime
–
–
–
–
remote references
protocol
transport
class loading and DGC
• rmiregistry: the remote object registry
• rmid: the RMI activation daemon
• rmic: the RMI stub compiler
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
RMI Architecture
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Method Invocation
caller’s VM
stub
remote object’s VM
remote object
reference
dispatcher
RMI runtime
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
RMI runtime
Parameter Passing
• RMI passes objects using Java Object
Serialization
– Read/write objects and graphs of objects
– Objects must agree to be serializable
– Preserves cycles (identity of objects within
graph)
– Customization on a per-class basis
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Class Loading
• RMI loads classes if not available
locally:
– Remote stub, parameter, and return value
classes
– Call stream is annotated with URL for class
location
– Loaded classes subject to installed security
manager
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Example
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Developing Applications
–
–
–
–
Define the interfaces to your remote objects
Implement the remote objects and clients
Run rmic on remote implementation classes
Make code network-accessible
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Compute Engine Service
• Remote object to compute arbitrary
tasks
– Client sends task to compute engine
– Compute engine runs task and returns result
– RMI loads task code dynamically
submit task
client
return result
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Compute
engine
Compute Remote Interface
• import java.rmi.*;
public interface Compute extends Remote {
Object executeTask(Task t)
throws RemoteException;
}
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Task Interface
• import java.io.Serializable;
public interface Task
extends Serializable
{
Object execute();
}
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Implementation Overview
• For a peer-to-peer remote object:
–
–
–
–
–
Extend UnicastRemoteObject class
Implement methods of remote interface
Create and install a security manager
Create remote object
Register remote object in RMI registry (or
other name facility)
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Compute Engine
Implementation
• import java.rmi.*;
import java.rmi.server.*;
public class ComputeEngine
extends UnicastRemoteObject
implements Compute
{
public ComputeEngine()
throws RemoteException
{
super();
}
public Object executeTask(Task t) {
return t.execute();
}
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
The main Method
• public static void main(String[] args) {
if (System.getSecurityManager() == null) {
System.setSecurityManager(
new RMISecurityManager());
}
try {
Compute engine = new ComputeEngine();
Naming.rebind(“Compute”, engine);
System.out.println(“Engine bound”);
} catch (Exception e) {
System.err.println(“exception: ” +
e.getMessage());
e.printStackTrace();
}
}
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Client Overview
• Create task to be executed
• Lookup the compute service by name
• Send task to compute service
• Print result
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Client’s main Method
• 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(“exception: ” +
e.getMessage());
e.printStackTrace();
}
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
p Task
• public class Pi implements Task {
private int digits;
public Pi(int digits) {
this.digits = digits;
}
public Object execute () {
return computePi(digits);
}
public static BigDecimal computePi(
int digits)
{
// exercise left to reader...
}
}
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Compute Engine: remote
communication
registry
Naming.lookup
ComputePi
Naming.rebind
comp.executeTask
server
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Compute Engine: class
loading
registry
Naming.lookup
ComputePi
Naming.rebind
comp.executeTask
server
ComputeEngine_Stub
Pi
web server
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
ComputeEngine_Stub
Compute
Task
web server
Conclusion
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
Future Directions
• Custom remote reference types
• Secure reference type
• Dynamically generated stub classes
• More performance improvements
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
More Information
• Java RMI home page:
– http://java.sun.com/products/jdk/rmi/
• Compute engine tutorial:
– http://java.sun.com/tutorial/rmi/
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.
January 26, 1999--Ann Wollrath Copyright 1999 Sun Microsystems, Inc., all rights reserved.