remote method invocation instructors: fu-chiung cheng ( 鄭福炯 ) associate professor computer...

40
Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭鄭鄭 ) Associate Professor Computer Science & Engineering Tatung University

Upload: barbra-nelson

Post on 19-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Remote Method Invocation

Instructors: Fu-Chiung Cheng

(鄭福炯 )Associate Professor

Computer Science & EngineeringTatung University

Page 2: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Contents

• RMI Concept from Java Network Programming • RMI (Sun)

– http://java.sun.com/j2se/1.3/docs/guide/rmi/index.html– http://developer.java.sun.com/developer/

onlineTraining/rmi/RMI.html

• Examples:– JBuilder Simple RMI – RMI Calculator

Page 3: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

RMI Concepts

• Two fundamental network applications–File and data transfer (FTP, SMTP (email), HTTP, …) –Allowing one host to run programs on another host (telnet, rlogin Remote procedure call (RPC), CGI..

• Remote Method Invocation (RMI) allows Java programs call other Java programs on the other machines.

• RMI == > distributed/parallel computing

Page 4: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Remote Method Invocation

• The remote method invocation API let Java objects on different hosts communicate with each other

• A remote lives on a server• Each remote object implements a remote

interface that specifies which of its methods can be invoked by clients.

• Clients invoke the methods of the remote object almost exactly as they invoke local mthods.

Page 5: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Remote Method Invocation

• Example:– An object running on a local client can pass a database query as a String argument to a method in a database object running on a remote server to ask it to sum up a series of records.– The server can return the result to the client as a double.– This is more efficient than download all the records and summing them up locally.

Page 6: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Remote Method Invocation

• From the programmer’s perspective, remote objects and methods work just like the local objects and methods

• All the implementation details are hidden.• You just

– import one package, – look up the remote object in a registry and – make sure that you catch the RemoteException

when you call the object’s methods

Page 7: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Remote Method Invocation

• A remote object is an object whose methods may be invoked from a different Java virtual machine.

• Each remote object implements one or more remote interfaces that declare which methods of the remote object can be invoked by the foreign system.

• RMI is the facility by which a Java program running on one machine can invoke a method in an object on a complete different machine.

Page 8: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Security

• The prospect of remote hosts invoking methods in the local host’s object raises many security issues

• The activities that a remote object can perform are limited in much the same way an applet’s activity is limited

• A Security Manager object checks all operations to make sure they’re allowed

• Custom security managers can be defined for specific applications

Page 9: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Security

• Public key authentication can be used to verify a user’s identity and allow different users different levels of access to a remote object.

• For example:– General public may be allowed to query a

database but not update it.– Users from inside a company might be

allowed to both query and update the database.

Page 10: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Object Serialization

• When a object is passed to or returned from a Java method, what is really transferred is a reference to the object

• Reference or object:– A special remote reference to the object is passed– A copy of the object can be passed

• Reference: the local machine passes a remote object to the remote machine

• Copied object: the local machine passes its own object (copy the object and send the copied object.)

Page 11: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Object Serialization

• To copy an object, you need a way to convert the object into a stream of bytes.– May not be easy: Objects may include other

objects

• Object Serialization is a scheme by which objects can be converted into a byte stream and then passed around to other machines, which rebuild the original objects from to bytes

• These bytes can also be written to disk and read back from disk at a later time.

Page 12: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Object Serialization

• For security reasons, Java places some limitations on which objects can be serialized– All Java primitive types can be serialized – Object Java objects that implement java.io.Serializable

interface

• Serializable objects:– String and Component– Vector– Integer, Float (inherits from Number which is serializable)– Exceptions– Most AWT and Swing

Page 13: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

RMI

• The fundamental difference between remote objects and local objects is that remote objects resides in different VM

• Method call: pass references (local machine is ok)

• Pass arguments (RMI way)– Primitive types are passed by values– Reference to remote objects are passed as a remote

reference– Local objects are passed by values (object

serialization)

Page 14: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

RMI

• RMI Layer model (see Fig 18.1 on page 599)

Page 15: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

RMI

• RMI Layer model (see Fig 18.1 on page 599)– The client appears to talk directly to the server– In reality, the client program talks only to the a stub.– The stub passes that conversation along to the remote

reference layer– The remote reference layer talks to the transport layer– The transport layer on the client passes the data across

the Internet to the transport layer on the server– The transport layer on the server then communicates with

the server’s remote reference layer– The remote reference layer talks to the skeleton– The skeleton communicates with the server.

Page 16: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

RMI

• RMI applications are often comprised of two

separate programs: a server and a client. • RMI provides the mechanism by which the

server and the client communicate and pass information back and forth.

• Such an application is sometimes referred to as a distributed object application.

Page 17: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Java RMI Architecture

• The RMI architecture is based on one important principle: the definition of behavior and the implementation of that behavior are separate concepts.

• RMI allows the code that defines the behavior and the code that implements the behavior to remain separate and to run on separate JVMs.

Page 18: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Java RMI Architecture

• Specifically, in RMI, the definition of a remote service is coded using a Java interface.

• The implementation of the remote service is coded in a class.

• Therefore, the key to understanding RMI is to remember that interfaces define behavior and classes define implementation.

Page 19: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Java RMI Architecture

Client Program Server Program

Interface implementation

Page 20: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Java RMI Architecture

Server rmiregistry Client

1. bind/rebind 2. lookup

3. Remote Procedure Call

Stub class Stub class

Page 21: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Naming (java.rmi.Naming)

• static void bind(String name, Remote obj)– Binds the specified name to a remote object.

• static String[] list(String name)– Returns an array of the names bound in the

registry.• static Remote lookup(String name)

– Returns a reference, a stub, for the remote object associated with the specified name.

Page 22: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Naming (java.rmi.Naming)

• static void rebind(String name, Remote obj)– Rebinds the specified name to a new

remote object.• static void unbind(String name)

– Destroys the binding for the specified name that is associated with a remote object.

Page 23: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Creating RMI (Distributed) Applications

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 24: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Creating RMI (Distributed) Applications

1. Design and implement the components of your distributed application.

– Job: decide on your application architecture and determine local objects and remote objects.

– 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.

Page 25: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Creating RMI (Distributed) Applications

1. Design and implement the components of your distributed application.

– Implementing the clients: • Clients that use remote objects can be

implemented at any time after the remote interfaces are defined.

• Clients can be invoked after the remote objects have been deployed.

Page 26: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Creating RMI (Distributed) Applications

2. Compile sources and generate stubs.– use the javac 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 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 27: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Creating RMI (Distributed) Applications

3. Make classes network accessible.

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

Page 28: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Creating RMI (Distributed) Applications

4. Start the application. – Starting the application includes running

the RMI remote object registry, the server, and the client

– 1. Start rmiregistry– 2. Start server (bind services)– 3. Start client (use services)

Page 29: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

RMI componets

• A working RMI system is composed of several parts:– Interface definitions for the remote services – Implementations of the remote services – Stub and Skeleton files – A server to host the remote services – An RMI Naming service that allows clients to find

the remote services – A class file provider (an HTTP or FTP server) – A client program that needs the remote services

Page 30: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

RMI Example

• RMI Calculator client/server application– Interface (define remote services):

Calculator.java– Implementation of remote services:

CalculatorImpl.java– Stub and Skeleton: CalculatorImpl_Stub.java

and CalculatorImpl_Skel.java– Server program: CalculatorServer.java– Client program: CalculatorClient.java

Page 31: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

RMI Calculator: Interface

• The Calculator interface defines all of the remote features offered by the service:

public interface Calculator extends java.rmi.Remote { public long add(long a, long b) throws RemoteException; public long sub(long a, long b) throws RemoteException; public long mul(long a, long b) throws RemoteException; public long div(long a, long b) throws RemoteException; }

• >javac Calculator.java

Page 32: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

RMI Calculator: Implementation

public class CalculatorImpl extends UnicastRemoteObject implements Calculator {

public CalculatorImpl() throws java.rmi.RemoteException { super(); }

public long add(long a, long b) throws RemoteException { return a + b; }

public long sub(long a, long b) throws RemoteException { return a - b; }

public long mul(long a, long b) throws RemoteException { return a * b; }

public long div(long a, long b) throws RemoteException { return a / b; }

}

Page 33: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

RMI Calculator: Implementation

• The implementation class uses UnicastRemoteObject to link into the RMI system.

• This is not a requirement. A class that does not extend UnicastRemoteObject may use its exportObject() method to be linked into RMI.

• When a class extends UnicastRemoteObject, it must provide a constructor that declares that it may throw a RemoteException object.

• When this constructor calls super(), it activates code in UnicastRemoteObject that performs the RMI linking and remote object initialization.

Page 34: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

RMI Calculator: Stub and Skeleton

• use the RMI compiler, rmic, to generate the stub and skeleton files.

• The compiler runs on the remote service implementation class file.

• >rmic CalculatorImpl

• After you run rmic you should find the file– Calculator_Stub.class (both 1.1 and 1.2) – Calculator_Skel.class (only 1.1).

Page 35: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Command rmic

Usage: rmic <options> <class names> <options> includes: -keep Do not delete intermediate generated source files -keepgenerated (same as "-keep") -g Generate debugging info -depend Recompile out-of-date files recursively -nowarn Generate no warnings -verbose Output messages about what the compiler is doing -classpath <path> Specify where to find input source and class files -d <directory> Specify where to place generated class files -J<runtime flag> Pass argument to the java interpreter

Page 36: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Command rmic

Usage: rmic <options> <class names>

<options> includes:

The Java 2 platform version of rmic add three new options:

-v1.1 Create stubs/skeletons for JDK 1.1 stub protocol version

-vcompat (default) Create stubs/skeletons compatible with both JDK 1.1 and Java 2 stub protocol versions

-v1.2 Create stubs for Java 2 stub protocol version only

Page 37: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

RMI Calculator: Server program

• Remote RMI services must be hosted in a server process.

• The class CalculatorServer is a very simple server that provides the bare essentials for hosting

Page 38: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

RMI Calculator: Server program

import java.rmi.Naming; public class CalculatorServer { public CalculatorServer() { try { Calculator c = new CalculatorImpl(); Naming.rebind(" rmi://localhost:1099/CalculatorService", c); } catch (Exception e) { System.out.println("Trouble: " + e); } } public static void main(String args[]) { new CalculatorServer(); }}

Page 39: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

RMI Calculator: Client program

public class CalculatorClient { public static void main(String[] args) { try { Calculator c = (Calculator) Naming.lookup( "rmi://remotehost/CalculatorService");

System.out.println( c.sub(4, 3) ); System.out.println( c.add(4, 5) ); System.out.println( c.mul(3, 6) );

System.out.println( c.div(9, 3) ); } catch (MalformedURLException murle) { …. }

Page 40: Remote Method Invocation Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University

Running RMI Applications

1. Start with the Registry. – $rmiregistry & (unix) or >start rmiregistry

(windows)

2. Start the server hosting the CalculatorService– >java CalculatorServer

3. Start the client program. – >java CalculatorClient