RMI Remote Methods Invocation CS 442 LAB Steps
RMI – Remote Methods Invocation CS 442 - LAB
Steps to develop RMI Application 1. Create server remote object interface. 2. Create implementation class for the interface. 3. Server Application(RMI Server): to create and register server remote object. 4. Client Application: to develop RMI client, and invoke methods on the remote object. NOTE: Application ( contains main )
1. 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 Server. Interface extends Remote { public void service 1(. . . ) throws Remote. Exception; // Other methods } // end interface Server. Interface Squ. Cube. java package rmi. Package; import java. rmi. Remote. Exception; public interface Squ. Cube extends Remote{ public int square(int x) throws Remote. Exception; public int cube(int x) throws Remote. Exception; }// end interface Squ. Cube
2. • • Create implementation class for the interface. The interface implementation class must: extend the Unicast. Remote. Object class. Define a class that implements the server object interface, as shown in the following outline: public class Server. Interface. Impl extends Unicast. Remote. Object implements Server. Interface { public void service 1(. . . ) throws Remote. Exception { // Implement it } // end method service 1 // Implement other methods } // end class Server. Interface. Impl Squ. Cube. Impl. java package rmi. Package; import java. rmi. Remote. Exception; import java. rmi. server. Unicast. Remote. Object; public class Squ. Cube. Impl extends Unicast. Remote. Object implements Squ. Cube{ public Squ. Cube. Impl() throws Remote. Exception { } public int square(int x) throws Remote. Exception { return x * x; }// end method public int cube(int x) throws Remote. Exception { return x * x; }// end method }// end class Squ. Cube. Impl
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{ Server. Interface. Impl server = new Server. Interface. Impl(); //create registry on port for example 2213 Registry registry = Locate. Registry. create. Registry(2213); //change port on each run /* create a new service named name. Of. Service , and bind it with the remote object(server)*/ registry. rebind(“name. Of. Service", server); }//end try catch(Exception e){ System. out. println("Server not connected: " + e. to. String()); }//end catch
Squ. Cube. Server. java package rmi. Package; import rmi. Package. Squ. Cube. Impl; // very important import java. rmi. registry. Registry; import java. rmi. registry. Locate. Registry; public class Squ. Cube. Server { public static void main(String[] args) { try{ Squ. Cube. Impl server = new Squ. Cube. Impl(); //create registry on port 5544 Registry registry = Locate. Registry. create. Registry(5544); //change port on each run System. out. println("RMI registry ready. "); /* create a new service named Square. Cube. Service , and bind it with the remote object */ registry. rebind("Square. Cube. Service", server); }//end try catch(Exception e){ System. out. println("Server not connected: " + e. to. String()); }//end catch System. out. println("Server is connected and ready for operation. "); }// end method main }// end class Squ. Cube. Server
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 my. Registry = Locate. Registry. get. Registry(); // search for Square. Cube. Service service Server. Interface my. Server = (Server. Interface ) my. Registry. lookup("Remote. Object. Name"); // NOTE: my. Server above is the remote object // call server's method my. Server. service 1(); }//end try catch(Exception e){ System. out. println("Client Exception: " + e. to. String()); }//end catch
Squ. Cube. Client. java package rmi. Package; import java. rmi. registry. Registry; import java. rmi. registry. Locate. Registry; import java. util. Scanner; public class Squ. Cube. Client { public static void main(String[] args) { try{ Registry my. Registry = Locate. Registry. get. Registry(); // search for Square. Cube. Service service Squ. Cube my. Server = (Squ. Cube) my. Registry. lookup("Square. Cube. Service"); // NOTE: my. Server above is the remote object Scanner input = new Scanner(System. in); int num; System. out. println("Enter a number : "); num = input. next. Int(); // call server's method int sq = my. Server. square(num); int cube = my. Server. 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. to. String()); }//end catch }// end method main }// end class Squ. Cube. Client
- Slides: 8