Download Lecture 12

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

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

Document related concepts
no text concepts found
Remote Method Invocation
Fu-Chiung Cheng
Associate Professor
Computer Science & Engineering
Tatung University
• RMI Concept from Java Network
• RMI (Sun)
• Examples:
– JBuilder Simple RMI
– RMI Calculator
RMI Concepts
• Two fundamental network applications
–File and data transfer (FTP, SMTP (email),
HTTP, …)
–Allowing one host to run programs on
another host (telnet, rlogin Remote procedure
call (RPC), CGI..
• Remote Method Invocation (RMI) allows
Java programs call other Java programs
on the other machines.
• RMI == > distributed/parallel computing
Remote Method Invocation
• The remote method invocation API let Java
objects on different hosts communicate with
each other
• A remote lives on a server
• Each remote object implements a remote
interface that specifies which of its methods
can be invoked by clients.
• Clients invoke the methods of the remote
object almost exactly as they invoke local
Remote Method Invocation
• Example:
– An object running on a local client can pass
a database query as a String argument to a
method in a database object running on a
remote server to ask it to sum up a series of
– The server can return the result to the client
as a double.
– This is more efficient than download all the
records and summing them up locally.
Remote Method Invocation
• From the programmer’s perspective, remote
objects and methods work just like the local
objects and methods
• All the implementation details are hidden.
• You just
– import one package,
– look up the remote object in a registry and
– make sure that you catch the RemoteException
when you call the object’s methods
Remote Method Invocation
• A remote object is an object whose methods
may be invoked from a different Java virtual
• Each remote object implements one or more
remote interfaces that declare which methods
of the remote object can be invoked by the
foreign system.
• RMI is the facility by which a Java program
running on one machine can invoke a method
in an object on a complete different machine.
• The prospect of remote hosts invoking
methods in the local host’s object raises many
security issues
• The activities that a remote object can
perform are limited in much the same way an
applet’s activity is limited
• A Security Manager object checks all
operations to make sure they’re allowed
• Custom security managers can be defined for
specific applications
• Public key authentication can be used to
verify a user’s identity and allow different
users different levels of access to a
remote object.
• For example:
– General public may be allowed to query a
database but not update it.
– Users from inside a company might be
allowed to both query and update the
Object Serialization
• When a object is passed to or returned from a
Java method, what is really transferred is a
reference to the object
• Reference or object:
– A special remote reference to the object is passed
– A copy of the object can be passed
• Reference: the local machine passes a remote
object to the remote machine
• Copied object: the local machine passes its
own object (copy the object and send the
copied object.)
Object Serialization
• To copy an object, you need a way to
convert the object into a stream of bytes.
– May not be easy: Objects may include other
• Object Serialization is a scheme by which
objects can be converted into a byte stream
and then passed around to other machines,
which rebuild the original objects from to bytes
• These bytes can also be written to disk and
read back from disk at a later time.
Object Serialization
• For security reasons, Java places some
limitations on which objects can be serialized
– All Java primitive types can be serialized
– Object Java objects that implement interface
• Serializable objects:
– String and Component
– Vector
– Integer, Float (inherits from Number which is
– Exceptions
– Most AWT and Swing
• The fundamental difference between remote
objects and local objects is that remote objects
resides in different VM
• Method call: pass references (local machine is
• Pass arguments (RMI way)
– Primitive types are passed by values
– Reference to remote objects are passed as a remote
– Local objects are passed by values (object
• RMI Layer model (see Fig 18.1 on page 599)
• RMI Layer model (see Fig 18.1 on page 599)
– The client appears to talk directly to the server
– In reality, the client program talks only to the a stub.
– The stub passes that conversation along to the
remote reference layer
– The remote reference layer talks to the transport layer
– The transport layer on the client passes the data
across the Internet to the transport layer on the server
– The transport layer on the server then communicates
with the server’s remote reference layer
– The remote reference layer talks to the skeleton
– The skeleton communicates with the server.
• RMI applications are often comprised of two
separate programs: a server and a client.
• RMI provides the mechanism by which the
server and the client communicate and pass
information back and forth.
• Such an application is sometimes referred to
as a distributed object application.
Java RMI Architecture
• The RMI architecture is based on one
important principle: the definition of
behavior and the implementation of
that behavior are separate concepts.
• RMI allows the code that defines the
behavior and the code that implements
the behavior to remain separate and to
run on separate JVMs.
Java RMI Architecture
• Specifically, in RMI, the definition of a
remote service is coded using a Java
• The implementation of the remote
service is coded in a class.
• Therefore, the key to understanding
RMI is to remember that interfaces
define behavior and classes define
Java RMI Architecture
Client Program
Server Program
Java RMI Architecture
1. bind/rebind
2. lookup
Server Stub class rmiregistry Stub class
3. Remote Procedure Call
Naming (java.rmi.Naming)
• static void bind(String name, Remote obj)
– Binds the specified name to a remote
• static String[] list(String name)
– Returns an array of the names bound in
the registry.
• static Remote lookup(String name)
– Returns a reference, a stub, for the remote
object associated with the specified name.
Naming (java.rmi.Naming)
• static void rebind(String name, Remote obj)
– Rebinds the specified name to a new
remote object.
• static void unbind(String name)
– Destroys the binding for the specified
name that is associated with a remote
Creating RMI (Distributed)
1. Design and implement the components of
your distributed application.
2. Compile sources and generate stubs.
3. Make classes network accessible.
4. Start the application.
Creating RMI (Distributed)
Design and implement the components of your
distributed application.
– Job: decide on your application architecture and
determine local objects and remote objects.
– Defining the remote interfaces:
• A remote interface specifies the methods that
can be invoked remotely by a client.
– Implementing the remote objects:
• Remote objects must implement one or more
remote interfaces.
Creating RMI (Distributed)
1. Design and implement the components of
your distributed application.
Implementing the clients:
• Clients that use remote objects can be
implemented at any time after the remote
interfaces are defined.
• Clients can be invoked after the remote
objects have been deployed.
Creating RMI (Distributed)
2. Compile sources and generate stubs.
use the javac to compile the source files, which
contain the implementation of the remote
interfaces and implementations, the server
classes, and the client classes.
use the rmic to create stubs for the remote
RMI uses a remote object's stub class as a
proxy in clients so that clients can communicate
with a particular remote object.
Creating RMI (Distributed)
3. Make classes network accessible.
– Make the class files associated with the
remote interfaces, stubs, and other
classes that need to be downloaded to
clients accessible via a Web server.
Creating RMI (Distributed)
4. Start the application.
– Starting the application includes running
the RMI remote object registry, the server,
and the client
– 1. Start rmiregistry
– 2. Start server (bind services)
– 3. Start client (use services)
RMI componets
• A working RMI system is composed of
several parts:
Interface definitions for the remote services
Implementations of the remote services
Stub and Skeleton files
A server to host the remote services
An RMI Naming service that allows clients to
find the remote services
– A class file provider (an HTTP or FTP server)
– A client program that needs the remote services
RMI Example
RMI Calculator client/server application
– Interface (define remote services):
– Implementation of remote services:
– Stub and Skeleton:
– Server program:
– Client program:
RMI Calculator: Interface
The Calculator interface defines all of the
remote features offered by the service:
public interface Calculator extends java.rmi.Remote {
public long add(long a, long b) throws RemoteException;
public long sub(long a, long b) throws RemoteException;
public long mul(long a, long b) throws RemoteException;
public long div(long a, long b) throws RemoteException;
RMI Calculator:
public class CalculatorImpl extends UnicastRemoteObject
implements Calculator {
public CalculatorImpl() throws java.rmi.RemoteException
{ super(); }
public long add(long a, long b) throws RemoteException
{ return a + b; }
public long sub(long a, long b) throws RemoteException
{ return a - b; }
public long mul(long a, long b) throws RemoteException
{ return a * b; }
public long div(long a, long b) throws RemoteException
{ return a / b; }
RMI Calculator:
The implementation class uses
UnicastRemoteObject to link into the RMI system.
This is not a requirement. A class that does not
extend UnicastRemoteObject may use its
exportObject() method to be linked into RMI.
When a class extends UnicastRemoteObject, it
must provide a constructor that declares that it
may throw a RemoteException object.
When this constructor calls super(), it activates
code in UnicastRemoteObject that performs the
RMI linking and remote object initialization.
RMI Calculator:
Stub and Skeleton
use the RMI compiler, rmic, to generate the
stub and skeleton files.
The compiler runs on the remote service
implementation class file.
>rmic CalculatorImpl
After you run rmic you should find the file
– Calculator_Stub.class (both 1.1 and 1.2)
– Calculator_Skel.class (only 1.1).
Command rmic
Usage: rmic <options> <class names>
<options> includes:
-keep Do not delete intermediate generated source files
-keepgenerated (same as "-keep")
-g Generate debugging info
-depend Recompile out-of-date files recursively
-nowarn Generate no warnings
-verbose Output messages about what the compiler is doing
-classpath <path> Specify where to find input source and
class files
-d <directory> Specify where to place generated class files
-J<runtime flag> Pass argument to the java interpreter
Command rmic
Usage: rmic <options> <class names>
<options> includes:
The Java 2 platform version of rmic add three new options:
-v1.1 Create stubs/skeletons for JDK 1.1 stub protocol
-vcompat (default) Create stubs/skeletons compatible with
both JDK 1.1 and Java 2 stub protocol versions
-v1.2 Create stubs for Java 2 stub protocol version only
RMI Calculator:
Server program
Remote RMI services must be hosted in a
server process.
The class CalculatorServer is a very simple
server that provides the bare essentials for
RMI Calculator:
Server program
import java.rmi.Naming;
public class CalculatorServer {
public CalculatorServer() {
try {
Calculator c = new CalculatorImpl();
Naming.rebind(" rmi://localhost:1099/CalculatorService", c);
} catch (Exception e) { System.out.println("Trouble: " + e); }
public static void main(String args[]) {
new CalculatorServer();
RMI Calculator:
Client program
public class CalculatorClient {
public static void main(String[] args) {
try {
Calculator c = (Calculator)
Naming.lookup( "rmi://remotehost/CalculatorService");
System.out.println( c.sub(4, 3) );
System.out.println( c.add(4, 5) );
System.out.println( c.mul(3, 6) );
System.out.println( c.div(9, 3) ); }
catch (MalformedURLException murle) {
Running RMI Applications
1. Start with the Registry.
– $rmiregistry & (unix) or >start rmiregistry
2. Start the server hosting the CalculatorService
– >java CalculatorServer
3. Start the client program.
– >java CalculatorClient