+ All Categories

RMI

Date post: 31-Dec-2015
Category:
Upload: brittany-perez
View: 53 times
Download: 0 times
Share this document with a friend
Description:
RMI. Java Remote Method Invocation. Java RMI. Remote method invocation addresses the incorporation of the network into a programming language, a key issue in network computing. RMI Goals. Support seamless remote method invocation on objects in different virtual machines. - PowerPoint PPT Presentation
25
RMI Java Remote Method Invocation
Transcript

RMI

Java Remote Method Invocation

04/19/23 2

Java RMI

Remote method invocation addresses the incorporation of the network into a programming language, a key issue in network computing.

04/19/23 3

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.

04/19/23 4

RMI Extended Goals

Garbage collection of remote objectsServer replicationActivation of persistent objects to service an invocationSimple invocation to a single object or invocation to an object replicated at multiple locations.

04/19/23 5

JAVA RMI Architecture

04/19/23 6

Stubs and Skeletons

Client Stub ServerSkeleton

Client Side Server Side

Networkm m

04/19/23 7

Tasks of the Stub

Initiating a call to the remote objectMarshaling arguments to a marshal streamInforming 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.

04/19/23 8

Tasks of the Skeleton

Unmarshaling arguments from the marshal stream.Making the up-call to the actual remote object implementationMarshaling the return value of the call or an exception (if one occurred) onto the marshal stream.

04/19/23 9

How to get Stub/Skeletons?

Specify the protocol of the server object Interface definition language: Java Interface has to extend

java.rmi.Remote

Automatically generate Stub/Skeleton code RMI Compiler

04/19/23 10

Parameter Passing in RMI

Pass by reference Remote objects

implement Remote interface

Pass by value (copy) Basic Types Non-remote objects are passed as

copieshas to implement

java.io.Serializable

04/19/23 11

Implementing a Remote Interface

The class usually extendsjava.rmi.server.UnicastRemoteObject,

thereby inheriting the remote behavior provided by the classes

java.rmi.server.RemoteObject andjava.rmi.server.RemoteServer.

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.

04/19/23 12

Locating Remote Objects

Simple name server providedServer side:BankAccount acct = new BankAcctImpl();String url = "account";// bind url to objectjava.rmi.Naming.bind(url, acct);…Client Side// lookup accountacct =(BankAccount) java.rmi.Naming.lookup(“rmi://localhost/account”);

04/19/23 13

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.

04/19/23 14

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.

04/19/23 15

Sample Application -clientpackage ncworld.rmi1;import java.rmi.*;import java.rmi.server.*;public class Client1 { public static void main(String[] args) { System.setSecurityManager(new RMISecurityManager()); try {Server1 ro = (Server1) Naming.lookup("doSomething"); System.out.println("Location: ” +

System.getProperty("LOCATION")); ro.doSomething(); } catch (Exception e){ e.printStackTrace(); System.exit(-1);} } }

04/19/23 16

About the Sample-client

Client: invokes the doSomething() method of a remote object (of type Server1). 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 applicationIf you don't set the security manager, RMI will only load classes from local system files as defined by CLASSPATH.

04/19/23 17

About the Sample-client ...

try/catch block: binds remote object to variable ro (type: Server1) 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 doSomething method of the object ro, which in turn invokes the method with

the same name on the Server1 object.

04/19/23 18

Server interface

package ncworld.rmi1;

import java.rmi.*;

public interface Server1 extends Remote { public void doSomething()

throws RemoteException; }

04/19/23 19

About the Sample - Server

Interface Server1 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 Server1 interface

It extends the Remote interface as all RMI interfaces must; and

the method doSomething() throws RemoteException

which all remote operations must be able to handle.

04/19/23 20

Server Implementation (1)

package ncworld.rmi1; import java.rmi.*; import java.rmi.server.*; import java.rmi.registry.*;public class Server1Impl extends java.rmi.server.UnicastRemoteObject implements Server1 { public static void main(String[] args) { System.setSecurityManager(new RMISecurityManager()); try {Server1Impl obj = new Server1Impl(); Naming.rebind("doSomething", obj); //Naming.bind also possible System.out.println("doSomething bound in registry"); } catch (Exception e) { e.printStackTrace();} }

04/19/23 21

Server Implementation (2)

public Server1Impl() throws RemoteException { }

public void doSomething() throws RemoteException {

System.out.println("This message is printed by the

Server1 object"); System.out.println("Location: " +

System.getProperty("LOCATION")); }

04/19/23 22

About the Sample - Server Implementation

The Server1Impl class extends the UnicastRemoteObject class.

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 Server1Impl register object using the name doSomething.

A remote implementation class must have a zero-argument constructor that may throw RemoteException. Finally, the doSomething method is defined

it throws RemoteException. 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.

04/19/23 23

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 rmi1.Server1Impl This will create the two files

ServerImpl_Stub.class ServerImpl_Skel.class.

Should you want to see the Java source code for these files, use the -keepgenerated option.

04/19/23 24

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 rmi1.Server1Impljava -DLOCATION=client rmi1.Client1

04/19/23 25

RMI Summary

Language-dependentPassing parameter objects supportedEasy to use


Recommended