300 likes | 401 Views
Java Remote Method Invocation. java.rmi.* java.rmi.registry.* java.rmi.server.*. Local Procedure Calls. #include <...> int myFunc (stuff) int main ( ) { : : val = myFunc (stuff) : } int myFunc (stuff) { : }. Local Procedure Call Limitations.
E N D
JavaRemote Method Invocation java.rmi.* java.rmi.registry.* java.rmi.server.*
Local Procedure Calls #include <...> intmyFunc (stuff) int main ( ) { : : val = myFunc(stuff) : } intmyFunc(stuff) { : }
Local Procedure Call Limitations • Function is bound to the executing program • compiled (or linked) into executable code • If function is needed again, it is copied • Any function modifications must be made to all copies
Remove LP Limitations • Remove the static relationship between function and calling program. • Allow function to be dynamically associated with calling program • Windows dynamically linked library • More general approach is to use Remote Procedure Calls • Developed in 1980’s
Remote Procedure Approach • Allow function call to span processes or even machines. • Replace (extend) the function call process. • Develop a standard set of rules (interface) that allows any process to call any function that follows the interface rules • Automatically build interface files needed to connect caller to function • Method_Skel.class • Method_Stub.class
Remote Procedure Issues • Where is the function? • Addressing procedures • Registration procedures • How do we format the data? • Must be readable by all utilized platforms • Common definitions for data (integers, floats, etc.) • Common byte order • Common structure
Remote Method Invocation • Java to Java approach for distributed communication and computation • Establishes Method Registry • Uses existing data management mechanisms to control data flow • Object serialization
RMI Benefits • Supports Distributed processing • Function can be located anywhere... • Supports Shared use of functions • All users use same function, get same responses • Supports Dynamic implementation of function • Independent of calling program • Within limits (interface remains the same)
Serializing Data • Needed when objects must be passed • Serializable objects implement the serializable interface • java.io.serializable • Most common objects implement serializable • Custom objects may need to implement the serializable interface to allow then to be transported between client and server
Remote Method Process • Define remote method (RM) • Define interface between caller and RM • Make data serializable • Build remote method interface • Build remote method (server) • Build calling program (client) • Compile server, client, and interface • Start server, register service • Run calling program (client)
RMI Registry Identification • RMI registry at “well known port” 1099 • Remote Method registers with the registry • Stores data pairs of the form • (Remote Method name): protocol port #
Method Registration and Use 6 Remote method RMI client 5 4 2 Registry 3 1
RMI Example • Implement Fahrenheit to centigrade temperature conversion using a remote method. • Remote method takes a Fahrenheit temperature and returns the equivalent centigrade temperature.
Define Remote Method • centigrade = 5*(Fahrenheit - 32) /9
Define Remote Interface • Input: • temperature in Fahrenheit • Return: • temperature in Celsius • Specify both values as integers • computations are rounded • integers are serializable
Build RM Interface import java.rmi.*; public interface iTemp extends Remote { public int getTemp( int farTemp) throws RemoteException; }
Build Server import java.rmi.*; import java.rmi.registry.*; import java.rmi.server.*; public class TempConverter extends UnicastRemoteObject implements iTemp { public TempConverter() throws RemoteException {} public int getTemp( int farTemp) { return ((int)(5*(farTemp -32)/9)); }
Build Server public static void main(String args[]) { try { TempConverter tc = new TempConverter(); String servObjName = "///TempConverter"; Naming.rebind(servObjName, tc); System.err.println("TempConv is up!"); } catch (Exception e) { e.printStackTrace( ); } }//end of main }//end of TempConverter
Build Client import java.rmi.*; import java.io.*; public class TempUser { public static void main(String args[]) { DataInputStream dis = new DataInputStream (System.in); String sIn; int nuTemp; iTemp remoteTemp;
Build Client try { remoteTemp = (iTemp) Naming.lookup ("rmi://vudt-cotter.cstp.umkc.edu/TempConverter"); System.out.print("Enter temperature(Farenheit) "); System.out.flush(); sIn = dis.readLine(); nuTemp = remoteTemp.getTemp(Integer.parseInt(sIn)); System.out.println("That is "+ nuTemp + " degrees centigrade"); } catch (Exception e) { e.printStackTrace(); } } //end of main }// end of TempUser
Original Approach to Compile programs • Compile Server • javac TempConverter.java • Create Remote Method Stubs and clients • rmicTempConverter • Compile Client • (uses TempConverter_Stub.class) • javacTempUser.java
Current Approach to Compile programs • Compile Interface and create archive • javaciTemp.java • jar cvf iTemp.jar iTemp.class • Compile Server • javac TempConverter.java • (uses iTemp.jar) • Compile Client • javacTempUser.java • (uses iTemp.jar)
Register Service / Run Program • Start up RMI registry • rmiregistry • Start up Server • java TempConverter • Start up Client • java TempUser
RMI TempConverter Output Server Side... D:\data\cs423\java\tempRMI>start rmiregistry D:\data\cs423\java\tempRMI>java TempConverter TempConv is up! ^C D:\data\cs423\java\tempRMI> Client Side... D:\data\cs423\java\tempRMI>java TempUser Enter temperature(Farenheit)32 That is 0 degrees centigrade D:\data\cs423\java\tempRMI>
iMath.java import java.rmi.*; java.io.*; public interface iMath extends Remote { public int getSum( Numbers sum) throws RemoteException; public int getProduct( Numbers product) throws RemoteException; public class Numbers implements Serializable { private int num1; private int num2; private int num3; public Numbers() { } public void setN1(int num) { num1 = num; } public void setN2(int num) { num2 = num; } etc.
MathServer.java import java.rmi.*;import java.rmi.registry.*;import java.rmi.server.*; public class MathServer extends UnicastRemoteObject implements iMath { public MathServer() throws RemoteException {} public intgetSum( iMath.Numbers sum) { int n1, n2, n3; : return (nuSum); } public int getProduct( iMath.Numbers product) { int n1, n2, n3; : return (nuProd); }
MathServer.java public static void main(String args[]) { try { MathServer ms = new MathServer(); String servObjName = "///MathServer"; Naming.rebind(servObjName, ms); System.err.println("MathServer is up!"); } catch (Exception e){ e.printStackTrace(); } } }
MathUser.java import java.rmi.*; import java.io.*; public class MathUser { public static void main(String args[]) { BufferedReader dis= new BufferedReader(new InputStreamReader(System.in)); String sIn1, sIn2, sIn3; int add1, add2, add3,answer; iMath remoteAdder; iMath.Numbers nums = new iMath.Numbers(); try { remoteAdder = (iMath) Naming.lookup ("rmi://134.193.128.56/MathServer"); System.out.print("Enter First number (integers) "); System.out.flush(); sIn1 = dis.readLine(); System.out.print("Enter Second number (integers) "); System.out.flush(); sIn2 = dis.readLine();
MathUser.java System.out.print ("Enter Third number (integers) "); System.out.flush(); sIn3 = dis.readLine(); nums.setN1(Integer.parseInt(sIn1)); nums.setN2(Integer.parseInt(sIn2)); nums.setN3(Integer.parseInt(sIn3)); System.out.print("Do you want the Sum or Product (S or P)?"); sIn1 = dis.readLine(); if (sIn1.equals("S")) { answer = remoteAdder.getSum(nums); System.out.println("That sum is " + answer ); } else { answer = remoteAdder.getProduct(nums); System.out.println("That product is " + answer ); } } catch (Exception e) { e.printStackTrace(); } } }
Summary • Java RMI process similar to RPC • RMI Registry acts as Portmapper. Registry is located on serving host. • RMIC acts as remote method communications generator (RPCgen)