90 likes | 196 Views
Java Computing. By Dr. Jiang B. Liu. 6. RMI Java RMI Classes & Interfaces Client/Server Development using RMI Pizza Order application. Java RMI Classes and Interfaces.
E N D
Java Computing By Dr. Jiang B. Liu 6. RMI Java RMI Classes & Interfaces Client/Server Development using RMI Pizza Order application
Java RMI Classes and Interfaces • Remote interface: The RMI system is based on remote interfaces through which a client accesses the methods of a remote object. • server.UnicastRemoteObject class: the only RemoteServer subclass server object support by RMI at this time. • Naming class: provide methods (bind/rebind, lookup) for server classes to make remote objects visible to clients.
Server Client/Server Developmentusing Java RMI Client Create RMI Java server and server interface Create RMI client Compile RMI server (javac server.java) Compile RMI client (javac client.java) RMI compile the binary server (rmic server) Check the stub file and interface file Copy the stub file and server interface file to the client Run the client (Set SecurityManager lookup server object invoke the remote methods.) Start the rmi registry Run the server (Set SecurityManager Bind server object to the registry)
The ”Pizza Order" Application(Server: PizzaServer class) import java.awt.*; import java.rmi.*; import java.rmi.server.*; public class PizzaServer extends UnicastRemoteObject implements PizzaServerInterface { PizzaServer() throws RemoteException { super(); } public String orderPizza(String name,String address,String phone, int size, String toppings,String pizzatype ) throws RemoteException { // Get pizza order infor ... }
The ”Pizza Order" Application(Server: PizzaServer Class) public static void main(String args[]) { try { // set the security manager System.setSecurityManager(new RMISecurityManager()); // create the local instance of the PizzaServer PizzaServer svr = new PizzaServer(); // put the local instance into the Naming Server Naming.rebind("rmi://myhost/PizzaServer", svr); System.out.println("Debug: Pizza Server is ready..."); } catch(Exception exc) { System.out.println("Error! - " + exc.toString()); } } }
The ”Pizza Order" Application(Server: PizzaServerInterface Interface) import java.rmi.*; public interface PizzaServerInterface extends Remote { String orderPizza(String name,String address, String phone,int size,String toppings, String pizzatype ) throws RemoteException; }
The ”Pizza Order" Application(Client: PizzaOrder Class) import java.awt.*; import java.rmi.*; import java.rmi.server.*; public class PizzaOrder extends Frame { // AWT GUI Commonets ... PizzaServerInterface pizzaServerIf; public PizzaOrder(PizzaServerInterface pif) { // initialize the application frame super("Pizza Pizza!"); setLayout(null); resize(400, 480); // assign the pizza server interface pizzaServerIf = pif; // set up the UI ...
The ”Pizza Order" Application(Client: PizzaOrder Class) public boolean action(Event evt,Object obj) { if(evt.target == sendButton){ // get customer order ... try{ totalString = pizzaServerIf.orderPizza( name, address, phone, size, toppings, pizzatype); } catch(Exception exc){ System.out.println("Error - " + exc.toString()); } // display final results … }
The ”Pizza Order" Application(Client: PizzaOrder Class) public static void main(String args[]){ Remote remoteObj = null; PizzaServerInterface pizzaInterface = null; System.setSecurityManager( new RMISecurityManager()); try{ remoteObj = Naming.lookup("rmi://myhost/PizzaServer"); }catch(Exception exc){ System.out.println(exc.toString())System.exit(1);} try{ if(remoteObj instanceof PizzaServerInterface) pizzaInterface = (PizzaServerInterface) remoteObj; }catch(Exception exc){…} PizzaOrder pizzaOrder=new PizzaOrder(pizzaInterface); pizzaOrder.show(); } }