Remote Method Invocation CS 587 x Lecture Department

  • Slides: 25
Download presentation
Remote Method Invocation CS 587 x Lecture Department of Computer Science Iowa State University

Remote Method Invocation CS 587 x Lecture Department of Computer Science Iowa State University

Introduction of RMI What is Remote Method Invocation? Bulb is a remote object Bulb

Introduction of RMI What is Remote Method Invocation? Bulb is a remote object Bulb Server Bulb. on() client Bulb. off() public class Light. Bulb { private boolean light. On; public void on() { light. On = true; } public void off() { light. On = false; } client public boolean is. On() { return light. On; } }

Introduction of RMI What is Remote Method Invocation? Bulb is a remote object Bulb

Introduction of RMI What is Remote Method Invocation? Bulb is a remote object Bulb Server Bulb. on() client Bulb. off() public class Light. Bulb { private boolean light. On; public void on() { light. On = true; } client 1. How to identify a remote object and its methods? 2. How to invoke a method of a remote object (e. g. , parameters passing, result returning)? } 3. How to locate a remote object? public void off() { light. On = false; } public boolean is. On() { return light. On; }

Introduction of RMI Primary goal of RMI n Allow programmers to develop distributed Java

Introduction of RMI Primary goal of RMI n Allow programmers to develop distributed Java programs with the same syntax and semantic used for non-distributed programs RMI vs. RPC n n RMI is for Java only, allowing Java objects on different JVM to communicate each other RMI is object-oriented w Input parameters could be objects n These objects could be executed in a remote host w Return value could be an object as well

RMI Architecture Each remote object has two separate parts n Definition of its behavior

RMI Architecture Each remote object has two separate parts n Definition of its behavior w Clients are concerned about the definition of a service w Coded using a Java interface, which defines the behavior n Implementation of its behavior w Servers are focused on providing the service w Coded using a Java class, which defines the implementation

RMI Layers RMI System Client Code Server Code Stubs Skeletons Remote Reference Layer Transport

RMI Layers RMI System Client Code Server Code Stubs Skeletons Remote Reference Layer Transport Layer (TCP/IP) Each remote object has two interfaces n n Client interface – a stub/proxy of the object Server interface – a skeleton of the object The communication of stub and skeleton is done across the RMI link n Read parameters/make call/accept return/write return back to the stub Remote reference layer defines and supports the invocation semantics of the RMI connection

Java Application Get. Local. Time(time, valid) Client Code Server Code Stubs Skeletons Remote Reference

Java Application Get. Local. Time(time, valid) Client Code Server Code Stubs Skeletons Remote Reference Layer Get. Local. OS(OS, valid) Get. Local. OS : : C RPC Implementation RPC_n Call the RPC implementation Base. Struct Send/Recv() TCP/UDP Socket J 2 C Transport Layer (TCP/IP) RMI

RMI Components RMI registry n n Each remote object needs to register their location

RMI Components RMI registry n n Each remote object needs to register their location RMI clients find remote objects via the lookup service Server hosting a remote object n n n Construct an implementation of the object Provide access to methods via skeleton Register the object to the RMI registry Client using a remote object n n n Ask registry for location of the object Construct stub Call methods via the object’s stub RMIRegistry 2. look for server object client 3. return server stub 1. register server objects 3. call and data communication server

Steps of Using RMI 1. 2. 3. 4. 5. Create Service Interface Implement Service

Steps of Using RMI 1. 2. 3. 4. 5. Create Service Interface Implement Service Interface Create Stub and Skeleton Classes Create RMI Server Create RMI Client 1. Define Service Interface 5. Develop Client program 2. Develop Service Implementation 4. Create and register Server Object 3. rmic stub skeleton

1. Defining RMI Service Interface Declare an Interface that extends java. rmi. Remote n

1. Defining RMI Service Interface Declare an Interface that extends java. rmi. Remote n n Stub, skeleton, and implementation will implement this interface Client will access methods declared in the interface Example public interface RMILight. Bulb extends java. rmi. Remote { public void on () throws java. rmi. Remote. Exception; public void off() throws java. rmi. Remote. Exception; public boolean is. On() throws java. rmi. Remote. Exception; }

2. Implementing RMI Service Interface Provide concrete implementation for each methods defined in the

2. Implementing RMI Service Interface Provide concrete implementation for each methods defined in the interface public class RMILight. Bulb. Impl extends java. rmi. server. Unicast. Remote. Object implements RMILight. Bulb { public RMILight. Bulb. Impl() throws java. rmi. Remote. Exception {set. Bulb(false); } private boolean light. On; public void on() throws java. rmi. Remote. Exception { set. Bulb(true); } public void off() throws java. rmi. Remote. Exception {set. Bulb(false); } public boolean is. On() throws java. rmi. Remote. Exception { return get. Bulb(); } public void set. Bulb (boolean value) { light. On = value; } public boolean get. Bulb () { return light. On; } }

3. Generating Stub & Skeleton Classes Simply run the rmic command on the implementation

3. Generating Stub & Skeleton Classes Simply run the rmic command on the implementation class Example: n rmic RMILight. Bulb. Impl n creates the classes: w RMILight. Bulb. Impl_Stub. class n Client stub w RMILight. Bulb. Impl_Skeleton. class n Server skeleton

4. Creating RMI Server Create an instance of the service implementation Register with the

4. Creating RMI Server Create an instance of the service implementation Register with the RMI registry (binding) import java. rmi. *; import java. rmi. server. *; public class Light. Bulb. Server { public static void main(String args[]) { try { RMILight. Bulb. Impl bulb. Service = new RMILight. Bulb. Impl(); Remote. Ref location = bulb. Service. get. Ref(); System. out. println (location. remote. To. String()); String registry = "localhost"; if (args. length >=1) { registry = args[0]; } String registration = "rmi: //" + registry + "/RMILight. Bulb"; Naming. rebind( registration, bulb. Service ); } catch (Exception e) { System. err. println ("Error - " + e); } } }

5. Creating RMI Client Obtain a reference to the remote interface Invoke desired methods

5. Creating RMI Client Obtain a reference to the remote interface Invoke desired methods on the reference import java. rmi. *; public class Light. Bulb. Client { public static void main(String args[]) { try { String registry = "localhost"; if (args. length >=1) { registry = args[0]; } String registration = "rmi: //" + registry + "/RMILight. Bulb"; Remote remote. Service = Naming. lookup ( registration ); RMILight. Bulb bulb. Service = (RMILight. Bulb) remote. Service; bulb. Service. on(); System. out. println ("Bulb state : " + bulb. Service. is. On() ); System. out. println ("Invoking bulbservice. off()"); bulb. Service. off(); System. out. println ("Bulb state : " + bulb. Service. is. On() ); } catch (Not. Bound. Exception nbe) { System. out. println ("No light bulb service available in registry!"); } catch (Remote. Exception re) { System. out. println ("RMI - " + re); } catch (Exception e) { System. out. println ("Error - " + e); } } }

Steps of Running RMI Make the classes available in the server host's, registry host's,

Steps of Running RMI Make the classes available in the server host's, registry host's, and client host's classpath n Copy, if necessary Start the registry n rmiregistry Start the server n java Light. Bulb. Server reg-hostname Start the client n java Light. Bulb. Client reg-hostname

Another Example: Compute Server Client Re su Ta sk Client t l u s

Another Example: Compute Server Client Re su Ta sk Client t l u s re lt Task tas k Server An Example of Corporate Server

Task interface public interface Task { Object run(); } When run is invoked, it

Task interface public interface Task { Object run(); } When run is invoked, it does some computation and returns an object that contains the results

Remote Interface of Compute. Server import java. rmi. * public interface Compute. Server extends

Remote Interface of Compute. Server import java. rmi. * public interface Compute. Server extends Remote { Object compute(Task task) throws Remote. Exception; } The only purpose of this remote interface is to allow a client to create a task object and send it to the Server for execution, returning the results

Remote Object Compute. Server. Impl import java. rmi. *; Import java. rmi. server. *;

Remote Object Compute. Server. Impl import java. rmi. *; Import java. rmi. server. *; public class Compute. Server. Impl extends Unicast. Remote. Object implements Compute. Server { public Compute. Server. Impl() throws Remote. Exception { } public Object compute(Task task) { return task. run(); } public static void main(String[] args) throws Exception { Compute. Server. Impl server = new Compute. Server. Impl(); Naming. rebind(“Compute. Server”, server); } }

A Task Example public class My. Task implements Task, Serializable { double data[]; My.

A Task Example public class My. Task implements Task, Serializable { double data[]; My. Task() { Read. File(data, “c: data. txt”); } double run() { // some CPU-intensive operations on data[]; } }

Submitting a Task public class Run. Task { public static void main(String[] args) throws

Submitting a Task public class Run. Task { public static void main(String[] args) throws Exception { Mytask my. Task = new My. Task(); // set the data[] of my. Task; } } // submit to the remote compute server and get result back Remote cs = Naming. lookup(“rmi: //localhost/Compute. Server”); Double result = (Compute. Server) cs). compute(my. Task);

RMI Safety and Security RMISecurity. Manager imposes restrictions on downloaded objects the same on

RMI Safety and Security RMISecurity. Manager imposes restrictions on downloaded objects the same on applets n n No access to local disk I/O No socket connection except to codebase, etc. public static void main(String[] args) throws Exception { System. set. Security. Manager(new RMISecurity. Manager()); Compute. Server. Impl server = new Compute. Server. Impl(); Naming. rebind(“Compute. Server”, server); return; }

Firewalls block all network traffic, with the exception of those intended for certain “well-known”

Firewalls block all network traffic, with the exception of those intended for certain “well-known” ports RMI traffic is typically blocked by firewall n RMI transport layer opens dynamic socket connections between the client and the server to facilitate communication Client WWW Stub Firewalls Server

RMI Solutions The sequence of trying to make connections: n n Communicate directly to

RMI Solutions The sequence of trying to make connections: n n Communicate directly to the server’s port using sockets If this fails, build a URL to the server’s host and port and use an HTTP post request on that URL, sending the information to the skeleton as the body of the POST. n n If this also fails, build a URL to the server’s host using port 80, the standard HTTP port, using a CGI script that will forward the posted RMI request to the server. n n need to set system property http. proxyhost java-rmi. cgi script needs to be install java. rmi. server. hostname = host. domain. com A more efficient solution is using servlet If all fails, RMI fails.

Summary RMI is a Java middleware to deal with remote objects based on RPC

Summary RMI is a Java middleware to deal with remote objects based on RPC communication protocol n n Interface defines behaviour and class defines implementation Remote objects are pass across the network as stubs and nonremote objects are copies RMI will not replace CORBA since a JAVA client may require to interact with a C/C++ server RMI fits well in n-tier architectures since it can intermix easily with servlets