Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is...

24
Remote Method Invocation Chin-Chih Chang
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    228
  • download

    3

Transcript of Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is...

Page 1: Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.

Remote Method Invocation

Chin-Chih Chang

Page 2: Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.

Java Remote Object Invocation• In Java, the object is serialized before being passed as a

parameter to an RMI.– Platform-dependent objects such as file descriptors and

sockets cannot be serialized.• During an RMI local objects are passed by object

copying whereas remote objects are passed by reference.• A remote object is built from two different classes:

– Server class – implementation of server-side code.– Client class – implementation of a proxy.

• In Java, a proxy is serializable and is used as a reference to the remote object.– It is possible to marshal a proxy and send it as a series of bytes

to another process. – Passing proxies as parameters works because each process is

executing in the same Java virtual machine.

Page 3: Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.

Remote Method Invocation (RMI)

RMI

java.net

TCP/IP stack

Application

network

• The Java Remote Method Invocation (RMI) system allows an object running in one Java Virtual Machine (VM) to invoke methods on an object running in another Java VM.

• Java RMI provides applications with transparent andlightweight access to remote objects. RMI defines a high-level protocol and API.

• Programming distributed applications in Java RMI is simple. It is a single-language system. The programmer of a remote object must

consider its behavior in a concurrent environment.

Page 4: Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.

Java RMI• A Java RMI application is referred to as a distributed

object application which needs to:– Locate remote objects: Applications can use one of two

mechanisms to obtain references to remote objects. An application can register its remote objects with RMI's simple naming facility, the rmiregistry, or the application can pass and return remote object references as part of its normal operation.

– Communicate with remote objects: Details of communication between remote objects are handled by RMI; to the programmer, remote communication looks like a standard Java method invocation.

– Load class bytecodes for objects that are passed around: Because RMI allows a caller to pass objects to remote objects, RMI provides the necessary mechanisms for loading an object's code, as well as for transmitting its data.

Page 5: Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.

Java RMI

• Java RMI extends the Java object model to provide support for distributed objects in the Java language.• It allows objects to invoke methods on remote objects using

the same syntax as for local invocations.

• Type checking applies equally to remote invocations as to local ones.

• The remote invocation is known because RemoteExceptions has been handled and the remote object is implemented using the Remote interface.

• The semantics of parameter passing is different from the local invocation because the invoker and the target reside on different machines.

Page 6: Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.

RMI Implementation

Java Virtual Machine

ClientObject

Java Virtual Machine

RemoteObject

Stub Skeleton

Client Host Server Host

Page 7: Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.

Remote References and Interfaces

• Remote References– Refer to remote objects, but can be invoked on a

client just like a local object reference

• Remote Interfaces– Declare exposed methods like an RPC specification– Implemented on the client like a proxy for the

remote object

Page 8: Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.

Stubs and Skeletons

• Client Stub – lives on the client– pretends to be the remote object

• Sever Skeleton– lives on the server– receives requests from the stub– talks to the true remote object– delivers the response to the stub

Page 9: Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.

Remote Interface

StubRemote Object

(Server)Client Skeleton

implements implements

Remote Interface

Page 10: Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.

RMI Implementation

• Reference Layer – determines if referenced object is local or remote.

• Transport Layer – packages remote invocations, dispatches messages between stub and skeleton, and handles distributed garbage collection.

• Both layers reside on top of java.net.

Page 11: Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.

RMI Registry

• the RMI registry is a simple server-side bootstrap naming facility that allows remote clients to get a reference to a remote object

• Servers name and register their objects to be accessed remotely with the RMI Registry.

• Clients use the name to find server objects and obtain a remote reference to those objects from the RMI Registry.

• A registry (using the rmiregistry command) is a separate process running on the server machine.

Page 12: Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.

Registry

RMI Registry Architecture

Java Virtual Machine

Client

Java Virtual Machine

Stub

Remote Object

Skeleton

Java Virtual Machine

“Bob”

Server

Page 13: Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.

Creating Distributed Applications Using RMI

• Steps to create a RMI application:1. Design and implement the components of

your distributed application.

2. Compile sources and generate stubs.

3. Make classes network accessible.

4. Start the application.

Page 14: Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.

Design and Implement the Application Components

• This step includes: – Defining the remote interfaces: A remote interface specifies

the methods that can be invoked remotely by a client. – Implementing the remote objects: Remote objects must

implement one or more remote interfaces. The remote object class may include implementations of other interfaces (either local or remote) and other methods (which are available only locally). If any local classes are to be used as parameters or return values to any of these methods, they must be implemented as well.

– Implementing the clients: Clients that use remote objects can be implemented at any time after the remote interfaces are defined, including after the remote objects have been deployed.

Page 15: Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.

Compile Sources and Generate Stubs

• This step includes: – Use the javac compiler to compile the source files,

which contain the implementation of the remote interfaces and implementations, the server classes, and the client classes.

– Use the rmic compiler to create stubs for the remote objects. RMI uses a remote object's stub class as a proxy in clients so that clients can communicate with a particular remote object.

Page 16: Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.

Make Classes Network Accessibleand Start the Application

• Make Classes Network Accessible:– In this step you make everything - the class files

associated with the remote interfaces, stubs, and other classes that need to be downloaded to clients -accessible via a Web server.

• Start the Application– Starting the application includes running the RMI

remote object registry (rmiregistry) , the server, and the client.

Page 17: Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.

Java RMI - Example

• The files needed for creating a Java RMI application are: A remote interface defines the remote interface provided by

the service. Usually, it is a single line statement specifies the service function (HelloInterface.java). (An interface is the skeleton for a public class.)

A remote object implements the remote service. It contains a constructor and required functions. (Hello.java)

A client that invokes the remote method. (HelloClient.java) The server offers the remote service, installs a security

manager and contacts rmiregistry with an instance of the service under the name of the remote object. (HelloServer.java)

Page 18: Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.

HelloInterface.java

import java.rmi.*;

public interface HelloInterface extends Remote {

public String say(String msg) throws RemoteException;

}

Page 19: Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.

Hello.javaimport java.rmi.*;import java.rmi.server.*;public class Hello extends UnicastRemoteObject implements HelloInterface { private String message;

public Hello(String msg) throws RemoteException { message = msg; } public String say(String m) throws RemoteException { // return input message - reversing input and suffixing // our standard message return new StringBuffer(m).reverse().toString() + "\n" +

message; }}

Page 20: Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.

HelloClient.javaimport java.rmi.*;

public class HelloClient { public static void main(String args[]) { String path = "//localhost/Hello"; try { if (args.length < 1) { System.out.println("usage: java HelloClient <host:port> <string> ... \n"); } else path = "//" + args[0] + "/Hello"; HelloInterface hello = (HelloInterface) Naming.lookup(path); for (int i = 0; i < args.length; ++i) System.out.println(hello.say(args[i])); } catch(Exception e) { System.out.println("HelloClient exception: " + e); } }}

Page 21: Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.

HelloServer.javaimport java.rmi.*;import java.rmi.server.*;

public class HelloServer { public static void main(String args[]) { // Create and install a security manager if (System.getSecurityManager() == null) System.setSecurityManager(new RMISecurityManager()); try { Naming.rebind("Hello", new Hello("Hello, world!")); System.out.println("server is running..."); } catch (Exception e) { System.out.println("Hello server failed:" + e.getMessage()); } }}

Page 22: Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.

Steps to Build a RMI application - Example• Write the following Java code:

• Interface - HelloInterface.java• Implementation class – Hello.java• Client – HelloClient.java• Server – HelloServer.java

• Compile the code javac Hello.java HelloClient.java HelloInterface.java

HelloServer.java• Generate Stub and Skeleton class files from the

implementation classes (make sure that your classpath contains your current

directory) rmic Hello

Page 23: Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.

Steps to Build a RMI application - Example

• Start the RMI registry (in a separate window or in the background)

rmiregistry & (be sure to kill this process when you're done)• Start the server in one window or in the background

with the security policy java -Djava.security.policy=policy HelloServer or without the security policy java HelloServer• Run the client in another window java HelloClient testing

Page 24: Remote Method Invocation Chin-Chih Chang. Java Remote Object Invocation In Java, the object is serialized before being passed as a parameter to an RMI.

Registry

RMI Steps

Java Virtual Machine

Client

Java Virtual Machine

Stub

Remote Object

Skeleton

Java Virtual Machine

“Bob”

Server

Server creates and registers remote object

Client obtains a remote reference from the registry, and a stub is created