1 / 8

RMI – Remote Methods Invocation

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.

cormac
Download Presentation

RMI – Remote Methods Invocation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. RMI – Remote Methods Invocation CS 442 - LAB

  2. 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 )

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

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

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

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

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

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

More Related