Web. Remote Method Invocation. core. programming. Training Courses: Java, JSP, Servlets, Struts, & JSF:

Similar documents
JAVA RMI. Remote Method Invocation

RMI Case Study. A Typical RMI Application

Contents. Java RMI. Java RMI. Java RMI system elements. Example application processes/machines Client machine Process/Application A

presentation DAD Distributed Applications Development Cristian Toma

5.4. Events and notifications

Remote Method Invocation

Distributed Systems COMP 212. Lecture 10 Othon Michail

Object Interaction. Object Interaction. Introduction. Object Interaction vs. RPCs (2)

Remote Procedure Call

RPC flow. 4.3 Remote procedure calls IDL. RPC components. Procedure. Program. sum (j,k) int j,k; {return j+k;} i = sum (3,7); Local procedure call

Generic architecture

RMI Example RMI. CmpE 473 Internet Programming RMI

Chapter 15: Distributed Communication. Sockets Remote Procedure Calls (RPCs) Remote Method Invocation (RMI) CORBA Object Registration

Written by: Dave Matuszek

03 Remote invocation. Request-reply RPC. Coulouris 5 Birrel_Nelson_84.pdf RMI

RMI (Remote Method Invocation) Over the year, there have been 3 different approaches to application development:

RMI. Remote Method Invocation. 16-Dec-16

RMI. (Remote Method Invocation)

Distributed Systems. 02r. Java RMI Programming Tutorial. Paul Krzyzanowski TA: Long Zhao Rutgers University Fall 2017

JAC444 - Lecture 11. Remote Method Invocation Segment 2 - Develop RMI Application. Jordan Anastasiade Java Programming Language Course

Grid Computing. Java Remote Method Invocation (RMI) RMI Application. Grid Computing Fall 2006 Paul A. Farrell 9/5/2006

Distributed Computing

The UNIVERSITY of EDINBURGH. SCHOOL of INFORMATICS. CS4/MSc. Distributed Systems. Björn Franke. Room 2414

Last Class: Network Overview. Today: Distributed Systems

Remote Objects and RMI

1 interface TemperatureSensor extends java.rmi.remote 2 { 3 public double gettemperature() throws java.rmi.remoteexception; 4 public void

Distributed Systems. Distributed Object Systems 2 Java RMI. Java RMI. Example. Applet continued. Applet. slides2.pdf Sep 9,

CS 5523 Operating Systems: Remote Objects and RMI

Desarrollo de Aplicaciones en Red RMI. Introduction. Considerations. Considerations. RMI architecture

Remote Method Invocation

CSci Introduction to Distributed Systems. Communication: RPC In Practice

JAVA RMI Java, summer semester

Remote Method Invocation. Benoît Garbinato

CS555: Distributed Systems [Fall 2017] Dept. Of Computer Science, Colorado State University

IBD Intergiciels et Bases de Données

55:182/22C:182. Distributed Application Frameworks Java RMI, CORBA, Web Services (SOAP)

Verteilte Systeme (Distributed Systems)

Remote Method Invocation Benoît Garbinato

Remote Method Invocation in Java

REMOTE METHOD INVOCATION INTRODUCTION TO RMI, A JAVA API FOR RPC-STYLE INVOCATION OF REMOTE OBJECT METHODS

Remote Method Invocation

Network. Dr. Jens Bennedsen, Aarhus University, School of Engineering Aarhus, Denmark

Component-Based Software Engineering

THE RMI PROXY USER GUIDE

Distributed Programming in Java. Distribution (2)

Distributed Software Systems

How are classes loaded into the Java Virtual Machine (JVM)? from the local file system (CLASSPATH). by an instance of ClassLoader

Distributed object component middleware I - Java RMI

Outline. EEC-681/781 Distributed Computing Systems. The OSI Network Architecture. Inter-Process Communications (IPC) Lecture 4

Agent and Object Technology Lab Dipartimento di Ingegneria dell Informazione Università degli Studi di Parma. Distributed and Agent Systems RMI

Distributed object component middleware I - Java RMI

Remote Method Invocation R.M.I.

RPC and RMI. 2501ICT Nathan

Applications. RMI, RPC and events. Request reply protocol External data representation. Operating System

Distributed Programming with RMI. Overview CORBA DCOM. Prepared By: Shiba R. Tamrakar

Distributed Systems. 5. Remote Method Invocation

Course Snapshot. The Next Few Classes. Parallel versus Distributed Systems. Distributed Systems. We have covered all the fundamental OS components:

IJESRT. http: //

Distributed Objects SPL/ SPL 201 / 0 1

#,!" $* ( #+,$ $$ $# -.,$ / 0' ".12 $ $$ 5/ #$" " " $ $ " # $ / 4 * # 6/ 8$8 ' # 6 $! 6$$ #$ * $ $$ ** 4 # 6 # * 0; & *! # #! #(' 7 / $#$ -.

Course Snapshot. The Next Few Classes

Remote Method Invocation Java RMI & Web-Services

Sockets and RMI. CS151 Chris Pollett Dec. 5, 2005.

Modulo II Socket, RMI e Corba

Java WebStart, Applets & RMI

Distributed Systems. 6. Remote Method Invocation. Werner Nutt

EDA095 Remote Method Invocation

EDA095 Remote Method Invocation

CC755: Distributed and Parallel Systems

Distributed Information Systems

Programming with RMI Reminder

5 Distributed Objects: The Java Approach

Info 408 Distributed Applications programming 2 nd semester of Credits: 5 Lecturer: Antoun Yaacoub Ph.D.

Chapter 4 Remote Procedure Calls and Distributed Transactions

Introduction & RMI Basics. CS3524 Distributed Systems Lecture 01

Communication and Distributed Processing

CS193k, Stanford Handout #12. Threads 4 / RMI

Questions and Answers. A. RMI allows us to invoke a method of java object that executes on another machine.

Outline. Chapter 4 Remote Procedure Calls and Distributed Transactions. Remote Procedure Call. Distributed Transaction Processing.

Communication and Distributed Processing

Java RMI Activation: A running example We have the following classes: MyRemoteInterface: the remote interface. Client: the client that invokes a

Chapter 5 Distributed Objects and Remote Invocation

A Typical RMI Application

Activation of remote objects

A Typical RMI Application. Case Study

Are Sockets a Good Programming Paradigm? EDA095 Remote Method Invocation. The Remote Procedure Call Model. The Remote Procedure Call Architecture

Chapter 4: Processes. Process Concept. Process State

Distributed Applications Programming. Lab 4

COMP 6231: Distributed System Design

Distributed Objects. Remote Method Invokation

Are Sockets a Good Programming Paradigm? EDA095 Remote Method Invocation. The Remote Procedure Call Model. The Remote Procedure Call Architecture

Firewall Issues. The possible scenarios: the RMI client, the server, or both can be operating from behind a firewall

Lecture VI: Distributed Objects. Remote Method Invocation

Reflection/RMI 4/28/2009

Java Programming Language Advance Feature

Middleware Labs: Java RMI

SUMMARY INTRODUCTION REMOTE METHOD INVOCATION

Lab 2 : Java RMI. request sayhello() Hello interface remote object. local object. response "Hello world"

Communication Basics, RPC & RMI. CS403/534 Distributed Systems Erkay Savas Sabanci University

EAST WEST UNIVERSITY

Transcription:

core Web programming Remote Method Invocation 1 Training Courses: Java, JSP, Servlets, Struts, & JSF: http://courses.coreservlets.com 2001-2004 Marty Hall, Larry Brown http://

Agenda Steps to build an RMI application Running and compiling an RMI program Example: Retrieving a String remotely Example: Performing numerical integration remotely Enterprise RMI configuration RMI Applets 2

RMI Overview Simpler but less powerful variation of Common Object Request Broker Architecture (CORBA) that only works with Java Systems Object-oriented version of Remote Procedure Calls (RPC) JDP RMI May 19, 2010

RMI: Idea Distribute objects across different machines to take advantage of hardware and dedicated software Developer builds network service and installs it on specified machine User requests an instance of a class using URL syntax User uses object as though it were a regular, local object Network connections happen automatically behind the scenes Java serialization lets you pass complex data structures over the network without writing code to parse and reconstruct them 4

RMI Operations Stub Operation Package identifier of remote object Package method identifier Marshall parameters Send package to server skeleton Skeleton Operation Unmarshall Parameters Calls return value or exception Marshall method return Send package to client stub 5

RMI Details 1. Starting: Build Four Required Classes a. An interface for the remote object Used by both the client and the server b. The RMI client This will look up the object on the remote server, cast it to the type of the interface from Step 1, then use it like a local object. Note that as long as there is a live reference to the remote object, an open network connection is maintained. The connection will be automatically closed when the remote object is garbage collected on the client. c. The object implementation This object needs to implement the interface of Step a, and will be used by the server d. The RMI server This will create an instance of the object from Step c and register it with a particular URL 6

RMI Details, cont. 2. Compile and Run the System a. Compile client and server. Compiles the remote object interface and implementation automatically b. Generate the client stub and the server skeleton Use the rmic compiler on the remote object implementation for this. The client system will need the client class, the interface class, and the client stub class If the client is an applet, these three classes must be available from the applet s home machine The server system will need the server class, the remote object interface and implementation, and the server skeleton class 7

RMI Details, cont. 2. Compile and Run the System, cont. c. Start the RMI registry This only needs to be done once, not for each remote object The current version of RMI requires this registry to be running on the same system as server d. Start the server This step must be on the same machine as the registry of step c e. Start the client This step can be done on an arbitrary machine 8

A Very Simple RMI Example: The Four Required Classes 1. The Interface for the Remote Object The interface should extend java.rmi.remote, and all its methods should throw java.rmi.remoteexception import java.rmi.*; /** The RMI client will use this interface directly. * The RMI server will make a real remote object that * implements this, then register an instance of it * with some URL. */ public interface Rem extends Remote { public String getmessage() throws RemoteException; 9

Simple Example, Required Classes, cont. 2. The RMI Client Look up the object from the host using Naming.lookup, cast it to the appropriate type, then use it like a local object import java.rmi.*; // For Naming, RemoteException, etc. import java.net.*; // For MalformedURLException import java.io.*; // For Serializable interface 10 public class RemClient { public static void main(string[] args) { try { String host = (args.length > 0)? args[0] : "localhost"; Rem remobject = (Rem)Naming.lookup("rmi://" + host + "/Rem"); System.out.println(remObject.getMessage()); catch(remoteexception re) { System.out.println("RemoteException: " + re); catch(notboundexception nbe) { System.out.println("NotBoundException: " + nbe); catch(malformedurlexception mfe) { System.out.println("MalformedURLException: " + mfe);

Simple Example, Required Classes, cont. 3. The Remote Object Implementation This class must extend UnicastRemoteObject and implement the remote object interface defined earlier The constructor should throw RemoteException import java.rmi.*; import java.rmi.server.unicastremoteobject; public class RemImpl extends UnicastRemoteObject implements Rem { public RemImpl() throws RemoteException { public String getmessage() throws RemoteException { return("here is a remote message."); 11

Simple Example, Required Classes, cont. 4. The RMI Server The server builds an object and register it with a particular URL Use Naming.rebind (replace any previous bindings) or Naming.bind (throw AlreadyBoundException if a previous binding exists) import java.rmi.*; import java.net.*; 12 public class RemServer { public static void main(string[] args) { try { RemImpl localobject = new RemImpl(); Naming.rebind("rmi:///Rem", localobject); catch(remoteexception re) { System.out.println("RemoteException: " + re); catch(malformedurlexception mfe) { System.out.println("MalformedURLException: " + mfe);

Simple Example: Compiling and Running the System 1. Compile the Client and the Server Prompt> javac RemClient.java This compiles the Rem interface automatically Prompt> javac RemServer.java This compiles the RemImpl object implementation automatically 2. Generate the Client Stub and Server Skeleton Prompt> rmic RemImpl 13 This builds RemImpl_Stub.class and RemImpl_Skeleton.class The client machine needs Rem.class, RemClient.class, and RemImpl_Stub.class The server machine needs Rem.class, RemImpl.class, RemServer.class, and RemImpl_Skeleton.class

Simple Example: Compiling and Running the System, cont. 3. Start the RMI Registry Server> rmiregistry On Unix systems you would probably add & to put the registry process in the background You can also specify a port number; if omitted, port 1099 is used 4. Start the Server Server> java RemServer 14 Again, on Unix systems you would probably add & to put the process in the background 5. Start the Client Client> java RemClient hostname Here is a remote message.

A Better RMI Example, Numerical Integration 1. Simple Iterative Program to Calculate Sums: 2. Use to Approximate Numeric Integrals of the Form: 3. MidPoint Rule: 15 4. Motivation for RMI Since smaller rectangles typically give better results, this can often be very cpu-intensive RMI can make it available on a fast floating-point box

Numerical Integration, Example, cont. public class Integral { /** Returns the sum of f(x) from x=start to x=stop, where the function f * is defined by the evaluate method of the Evaluatable object. */ public static double sum(double start, double stop, double stepsize, Evaluatable evalobj) { double sum = 0.0, current = start; while (current <= stop) { sum += evalobj.evaluate(current); current += stepsize; return(sum); public static double integrate(double start, double stop, int numsteps, Evaluatable evalobj) { double stepsize = (stop - start) / (double)numsteps; start = start + stepsize / 2.0; return(stepsize * sum(start, stop, stepsize, evalobj)); 16

Numerical Integration, Example, cont. /** An interface for evaluating functions y = f(x) at a specific * value. Both x and y are double-precision floating-point * numbers. */ public interface Evaluatable { public double evaluate(double value); 17

Integration Example: Four Required Classes 1. The RemoteIntegral Interface The interface shared by the client and server import java.rmi.*; public interface RemoteIntegral extends Remote { public double sum(double start, double stop, double stepsize, Evaluatable evalobj) throws RemoteException; public double integrate(double start, double stop, int numsteps, Evaluatable evalobj) throws RemoteException; 18

Integration Example: Four Required Classes, cont. 2. The Remote Integral Client Sends the RemoteIntegral an Evaluatable to integrate 19 public class RemoteIntegralClient { public static void main(string[] args) { try { String host = (args.length > 0)? args[0] : "localhost"; RemoteIntegral remoteintegral = (RemoteIntegral)Naming.lookup("rmi://" + host + "/RemoteIntegral"); for(int steps=10; steps<=10000; steps*=10) { System.out.println("Approximated with " + steps + " steps:" + "\n Integral from 0 to pi of sin(x)=" + remoteintegral.integrate(0.0, Math.PI, steps, new Sin())); System.out.println("'Correct' answer using Math library:" + "\n Integral from 0 to pi of sin(x)=" + (-Math.cos(Math.PI) - -Math.cos(0.0))); catch(remoteexception re) { System.out.println("RemoteException: " + re); catch(notboundexception nbe) { System.out.println("NotBoundException: " + nbe); catch(malformedurlexception mfe) { System.out.println("MalformedURLException: " + mfe);

Integration Example: Four Required Classes, cont. 2. The Remote Integral Client, cont. Evaluatable Sin function import java.io.serializable; class Sin implements Evaluatable, Serializable { public double evaluate(double val) { return(math.sin(val)); public String tostring() { return("sin"); 20

Integration Example: Four Required Classes, cont. 3. The Remote Integral Implementation Remote object that calculates the integral value import java.rmi.*; import java.rmi.server.unicastremoteobject; public class RemoteIntegralImpl extends UnicastRemoteObject implements RemoteIntegral { public RemoteIntegralImpl() throws RemoteException { public double sum(double start, double stop, double stepsize, Evaluatable evalobj) { return(integral.sum(start, stop, stepsize, evalobj)); 21 public double integrate(double start, double stop, int numsteps, Evaluatable evalobj) { return(integral.integrate(start, stop, numsteps, evalobj));

Integration Example: Four Required Classes, cont. 4. The Remote Integral Server Creates the RemoteIntegral and registers it with the rmi registry import java.rmi.*; import java.net.*; public class RemoteIntegralServer { public static void main(string[] args) { try { RemoteIntegralImpl integral = new RemoteIntegralImpl(); Naming.rebind("rmi:///RemoteIntegral", integral); catch(remoteexception re) { System.out.println("RemoteException: " + re); catch(malformedurlexception mfe) { System.out.println("MalformedURLException: " + mfe); 22

Integration Example: Compiling and Running the System 1. Compile the Client and the Server Prompt> javac RemoteIntegralClient.java Prompt> javac RemoteIntegralServer.java 2. Generate the Client Stub and Server Skeleton Prompt> rmic v1.2 RemoteIntegralImpl Client requires: RemoteIntegral.class, RemoteIntegralClient.class and RemoteIntegralImpl_Stub.class Server requires: RemoteIntegral.class, RemoteIntegralImpl.class, and RemoteIntegralServer.class If the server and client are both running JDK 1.1, use the -v1.1 switch to produce the RMI 1.1 skeleton stub, RemoteIntegralImpl_Skeleton, required by the server 23

Integral Example: Compiling and Running the System, cont. 3. Start the RMI Registry Prompt> rmiregistry 4. Start the Server Prompt> java RemoteIntegralServer 5. Start the Client Prompt> java RemoteIntegralClient Approximated with 10 steps: Integral from 0 to pi of sin(x)=2.0082484079079745 Approximated with 100 steps: Integral from 0 to pi of sin(x)=2.0000822490709877 Approximated with 1000 steps: Integral from 0 to pi of sin(x)=2.0000008224672983 Approximated with 10000 steps: Integral from 0 to pi of sin(x)=2.00000000822436... 24

Enterprise RMI Configuration Stub files need to be placed on a HTTP server for downloading In Java 2, the RMI 1.2 protocol does not require the skeleton Client must install an RMISecurityManager to load the RMI classes remotely System.setSecurityManager(new RMISecurityManager()); Client requires a policy file to connect to registry and HTTP server 25

Policy File for Client grant { // rmihost - RMI registry and the server // webhost - HTTP server for stub classes permission java.net.socketpermission "rmihost:1024-65535", "connect"; permission java.net.socketpermission "webhost:80", "connect"; ; Need to grant permission to ports 1024-65535 on the server The server communicates with the rmiregistry (and client) on a randomly selected source port Alternatively, can set policies in java.policy located in JAVA_HOME/lib/security/ 26

Enterprise RMI, Remote Integral, Example public class RemoteIntegralClient2 { public static void main(string[] args) { try { System.setSecurityManager(new RMISecurityManager()); String host = (args.length > 0)? args[0] : "localhost"; RemoteIntegral remoteintegral = (RemoteIntegral)Naming.lookup("rmi://" + host + "/RemoteIntegral"); for(int steps=10; steps<=10000; steps*=10) { System.out.println ("Approximated with " + steps + " steps:" + "\n Integral from 0 to pi of sin(x)=" + remoteintegral.integrate(0.0, Math.PI, steps, new Sin()));... catch(remoteexception re) { System.out.println("RemoteException: " + re);... 27

Enterprise Example: Compiling and Running the System 1. Compile the Client and the Server Prompt> javac RemoteIntegralClient2.java Prompt> javac RemeteIntegralServer.java 2. Generate the Client Stub and Server Skeleton Prompt> rmic v1.2 RemoteIntegralImpl 3. Place the files on the correct machines 28

Enterprise Example: Compiling and Running the System, cont. 4. Start the HTTP Server Place RemoteIntegral_Stub.class, RemoteIntegeral.class, and Evaluatable.class on an HTTP server Verify that you can access the files through a browser 5. Start the RMI Registry Server> /somedirectory/rmiregistry Make sure that none of the class files are in the directory in which you started the registry or available through the classpath 6. Start the Server Server> java -Djava.rmi.server.codebase=http://webhost/rmi/ RemoteIntegralServer Server must be started on same host as rmiregistry 29

Enterprise Example: Compiling and Running the System, cont. 7. Start the Client Client> java -Djava.security.policy=rmiclient.policy RemoteIntegralClient2 rmihost Approximated with 10 steps: Integral from 0 to pi of sin(x)=2.0082484079079745 Approximated with 100 steps: Integral from 0 to pi of sin(x)=2.0000822490709877... The rmihost is where server in which the rmiregistry was started 30

An RMI Applet Applet does not require a RMI Security Manager Applet can only access server in which class files were loaded RMI Registry and remote object server must be the same HTTP host in which the applet was loaded RMI 1.1 stub protocol not properly supported in IE RMI 1.2 stub protocol require Java Plug-In or Netscape 6 31

RMI Applet, Example... import javax.swing.*; public class RemoteIntegralApplet extends JApplet implements ActionListener { private Evaluatable[] shapes; private RemoteIntegral remoteintegral; private JLabel result; private JTextField startinput, stopinput, stepinput; private JComboBox combo; public void init() { String host = getcodebase().gethost(); try { remoteintegral = (RemoteIntegral)Naming.lookup("rmi://" + host + "/RemoteIntegral"); catch(remoteexception re) { reporterror("remoteexception: " + re);... 32

RMI Applet, Example 33... public void actionperformed(actionevent event) { try { int steps = Integer.parseInt(stepInput.getText()); double start = Double.parseDouble(startInput.getText()); double stop = Double.parseDouble(stopInput.getText()); showstatus("calculating..."); Evaluatable shape = (Evaluatable)combo.getSelectedItem(); double area = remoteintegral.integrate(start, stop, steps, shape); result.settext(double.tostring(area)); showstatus(""); catch(numberformatexception nfe) { reporterror("bad input: " + nfe); catch(remoteexception re) { reporterror("remoteexception: " + re);

RMI Applet, Result Applet that communicates to a remote object through RMI in Netscape 6 34

Summary RMI is a pure Java-based protocol for communicating with remote objects Register (bind) and look-up remote objects in a registry Java 2 no longer requires the skeleton class needed with the RMI 1.1 protocol Enterprise RMI configuration requires a RMI Security Manager and client policy file for permissions 35

core Web programming Questions? 36 2001-2004 Marty Hall, Larry Brown http://