Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Computing 6. RMI By Dr. Jiang B. Liu

Similar presentations


Presentation on theme: "Java Computing 6. RMI By Dr. Jiang B. Liu"— Presentation transcript:

1 Java Computing 6. RMI By Dr. Jiang B. Liu
 Java RMI Classes & Interfaces  Client/Server Development using RMI  Pizza Order application

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

3 Client/Server Development using Java RMI
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)

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

5 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());

6 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; } Notes: 1. PizzaServerInterface is a remote interface for client/server to specify the remote method prototypes. 2. The implementation of the remote method “orderPizza” is coded in PizzaServer class.

7 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

8 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

9 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);} if(remoteObj instanceof PizzaServerInterface) pizzaInterface = (PizzaServerInterface) remoteObj; }catch(Exception exc){…} PizzaOrder pizzaOrder=new PizzaOrder(pizzaInterface); pizzaOrder.show(); } }


Download ppt "Java Computing 6. RMI By Dr. Jiang B. Liu"

Similar presentations


Ads by Google