internet software development remote method invocation paul krause

25
Internet Software Internet Software Development Development Remote Method Invocation Remote Method Invocation Paul Krause Paul Krause

Post on 22-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Internet Software Development Remote Method Invocation Paul Krause

Internet Software Internet Software DevelopmentDevelopment

Remote Method InvocationRemote Method Invocation

Paul KrausePaul Krause

Page 2: Internet Software Development Remote Method Invocation Paul Krause

Lecture 10 - RMILecture 10 - RMI

Introduction to RMIIntroduction to RMI Simple Example - “DivideServer”Simple Example - “DivideServer” Demo of this exampleDemo of this example Review a more complex example - Review a more complex example -

“StudentEnrollment”“StudentEnrollment”

Page 3: Internet Software Development Remote Method Invocation Paul Krause

What is RMI?What is RMI?

A high-level networking technology for A high-level networking technology for distributed Java applicationsdistributed Java applications

Allows a Java object executing on one Allows a Java object executing on one machine to remotely invoke methods in an machine to remotely invoke methods in an object executing on a second machineobject executing on a second machine

Such method calls have the appearance of Such method calls have the appearance of operating on objects in the same programoperating on objects in the same program

Page 4: Internet Software Development Remote Method Invocation Paul Krause

Skeletons and StubsSkeletons and Stubs

Client

Stub

Java Virtual Machine and RMI System

Client Machine

C Server

Skeleton

Java Virtual Machine and RMI System

Server Machine

S

Page 5: Internet Software Development Remote Method Invocation Paul Krause

StubStub

Presents same interfaces as remote objectPresents same interfaces as remote object Works with JVM and RMI to serialise Works with JVM and RMI to serialise

arguments in the remote method callarguments in the remote method call Receives results from the remote method call Receives results from the remote method call

and returns them to the client objectand returns them to the client object

Page 6: Internet Software Development Remote Method Invocation Paul Krause

SkeletonSkeleton

Receives the remote method call and Receives the remote method call and associated argumentsassociated arguments

Works with JVM and RMI to deserialize Works with JVM and RMI to deserialize arguments in the remote method callarguments in the remote method call

Invokes appropriate methods in the remote Invokes appropriate methods in the remote objectobject

Receives return value (if any) from the method Receives return value (if any) from the method call, serializes it and returns it to the clientcall, serializes it and returns it to the client

No longer needed in Java 2No longer needed in Java 2

Page 7: Internet Software Development Remote Method Invocation Paul Krause

Referencing remote objectsReferencing remote objects

A A RegistryRegistry object maintains a mapping from Server object maintains a mapping from Server names to remote object referencesnames to remote object references We will use the example “DivideServer”We will use the example “DivideServer”

The remote object is named via a URL using the RMI The remote object is named via a URL using the RMI protocol:protocol: rmi://rmi://host:porthost:port//serverserver hosthost - IP address or name of server machine - IP address or name of server machine portport - optional port number of the Registry on above - optional port number of the Registry on above serverserver - name of the remote server - name of the remote server e.g. rmi://127.0.0.1/DivideServere.g. rmi://127.0.0.1/DivideServer

Page 8: Internet Software Development Remote Method Invocation Paul Krause

Key Classes and InterfacesKey Classes and Interfaces

RemoteRemote Interface Interface NamingNaming Class Class RemoteObjectRemoteObject Class Class RemoteServerRemoteServer Class Class UnicastRemoteObjectUnicastRemoteObject Class Class RemoteExceptionRemoteException Class Class

Page 9: Internet Software Development Remote Method Invocation Paul Krause

RemoteRemote Interface Interface

In the In the java.rmijava.rmi package package Contains no constants or methodsContains no constants or methods It is used to designate which interfaces are It is used to designate which interfaces are

remote -remote - remote interfaces must extend remote interfaces must extend java.rmi.Remotejava.rmi.Remote remote objects must implement a remote interfaceremote objects must implement a remote interface

Page 10: Internet Software Development Remote Method Invocation Paul Krause

NamingNaming Class Class

In the In the java.rmijava.rmi package package Three methods to Three methods to associateassociate names with names with

remote objects (used at the Server side):remote objects (used at the Server side): static void bind(String static void bind(String namename, Remote , Remote robjrobj)) static void rebind(String static void rebind(String namename, Remote , Remote robjrobj)) static void unbind(String static void unbind(String namename))

One method to One method to obtainobtain a name for a remote a name for a remote object (used at the Client side):object (used at the Client side): static Remote lookup(String static Remote lookup(String urlurl))

Page 11: Internet Software Development Remote Method Invocation Paul Krause

Remote ObjectsRemote Objects

RemoteObjectRemoteObject extends java.lang.Object to provide correct behaviour for extends java.lang.Object to provide correct behaviour for

remote objectsremote objects

RemoteServerRemoteServer an abstract class that extends an abstract class that extends RemoteObjectRemoteObject. Defines the . Defines the

methods needed to create and export remote objectsmethods needed to create and export remote objects

UnicastRemoteObjectUnicastRemoteObject a concrete subclass of a concrete subclass of RemoteServerRemoteServer. Extend this class to . Extend this class to

make a remote objectmake a remote object

Page 12: Internet Software Development Remote Method Invocation Paul Krause

Simple Client/Server ApplicationSimple Client/Server Application

Client program takes three arguments:Client program takes three arguments: IP address or name of remote serverIP address or name of remote server two numberstwo numbers

The two numbers are passed to the serverThe two numbers are passed to the server The server divides one into the other and The server divides one into the other and

returns the resultreturns the result From: From: JavaBeans Programming from the JavaBeans Programming from the

Ground UpGround Up, Joseph O’Neil and Herb Schildt, Joseph O’Neil and Herb Schildt

Page 13: Internet Software Development Remote Method Invocation Paul Krause

DivideServer.javaDivideServer.java

import java.rmi.*;import java.rmi.*;

public interface DivideServer extends Remote {public interface DivideServer extends Remote {

double divide(double d1, double d2) throwsdouble divide(double d1, double d2) throws

RemoteException;RemoteException;

}}

Page 14: Internet Software Development Remote Method Invocation Paul Krause

Remote InterfacesRemote Interfaces

The remote interface must extend The remote interface must extend RemoteRemote Remote methods can throw a Remote methods can throw a RemoteExceptionRemoteException We now require a server that implements the We now require a server that implements the

interfaceinterface By convention, we name the implementation of By convention, we name the implementation of

DivideServerDivideServer DivideServerImplDivideServerImpl

Remote objects must extend Remote objects must extend UnicastRemoteObjectUnicastRemoteObject

Page 15: Internet Software Development Remote Method Invocation Paul Krause

DivideServerImpl.javaDivideServerImpl.java

import java.rmi.*;import java.rmi.*;import java.rmi.server.*;import java.rmi.server.*;

public class DivideServerImplpublic class DivideServerImplextends UnicastRemoteObject implements DivideServer {extends UnicastRemoteObject implements DivideServer {

public DivideServerImpl( )public DivideServerImpl( )throws RemoteException { throws RemoteException { }}

public double divide(double d1, double d2)public double divide(double d1, double d2)throws RemoteException {throws RemoteException {return d1/d2;return d1/d2;

}}

}}

Page 16: Internet Software Development Remote Method Invocation Paul Krause

DivideServerApp.javaDivideServerApp.java

This is the main program for the server sideThis is the main program for the server side Its primary function is to bind the name Its primary function is to bind the name

“DivideServer” to an instance of “DivideServer” to an instance of DivideServerImplDivideServerImpl

Note that we need to start this server program Note that we need to start this server program running running beforebefore the client program is invoked the client program is invoked

Page 17: Internet Software Development Remote Method Invocation Paul Krause

DivideServerApp.javaDivideServerApp.java

Core part is:Core part is:

DivideServerImpl divideServerImpl;DivideServerImpl divideServerImpl;

divideServerImpl = new DivideServerImpl();divideServerImpl = new DivideServerImpl();

Naming.rebind("DivideServer", Naming.rebind("DivideServer", divideServerImpl);divideServerImpl);

Page 18: Internet Software Development Remote Method Invocation Paul Krause

DivideServerApp.javaDivideServerApp.java

import java.net.*;import java.net.*;import java.rmi.*;import java.rmi.*;public class DivideServerApp {public class DivideServerApp {

public static void main(String args[]) {public static void main(String args[]) { try {try {

DivideServerImpl divideServerImpl;DivideServerImpl divideServerImpl;divideServerImpl = new DivideServerImpl();divideServerImpl = new DivideServerImpl();Naming.rebind("DivideServer", divideServerImpl);Naming.rebind("DivideServer", divideServerImpl);

}} catch(Exception ex) {catch(Exception ex) {

ex.printStackTrace();ex.printStackTrace(); }}}}

}}

Page 19: Internet Software Development Remote Method Invocation Paul Krause

DivideCleint.javaDivideCleint.java

Looks up the object that is bound to the RMI Looks up the object that is bound to the RMI URLURL

The lookup method of the The lookup method of the NamingNaming class class returns an object of type returns an object of type RemoteRemote

This must then be cast to a This must then be cast to a DivideServerDivideServer in in order to be able to access the order to be able to access the dividedivide method method

Page 20: Internet Software Development Remote Method Invocation Paul Krause

DivideCleint.javaDivideCleint.java

DivideServer divideServer;DivideServer divideServer;

divideServer = divideServer = (DivideServer)Naming.lookup(divideServerURL);(DivideServer)Naming.lookup(divideServerURL);

// Invoke remote method and display results// Invoke remote method and display results

double result = divideServer.divide(d1, d2);double result = divideServer.divide(d1, d2);

System.out.println("The result is: " + result);System.out.println("The result is: " + result);

Page 21: Internet Software Development Remote Method Invocation Paul Krause

DivideCleint.javaDivideCleint.java

import java.rmi.*;import java.rmi.*;public class DivideClient {public class DivideClient {

public static void main(String args[]) {public static void main(String args[]) { try {try {

// Make rmi URL to name DivideServer// Make rmi URL to name DivideServerString divideServerURL;String divideServerURL;divideServerURL = "rmi://" + args[0] + "/DivideServer";divideServerURL = "rmi://" + args[0] + "/DivideServer";

// Obtain reference to that remote object// Obtain reference to that remote objectDivideServer divideServer;DivideServer divideServer;divideServer = (DivideServer)Naming.lookup(divideServerURL);divideServer = (DivideServer)Naming.lookup(divideServerURL);

Page 22: Internet Software Development Remote Method Invocation Paul Krause

DivideCleint.javaDivideCleint.java// Display numbers// Display numbersSystem.out.println("The first number is: " + args[1]);System.out.println("The first number is: " + args[1]);double d1 = Double.valueOf(args[1]).doubleValue();double d1 = Double.valueOf(args[1]).doubleValue();System.out.println("The second number is: " + args[2]);System.out.println("The second number is: " + args[2]);double d2 = Double.valueOf(args[2]).doubleValue();double d2 = Double.valueOf(args[2]).doubleValue();

// Invoke remote method and display results// Invoke remote method and display resultsdouble result = divideServer.divide(d1, d2);double result = divideServer.divide(d1, d2);System.out.println("The result is: " + result);System.out.println("The result is: " + result);

}}

catch(Exception ex) {catch(Exception ex) {ex.printStackTrace();ex.printStackTrace();

}}}}

}}

Page 23: Internet Software Development Remote Method Invocation Paul Krause

Executing the ApplicationExecuting the Application

Use javac to compile all filesUse javac to compile all files Open a command prompt and change to the Open a command prompt and change to the

working directoryworking directory Use the Use the rmicrmic compiler to produce a stub class compiler to produce a stub class

for the remote server class:for the remote server class: rmic -v1.2 DivideServerImplrmic -v1.2 DivideServerImpl

Copy all files to the client machineCopy all files to the client machine exercise: You don’t need all of them on the client. exercise: You don’t need all of them on the client.

Which ones do you needWhich ones do you need

Page 24: Internet Software Development Remote Method Invocation Paul Krause

Executing the ApplicationExecuting the Application

open a command prompt on the server and start the open a command prompt on the server and start the RMI registry:RMI registry: rmiregistryrmiregistry

open a second command prompt on the server open a second command prompt on the server machine and run the server application to bind the machine and run the server application to bind the remote server object to the registryremote server object to the registry java DivideAppjava DivideApp

Start the client (again, use a command prompt):Start the client (again, use a command prompt): java DivideClient server1 8 2java DivideClient server1 8 2

Page 25: Internet Software Development Remote Method Invocation Paul Krause

The resultThe result

You should get the following output at the client’s You should get the following output at the client’s command prompt:command prompt:

The first number is: 8The first number is: 8The second number is: 2The second number is: 2The result is: 4.0The result is: 4.0 Notes:Notes:

Use IP address 127.0.0.1 if experimenting on a single Use IP address 127.0.0.1 if experimenting on a single machinemachine

On Windows 2000 you can find out the IP address of a On Windows 2000 you can find out the IP address of a machine using machine using hostnamehostname and and ipconfig/allipconfig/all