80 likes | 231 Views
RMI – Remote Methods Invocation. CS 442 - LAB. Steps to develop RMI Application. Create server remote object interface . Create implementation class for the interface. Server Application(RMI Server): to create and register server remote object.
E N D
RMI – Remote Methods Invocation CS 442 - LAB
Steps to develop RMI Application • Create server remote object interface. • Createimplementation class for the interface. • Server Application(RMI Server): to create and register server remote object. • Client Application: to develop RMI client, and invoke methods on the remote object. NOTE: Application ( contains main )
Create server remote object interface. • serves as the contract between the server and its clients. • A server object interface must be public interface. • A server object interface must extend Remote interface. public interface ServerInterfaceextends Remote { public void service1(...) throwsRemoteException; // Other methods } // end interface ServerInterface SquCube.java packagermiPackage; importjava.rmi.Remote; importjava.rmi.RemoteException; publicinterfaceSquCubeextends Remote{ publicint square(int x) throwsRemoteException; publicint cube(int x) throwsRemoteException; }// end interface SquCube
Create implementation class for the interface. • The interface implementation class must: extend the UnicastRemoteObject class. • Define a class that implements the server object interface, as shown in the following outline:public class ServerInterfaceImplextendsUnicastRemoteObjectimplementsServerInterface { public void service1(...) throwsRemoteException { // Implement it } // end method service1 // Implement other methods } // end class ServerInterfaceImpl SquCubeImpl.java packagermiPackage; importjava.rmi.RemoteException; importjava.rmi.server.UnicastRemoteObject; publicclassSquCubeImplextendsUnicastRemoteObjectimplementsSquCube{ publicSquCubeImpl() throwsRemoteException { } publicint square(int x) throwsRemoteException { return x * x; }// end method publicint cube(int x) throwsRemoteException { return x * x * x; }// end method }// end class SquCubeImpl
3. Server Application to create and register server remote object(RMI server). Create a server object from the server implementation class and register it with an RMI registry: try{ ServerInterfaceImplserver = newServerInterfaceImpl(); //create registry on port for example 2213 Registry registry = LocateRegistry.createRegistry(2213);//change port on each run /* create a new service named nameOfService , and bind it with the remote object(server)*/ registry.rebind(“nameOfService", server); }//end try catch(Exception e){ System.out.println("Server not connected: " + e.toString()); }//end catch
SquCubeServer.java packagermiPackage; importrmiPackage.SquCubeImpl; // very important importjava.rmi.registry.Registry; importjava.rmi.registry.LocateRegistry; publicclassSquCubeServer { publicstaticvoid main(String[] args) { try{ SquCubeImpl server = newSquCubeImpl(); //create registry on port 5544 Registry registry = LocateRegistry.createRegistry(5544);//change port on each run System.out.println("RMI registry ready."); /* create a new service named SquareCubeService , and bind it with the remote object */ registry.rebind("SquareCubeService", server); }//end try catch(Exception e){ System.out.println("Server not connected: " + e.toString()); }//end catch System.out.println("Server is connected and ready for operation."); }// end method main }// end class SquCubeServer
4. Client Application: to develop RMI client. Develop a client that locates a remote object and invokes its methods, as shown in the following outline: try{ Registry myRegistry = LocateRegistry.getRegistry(); // search for SquareCubeService service ServerInterfacemyServer = (ServerInterface) myRegistry.lookup("RemoteObjectName"); // NOTE: myServer above is the remote object // call server's method myServer.service1(); }//end try catch(Exception e){ System.out.println("Client Exception: " + e.toString()); }//end catch
SquCubeClient.java packagermiPackage; importjava.rmi.registry.Registry; importjava.rmi.registry.LocateRegistry; importjava.util.Scanner; publicclassSquCubeClient { publicstaticvoid main(String[] args) { try{ Registry myRegistry = LocateRegistry.getRegistry(); // search for SquareCubeService service SquCubemyServer = (SquCube) myRegistry.lookup("SquareCubeService"); // NOTE: myServer above is the remote object Scanner input = new Scanner(System.in); int num; System.out.println("Enter a number : "); num = input.nextInt(); // call server's method int sq = myServer.square(num); int cube = myServer.cube(num); /////////////////////////////// System.out.println("The square of "+num+" = "+sq); System.out.println("The cube of "+num+" = "+cube); }//end try catch(Exception e){ System.out.println("Client Exception: " + e.toString()); }//end catch }// end method main }// end class SquCubeClient