RMI

Remote Method Invocation

14-Apr-12

“The network is the computer”*

Consider the following program organization:
method call SomeClass
computer 1

AnotherClass

returned object
computer 2

If the network is the computer, we ought to be able to put the two classes on different computers RMI is one technology that makes this possible
2

* For an opposing viewpoint, see http://www.bbspot.com/News/2001/04/network.html

RMI and other technologies

CORBA (Common Object Request Broker Architecture) has long been king

CORBA supports object transmission between virtually any languages Objects have to be described in IDL (Interface Definition Language), which looks a lot like C++ data definitions CORBA is complex and flaky

 

Microsoft supported CORBA, then COM, now .NET RMI is purely Java-specific
 

Java to Java communications only As a result, RMI is much simpler than CORBA
3

Overview
RMI

JDBC

CORBA

java.net

TCP/IP

Network OS

RMI Layers Java Virtual Machine Client Object Java Virtual Machine Remote Object Stub Skeleton Remote Reference Layer Remote Reference Layer Transport Layer TCP Transport Layer .

”  The “client object” has to find the object  Do this by looking it up in a registry  The client object then has to marshal the parameters (prepare them for transmission)   Java requires Serializable parameters The server object has to unmarshal its parameters.What is needed for RMI   Java makes RMI (Remote Method Invocation) fairly easy. do its computation. and marshal its response  The client object has to unmarshal the response 6  Much of this is done for you by special software . but there are some extra steps To send a message to a remote “server object.

“client” and “server” can easily trade roles (each can make requests of the other) The rmiregistry is a special server that looks up objects by name  Hopefully. the name is unique!  rmic is a special compiler for creating stub (client) and skeleton (server) classes 7 .Terminology      A remote object is an object on another computer The client object is the object making the request (sending a message to the other object) The server object is the object receiving the request As usual.

which is like a DNS service for objects  You also need TCP/IP active 8 .Processes  For RMI. rmiregistry. you need to be running three processes    The Client The Server The Object Registry.

RMI System Architecture Client Virtual Machine Client Server Virtual Machine Remote Object Stub Skeleton Server “Fred” Registry Virtual Machine .

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 “Fred” Registry Virtual Machine .

Client requests object from Registry Object 4.RMI Flow Client Virtual Machine Client Server Virtual Machine Remote 3. Registry returns remote reference (and stub gets created) Stub 3 4 Skeleton Server “Fred” Registry Virtual Machine .

RMI Flow Client Virtual Machine Client 5 6 7 Server Virtual Machine Remote Object Stub Skeleton Server 5. Stub talks to skeleton 7. Client invokes stub method 6. Skeleton invokes remote object “Fred” method Registry Virtual Machine .

the server must know both its interface (behavior) and its class (implementation) The interface must be available to both client and server The class should only be on the server 13  In short.    In order to use a remote object.Interfaces   Interfaces define behavior Classes define implementation Therefore.   . but does not need to know its implementation (class) In order to provide an object. the client must know its behavior (interface).

Classes  A Remote class is one whose instances can be accessed remotely   On the computer where it is defined. instances of this class can be accessed just like any other object On other computers. the remote object can be accessed via object handles  A Serializable class is one whose instances can be marshaled (turned into a linear sequence of bits)  Serializable objects can be transmitted from one computer to another  It probably isn’t a good idea for an object to be both remote and serializable 14 .

Conditions for serializability  If an object is to be serialized:     The class must be declared as public The class must implement Serializable The class must have a no-argument constructor All fields of the class must be serializable: either primitive types or serializable objects 15 .

Remote Every method in the interface must declare that it throws java.Remote interfaces and class  A Remote class has two parts:  The interface (used by both client and server):    Must be public Must extend the interface java.server.rmi.UnicastRemoteObject May have locally accessible methods that are not in its Remote interface 16  The class itself (used only by the server):    .rmi.rmi.RemoteException (other exceptions may also be thrown) Must implement a Remote interface Should extend java.

Serializable  A Remote object lives on another computer (such as the Server)    You can send messages to a Remote object and get responses back from the object All you need to know about the Remote object is its interface Remote objects don’t pose much of a security issue  You can transmit a copy of a Serializable object between computers    The receiving object needs to know how the object is implemented.Remote vs. it needs the class as well as the interface There is a way to transmit the class definition Accepting classes does pose a security issue 17 .

  Most discussions of RMI assume you should do this on both the client and the server  Unless your server also acts as a client.setSecurityManager(new RMISecurityManager()).Security  It isn’t safe for the client to use somebody else’s code on some random server  Your client program should use a more conservative security manager than the default System. it isn’t really necessary on the server 18 .

rebind(url.   Every remotely available method must throw a RemoteException (because connections can fail) Every remotely available method should be synchronized 19 .   The server class needs to register its server object:  The default port is 1099  Naming. object).The server class  The class that defines the server object should extend UnicastRemoteObject    This makes a connection with exactly one other computer If you must extend some other class. you can use exportObject() instead Sun does not provide a MulticastRemoteObject class String url = "rmi://" + host + ":" + port + "/" + objectName.

Implementing RMI  Implement RMI server      Create Remote Interface Create a class that implements the remote interface Create stub and skeleton classes Copy Remote interface and stub to client Create and register remote objcet  Implement the RMI client  Call the remote interface by using the ‘lookup()’ method of the Naming class. 20 .

I Client Virtual Machine Client Server Virtual Machine Remote Object 1 Stub Skeleton Server 1.RMI flow . Server Creates Remote object. 2. Server Registers Remote object with RMI registry Registry Virtual Machine Remote Object Registered 2 21 .

Registry returns remote reference through interface 22 .II Client Virtual Machine Client Server Virtual Machine Remote Object Stub 3 4 Registry Virtual Machine Remote Object Registered Skeleton Server 3.RMI flow . Client requests object from registry 4.

RMI flow . Skeleton invokes remote object method. 23 .III Client Virtual Machine Client 5 Server Virtual Machine 7 Remote Object 6 Skeleton Server Stub Registry Virtual Machine Remote Object Registered 5. Client invokes stub method 6. Stub talks to skeleton 7.

rmi. } 24 .*.Hello world server: interface  import java. public interface HelloInterface extends Remote { public String say() throws RemoteException.

// Strings are serializable public Hello (String msg) throws RemoteException { message = msg.server.*. } public String say() throws RemoteException { return message.*. import java.Hello world server: class  import java.rmi. public class Hello extends UnicastRemoteObject implements HelloInterface { private String message. } 25 } .rmi.

System.println("Hello Server is ready.out. } catch (Exception e) { System. } } } 26 .out.Registering the hello world server  class HelloServer { public static void main (String[] argv) { try { Naming.println("Hello Server failed: " + e). new Hello("Hello. world!")).").rebind("rmi://localhost/HelloServer".

lookup(name).out.say()). } } } 27 . try { hello = (HelloInterface)Naming.out. System. } catch (Exception e) { System. String name = "rmi://localhost/HelloServer".println(hello.The hello world client program  class HelloClient { public static void main (String[] args) { HelloInterface hello.println("HelloClient exception: " + e).

it should be compiled with rmic:  rmic Hello   This will generate files Hello_Stub.1 but is no longer necessary 28 .rmic   The class that implements the remote object should be compiled as usual Then.class These classes do the actual communication   The “Stub” class must be copied to the client area The “Skel” was needed in SDK 1.class and Hello_Skel.

Run the server program: • java HelloServer 3.Trying RMI  In three different terminal windows: 1. Run the registry program: • start rmiregistry 2. Run the client program: • java HelloClient  If all goes well. you should get the “Hello. World!” message 29 .

4. 2.Summary 1. with the registry server The client looks up the object in the registry server The request actually goes to the Stub class The Stub classes on client and server talk to each other The client’s Stub class returns the result 30 3. Start the registry server. . with a name. 2. rmiregistry Start the object server 1. Start the client 1. The object server registers an object. 3. The client makes a request 1.

*. defines the remote interface that is provided by the server. double d2) throws RemoteException. } 31 .java. import java.A Simple Client/Server Application Using RMI  Step One: Enter and Compile the Source Code  AddServerIntf.rmi. public interface AddServerIntf extends Remote { double add(double d1.

import java. double d2) throws RemoteException { return d1 + d2.server.*. public class AddServerImpl extends UnicastRemoteObject implements AddServerIntf { public AddServerImpl() throws RemoteException { } public double add(double d1.rmi.*.import java.rmi. } } 32 .

out.rmi. } } } 33 .println("Exception: " + e).import java.rebind("AddServer". public class AddServer { public static void main(String args[]) { try { AddServerImpl addServerImpl = new AddServerImpl(). } catch(Exception e) { System.*.*. Naming.net. addServerImpl). import java.

println("The sum is: " + addServerIntf. double d2 = Double. double d1 = Double.println("The first number is: " + args[1]).doubleValue().import java. System.out.println("The second number is: " + args[2]). public class AddClient { public static void main(String args[]) { try { String addServerURL = "rmi://" + args[0] + "/AddServer".out.add(d1.println("Exception: " + e). } catch(Exception e) { System. AddServerIntf addServerIntf = (AddServerIntf)Naming.out. }}} 34 .doubleValue(). System.valueOf(args[1]).lookup(addServerURL).*.valueOf(args[2]). d2)). System.out.rmi.

It works with the other parts of the 1. and invoke the appropriate code on the server. perform deserialization. rmic AddServerImpl 35 . A skeleton is a Java object that resides on the server machine.Step Two: Generate Stubs and Skeletons   a stub is a Java object that resides on the client machine.1 RMI system to receive requests. Its function is to present the same interfaces as the remote server.

and AddServerIntf.class.class.class.class. and AddServer. AddServerImpl_Stub. AddServerImpl_Skel.class.Step Three: Install Files on the Client and Server Machines   Copy AddClient. AddServerImpl_Stub.class to a directory on the server machine.class.class to a directory on the client machine Copy AddServerIntf. AddServerImpl. 36 .

you should see that a new window has been created. 37 .Step Four: Start the RMI Registry on the Server Machine start rmiregistry  When this command returns.

Step Five: Start the Server java AddServer  Recall that the AddServer code instantiates AddServerImpl and registers that object with the name “AddServer”. 38 .

0.Step Six: Start the Client  The AddClient software requires three arguments: the name or IP address of the server machine and the two numbers that are to be summed together. java AddClient 127.1 8 9  Output: The first number is: 8 The second number is: 9 The sum is: 17.0.0 39 .

The End 40 .

489..4.

071.04$07.07870.07 3004 04 47/  $8902 4:9 57393 04$07.0/ 0  < < <  ..059430  $8902 4:9 57393 04$07. ./   < .9.

89.4/2.2072.%00447/.9...78  0439071.004 $9733.2 ..8804039 5:-.3 $973(.0395747.

.

4.489..

9...  < . .0 .20  $8902 4:9 57393 04 8.23 44:5 3.07 97 04 0439071.059430  $8902 4:9 57393 040390.04$07.05943 0  < < <  .

.039..422:3.3/ 04*$0 .4250/.4250/972.8808/490.943 %0 $9:.70.. %03 984:/-0.984:/-0 ..9010804*$9:- .8:8:..088..8300/0/3$ -:9834430730.882:89-0.925020398907024904-0.889.. 72.7  .450/ 9490.. %0 $0 .72.9:. %0.88 .88 %080..04 %80307.

0  .3/48  #:390708975747..4080 4:84:/0990 04 47/  2088.2 W 89....%73# 39700/110703990723.075747.04$07.2 W .0395747.797270897  #:390807.07  #:390.04039 1.2 W .

.3/807.4907 %0.939070897807.039.34-0.4089490$9:-..7   $9.799070897807.3.07 %0706:089.077089078.079.07 %0.706:089    .88 %0$9:-.9807..9 9.880843..039 8$9:-..07 7270897 $9.08.79904-0.$:22.07  %04-0.039   %0.20 990 70897807.039448:5904-0.9:.940.7990.0392.9807.88709:73890708:9   $9.

$250039.

05943 <  ..0//$07.07391 .3/425090$4:7. 72  5:-.073910903/8#02490 /4:-0..09.0755.943&83# $905 303907.// /4:-0/ /4:-0/ 9748#02490.$07.07 25479. /013089070249039071.39071...98 574.../0/-90807..04/0 //$07.

89#02490 -0.05943 < 5:-..07391 5:-.9 25020398//$07.// /4:-0/ /4:-0/ 9748 #02490.05943 709:73/ / < <  .. 72  25479.. 72 807..25479...07250903/8&3./4:-0.88//$07.07  5:-..0725 9748#02490.//$07.

25479. 309  25479...//$07.3 $973..4/2..9.0725  < ...9.//$07. .88//$07.07 5:-..0725 .07 .072530//$07..23 70-3/ //$07.05943 0  < < <   .78(  97 //$07.0725.89. 72  5:-.059430  $8902 4:9 57393 .

72  5:-.78(  97 $973.4/2.//$07.25479..3 $973.07&#72....9..89.88//039 5:-.

.

 .78 (  .

//$07.07391 .:0 1 .//$07.//$07.9.//$07.78( /4:-0'.07 //$07.78(  /4:-0/4:-0 .23 44:5 .07391 //$07.78(  /4:-0/4:-0 ..05943 0  <<<    .07391 . .:0 1 .059430  $8902 4:9 57393 .:0 $8902 4:9 57393 %080..:0 $8902 4:9 57393 %08:28  .07&#  $8902 4:9 57393 %017893:2-078  .78( /4:-0'..43/3:2-078  .// / /  < .07391.

0.07 2...890 702490807.7984190 # 889029470.89:-8.9708/084390807.9708/084390..07 8009438.4090.$905%40307..//$07.9438945708039908.30 947899049075.30  981:3...90.0706:0898 5071472/0807.0725  .90$9:-8.4-0.99.943 .3/ 3.99..557457...3/$009438 .4-0..4/04390807.08.07 72.2039071.0392.

...9474390..3///$07.$905%700389.072.9474390807.88 //$07.88  .308 45//039 .0725*$9:- .07391 .88 .88  //$07.8894..88 //$07.8894./70...084390039..039 2..07 .07391 .30 45//$07..3/$07.3///$07.0725*$0 .0725*$9:- . /70.07 .0725 ..88  //$07.30  .

422.7990##08974390$07.3/709:738 4:84:/8009.90/  .30 3/4.9.70.$9054:7$9..30 89.8-003.797270897 0398.07 .

07 #0.07  .3/70890789..4/0389.0$9.99903..94-0.7990$07.//$07.39.908 //$07.20 //$07.9.07 .990//$07.$905.07.0725..

7990039 %0//0398419.2047!.7:2039890 3.7094-08:220/940907 .70706:7089700..//039  :95:9 %017893:2-078 %080.072.$905$$9.43/3:2-078 %08:28  .9.3/9094 3:2-0789...30.//70884190807.

%03/  .

Sign up to vote on this title
UsefulNot useful