RMI
RMI
RMI
Introduction
Java
Networking Distributed
Computing
Java Remote Method Invocation (RMI) Is an Inter-process Protocol for Java, Allowing Java Objects Living in Different Java Virtual Machines to Invoke Transparently Each Other's Methods. Since These Virtual Machines Can Be Running on Different Computers Anywhere on the Network, RMI Enables Objectoriented Distributed Computing. Java RMI Provides the Java Programmers With an Efficient, Transparent Communication Mechanism That Frees Them of All the Application-level Protocols Necessary to Encode and Decode Messages for Data Exchange.
RMI defined
What Is RMI?
Access
to Remote Objects Java-to-Java only Client-Server Protocol High-level API Transparent Lightweight
Examples of Use
Database
Related Technologies
RPC
CORBA
DCOM
LDAP
Middleware Layers
Applications RPCs and RMIs, e.g., CORBA Request reply protocol External data representation Operating System Middleware layers= Provide support to the application
Client Object
TCP
Remote Object
RMI Layers
Java Virtual Machine Client Object Java Virtual Machine Remote Object
Stub
Skeleton
Transport Layer
TCP
Transport Layer
Remote Objects
Remote
Live
Objects
Registries
Name
and look up remote objects Servers can register their objects Clients can find server objects and obtain a remote reference A registry is a running process on a host machine
The Server Object Makes Methods Available for Remote Invocation by Binding It to a Name in the RMI Registry.
The Client Object, Can Thus Check for the Availability of a Certain Server Object by Looking up Its Name in the Registry.
The RMI Registry Thus Acts As a Central Management Point for Java-RMI. The RMI Registry Is Thus a Simple Name Repository. It Does Not Address the Problem of Actually Invoking Remote Methods.
References
Interfaces
Declare
exposed methods Implemented on client Like a proxy for the remote object
Proxy
Is responsible of making RMI transparent to clients by behaving like a local object to the invoker. The proxy implements (Java term, not literally) the methods in the interface of the remote object that it represents. But, Instead of executing an invocation, the proxy forwards it to a remote object. Each method of the proxy marshals the following into a request message: (i) a reference to the target object, (ii) its own method id and (iii) the argument values. Request message is sent to the target, then proxy awaits the reply message, un-marshals it and returns the results to the invoker.
implements
implements
Client
Stub
Skeleton
out which remote object is being referenced Could span multiple virtual machines Communicates via TCP/IP
Transport Layer
Deals
with communications Connection management Dispatching messages between stub and skeleton Distributed Garbage Collection Sits on top of java.net
RMI Architecture
RMI Architecture
The first layer is the Stub/Skeleton Layer. This layer is responsible for managing the remote object interface between the client and server. The second layer is the Remote Reference Layer (RRL). This layer is responsible for managing the "liveliness" of the remote objects. It also manages the communication between the client/server and virtual machines, (e.g., threading, garbage collection, etc.) for remote objects. The third layer is the Transport Layer. This is the actual network/communication layer that is used to send the information between the client and server over the wire. It is currently TCP/IP based.
uses a network-based registry to keep track of the distributed objects. The server object makes a method available for remote invocation by binding it to a name in the registry. The client object, in turn, can check for availability of an object by looking up its name in the registry . The registry acts as a limited central management point for RMI . The registry is simply a name repository . It does not address the problem of actually invoking the remote method.
two objects may physically reside on different machines. A mechanism is needed to transmit the client's request to invoke a method on the server object to the server object and provide a response. RMI uses an approach similar to RPC in this regard. The code for the server object must be processed by an RMI compiler called rmic, which is part of the JDK.
rmic compiler generates two files: a stub and a skeleton. The stub resides on the client machine and the skeleton resides on the server machine. The stub and skeleton are comprised of Java code that provides the necessary link between the two objects.
a client invokes a server method, the JVM looks at the stub to do type checking (since the class defined within the stub is an image of the server class). The request is then routed to the skeleton on the server, which in turn calls the appropriate method on the server object. In other words, the stub acts as a proxy to the skeleton and the skeleton is a proxy to the actual remote method.
Stub
Skeleton Server
RMI Flow
1. Server Creates Remote Object Client Virtual Machine 2. Server Registers Remote Object Client Server Virtual Machine Remote Object
1
Stub
Skeleton Server
2
RMI Flow
Client Virtual Machine Client Server Virtual Machine Remote 3. Client requests object from Registry Object 4. Registry returns remote reference (and stub gets created) Stub
3 4
Skeleton Server
RMI Flow
Client Virtual Machine Client
5 6 7
Stub
Skeleton Server
5. Client invokes stub method 6. Stub talks to skeleton 7. Skeleton invokes remote object Fred method Registry Virtual Machine
Define an interface that declares the methods that will be available remotely. The server program must include a class that implements this interface. The server program must create a remote object and register it with the naming service.
The client program creates a remote object by asking the naming service for an object reference.
Steps
Create
source file and compile it Generate Stubs and Skeletons Install Files on the Client and Server Machines Start the RMI Registry on the Server Machine Start the Server Start the Client
a Remote Interface
java.rmi.Remote
extends
java.rmi.RemoteObject or java.rmi.UnicastRemoteObject
interface You must make references, arrays, etc. out of the interface type, not the implementation type You cant cast the remote reference to a normal reference So name your Remote Objects with Impl (so you dont get confused)
Parameter Passing
Primitive
passed
types
by value
Remote
passed
objects
by reference
Non-remote
passed
objects
a new instance of the remote object Registers it in the registry with a unique name Use Naming.bind or Naming.rebind method Bind throws AlreadBoundException if previous binding is there.
Server
import java.net.*; import java.rmi.*; public class AddServer { public static void main(String args[]) { try { AddServerImpl addServerImpl = new AddServerImpl(); Naming.rebind("AddServer", addServerImpl); } catch(Exception e) { System.out.println("Exception: " + e); } } }
a Security Manager
Find
a registry
java.rmi.Naming
use
Lookup
the name, returns a reference Cast the reference to the appropriate Remote Interface
RMI URLs
rmi://host[:port]/name
default
port is 1099 Specifies hostname of registry can also use relative URLs
name
Compiling classes
Compile
javac
Compile
javac
AddServerImpl.java (remote class) AddServerImpl.class (classfile)
rmic
Step 3
Make
Server folder
Copy
Make
Client folder
Copy
Registry CLASSPATH
Registry
file(s) You must set the CLASSPATH to include the directory containing the stub file Or, your server needs to specify the java.rmi.server.codebase System property (more later)
the registry
running
process Before running registry make sure the classpath is set properly
Unix:
rmiregistry &
Windows:
start rmiregistry For our example C:\server>set classpath=c:\server;c:\client C:\server>start rmiregistry C:\server>java AddServer
Start client
Java AddClient localhost 4 5
Output
C:\client>java -classpath . AddClient 127.0.0.1 5 7 The first number is: 5 The second number is: 7 The sum is: 12.0
applet is run on client side and it gets the message from the server
Interface class
import java.rmi.*; public interface Hello extends Remote { String sayHello() throws java.rmi.RemoteException; }
Server class
import java.rmi.server.*; import java.rmi.*; public class HelloImpl extends UnicastRemoteObject implements Hello { private String name; public HelloImpl(String s) throws RemoteException { super(); name = s; } public String sayHello() throws RemoteException { return "Hello World!"; }
Applet class
import java.awt.*; import java.rmi.*; public class HelloApplet extends java.applet.Applet { String message = ""; public void init() { try { Hello obj = (Hello)Naming.lookup("//" + getCodeBase().getHost() + "/HelloServer"); message = obj.sayHello(); } catch (Exception e) { System.out.println("HelloApplet exception: " + e.getMessage()); e.printStackTrace(); } } public void paint(Graphics g) { g.drawString(message, 25, 50); } }
Output
RMI Security
Server
is untrusted Stubs could be changed rmic is OK, but someone could customcode an evil stub: its just a .class file
RMISecurityManager
disables
all functions except class definition and access A downloaded class is allowed to make a connection if the connection was initiated via the RMI transport.
None
Stub
Limitations of RMI
Java-only
but
Uses
TCP, not UDP At least two sockets per connection Untested for huge loads
RMI Vs RPC
Because
RPC is designed to be functional between all combinations of application language it requires that a call's data representation be converted from its local language into a common language in order for it to be transmitted across the network. RMI is designed to operate only within the java environment thus no changes to the call's data representation are required.
RMI Vs RPC
RMI
allows any Java object type to be used for communication-even if the client or server has never encountered it before. RPC require that only simple data types or defined structures be passed to and from the methods for remote execution. RMI also both client and server to dynamically load new object types as required RMI Language dependent RPC language neutral
Summary
RMI
is a very clean API Easy way to write distributed programs Wire protocol may need improvement for large-scale problems