RMI Java Remote Method Invocation 1999 F Maurer

  • Slides: 25
Download presentation
RMI Java Remote Method Invocation © 1999 F. Maurer / G. Succi Advanced Implementation

RMI Java Remote Method Invocation © 1999 F. Maurer / G. Succi Advanced Implementation Techniques

Java RMI Remote method invocation addresses the incorporation of the network into a programming

Java RMI Remote method invocation addresses the incorporation of the network into a programming language, a key issue in network computing. 1/4/2022 2

RMI Goals Support seamless remote method invocation on objects in different virtual machines. Support

RMI Goals Support seamless remote method invocation on objects in different virtual machines. Support callbacks from servers to applets. Integrate the distributed object model into the Java language in a natural way while retaining most of the Java language’s object semantics. Make differences between the distributed object model and local Java object model apparent. Make writing reliable distributed applications as simple as possible. Preserve the safety provided by the Java runtime environment. 1/4/2022 3

RMI Extended Goals Garbage collection of remote objects Server replication Activation of persistent objects

RMI Extended Goals Garbage collection of remote objects Server replication Activation of persistent objects to service an invocation Simple invocation to a single object or invocation to an object replicated at multiple locations. 1/4/2022 4

JAVA RMI Architecture 1/4/2022 5

JAVA RMI Architecture 1/4/2022 5

Stubs and Skeletons Client Side Network m Client 1/4/2022 Server Side Stub m Skeleton

Stubs and Skeletons Client Side Network m Client 1/4/2022 Server Side Stub m Skeleton Server 6

Tasks of the Stub Initiating a call to the remote object Marshaling arguments to

Tasks of the Stub Initiating a call to the remote object Marshaling arguments to a marshal stream Informing the remote reference layer that the call should be invoked. Unmarshaling the return value or exception from a marshal stream. Informing the remote reference layer that the call is complete. 1/4/2022 7

Tasks of the Skeleton Unmarshaling arguments from the marshal stream. Making the up-call to

Tasks of the Skeleton Unmarshaling arguments from the marshal stream. Making the up-call to the actual remote object implementation Marshaling the return value of the call or an exception (if one occurred) onto the marshal stream. 1/4/2022 8

How to get Stub/Skeletons? Specify the protocol of the server object n n Interface

How to get Stub/Skeletons? Specify the protocol of the server object n n Interface definition language: Java Interface has to extend java. rmi. Remote Automatically generate Stub/Skeleton code n 1/4/2022 RMI Compiler 9

Parameter Passing in RMI Pass by reference n Remote objects implement Remote interface Pass

Parameter Passing in RMI Pass by reference n Remote objects implement Remote interface Pass by value (copy) n n 1/4/2022 Basic Types Non-remote objects are passed as copies has to implement java. io. Serializable 10

Implementing a Remote Interface The class usually extends java. rmi. server. Unicast. Remote. Object,

Implementing a Remote Interface The class usually extends java. rmi. server. Unicast. Remote. Object, thereby inheriting the remote behavior provided by the classes java. rmi. server. Remote. Object and java. rmi. server. Remote. Server. The class can implement any number of remote interfaces. The class can extend another remote implementation class. The class can define methods that do not appear in the remote interface, but those methods can only be used locally and are not available remotely. 1/4/2022 11

Locating Remote Objects Simple name server provided Server side: Bank. Account acct = new

Locating Remote Objects Simple name server provided Server side: Bank. Account acct = new Bank. Acct. Impl(); String url = "account"; // bind url to object java. rmi. Naming. bind(url, acct); … Client Side // lookup account acct =(Bank. Account) java. rmi. Naming. lookup(“rmi: //localhost/account”); 1/4/2022 12

RMI Registry RMI uses a network-based registry to keep track of the distributed objects.

RMI Registry RMI uses a network-based registry to keep track of the distributed objects. The server object makes a method available for remote invocation by binding itself to a name in the registry. The client object 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. 1/4/2022 13

How RMI works. . . When a client invokes a server method, the JVM

How RMI works. . . When 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. The stub acts as a proxy to the skeleton and the skeleton is a proxy to the actual remote method. 1/4/2022 14

Sample Application -client package ncworld. rmi 1; import java. rmi. *; import java. rmi.

Sample Application -client package ncworld. rmi 1; import java. rmi. *; import java. rmi. server. *; public class Client 1 { public static void main(String[] args) { System. set. Security. Manager(new RMISecurity. Manager()); try {Server 1 ro = (Server 1) Naming. lookup("do. Something"); System. out. println("Location: ” + System. get. Property("LOCATION")); ro. do. Something(); } catch (Exception e){ e. print. Stack. Trace(); System. exit(-1); } } } 1/4/2022 15

About the Sample-client Client: invokes the do. Something() method of a remote object (of

About the Sample-client Client: invokes the do. Something() method of a remote object (of type Server 1). All RMI-based applications will need to import java. rmi and java. rmi. server packages. Security manager: grants or denies permissions on the operations performed by the application If you don't set the security manager, RMI will only load classes from local system files as defined by CLASSPATH. 1/4/2022 16

About the Sample-client. . . try/catch block: n n binds remote object to variable

About the Sample-client. . . try/catch block: n n binds remote object to variable ro (type: Server 1) performs the remote method invocation The client can then use ro and invoke its methods as if it was a local object. The client application invokes the do. Something method of the object ro, which in turn invokes the method with the same name on the Server 1 object. 1/4/2022 17

Server interface package ncworld. rmi 1; import java. rmi. *; public interface Server 1

Server interface package ncworld. rmi 1; import java. rmi. *; public interface Server 1 extends Remote { public void do. Something() throws Remote. Exception; } 1/4/2022 18

About the Sample - Server Interface Server 1 is used in the client application

About the Sample - Server Interface Server 1 is used in the client application to declare the remote object type. All remote objects are referenced through interfaces. Two points must be made about the Server 1 interface n n It extends the Remote interface as all RMI interfaces must; and the method do. Something() throws Remote. Exception which all remote operations must be able to handle. 1/4/2022 19

Server Implementation (1) package ncworld. rmi 1; import java. rmi. *; import java. rmi.

Server Implementation (1) package ncworld. rmi 1; import java. rmi. *; import java. rmi. server. *; import java. rmi. registry. *; public class Server 1 Impl extends java. rmi. server. Unicast. Remote. Object implements Server 1 { public static void main(String[] args) { System. set. Security. Manager(new RMISecurity. Manager()); try {Server 1 Impl obj = new Server 1 Impl(); Naming. rebind("do. Something", obj); //Naming. bind also possible System. out. println("do. Something bound in registry"); } catch (Exception e) { e. print. Stack. Trace(); } } 1/4/2022 20

Server Implementation (2) public Server 1 Impl() throws Remote. Exception { } public void

Server Implementation (2) public Server 1 Impl() throws Remote. Exception { } public void do. Something() throws Remote. Exception { System. out. println("This message is printed by the Server 1 object"); System. out. println("Location: " + System. get. Property("LOCATION")); } 1/4/2022 21

About the Sample - Server Implementation The Server 1 Impl class extends the Unicast.

About the Sample - Server Implementation The Server 1 Impl class extends the Unicast. Remote. Object class. n n non-replicated remote object whose references are valid only while the server process is alive point-to-point active object references via TCP streams. instantiates an object of type Server 1 Impl register object using the name do. Something. A remote implementation class must have a zero-argument constructor that may throw Remote. Exception. Finally, the do. Something method is defined n n n 1/4/2022 it throws Remote. Exception. this remote method doesn't really do anything. In a more meaningful application, the remote method may perform a query on a database, or read data from a file and process that data. 22

Deployment with JDK We have a client, a server, and an implementation of the

Deployment with JDK We have a client, a server, and an implementation of the server interface. We still need two more pieces -- the stub and the skeleton. JDK 1. 1. x up contains tool to generate these rmic rmi 1. Server 1 Impl n This will create the two files w Server. Impl_Stub. class w Server. Impl_Skel. class. n 1/4/2022 Should you want to see the Java source code for these files, use the -keepgenerated option. 23

Run with JDK We now have all the necessary pieces for our application. You

Run with JDK We now have all the necessary pieces for our application. You can open three windows and execute the following commands in a separate window in the order shown below: rmiregistry & java -DLOCATION=server rmi 1. Server 1 Impl java -DLOCATION=client rmi 1. Client 1 1/4/2022 24

RMI Summary Language-dependent Passing parameter objects supported Easy to use 1/4/2022 25

RMI Summary Language-dependent Passing parameter objects supported Easy to use 1/4/2022 25