APJI-Lab7-RMI Application Programming I Module – RMI Lab Guide for Session Session Objectives In this session, you will be practicing with RMI classes and interface Remote, RemoteException, Registry, LocateRegistry, UnicastRemoteObject How to create an rmi application and client Part – Getting started (30 minutes) Create an rmi server application and client (10 minutes) Server application stores some products and each one sends a message like "I am a microwave Buy me!" to client if it requested Client application lookup products on server and request theirs descriptions and show to screem Scan the code first, type the code, compile, run and observe the result - Step 1: create interface of server component: Product.java import java.rmi.*; public interface Product extends Remote { String getDescription() throws RemoteException; } - Step : create a server component that implements above interface: ProductImpl.java import java.rmi.*; import java.rmi.server.*; public class ProductImpl extends UnicastRemoteObject implements Product { public ProductImpl(String n) throws RemoteException { name = n; } public String getDescription() throws RemoteException { return "I am a " + name + " Buy me!"; } © 2009 FPT-Aptech Page / APJI-Lab7-RMI private String name; } Step 3: create an application at server side that loads server component into memory: ProductServer.java import java.rmi.*; import java.rmi.server.*; import sun.applet.*; public class ProductServer { public static void main(String args[]) { try { System.out.println ("Constructing server implementations "); ProductImpl p1 = new ProductImpl("Blackwell Toaster"); ProductImpl p2 = new ProductImpl("ZapXpress Microwave Oven"); System.out.println ("Binding server implementations to registry "); Registry r = LocateRegistry.createRegistry(4000); Naming.rebind("toaster", p1); Naming.rebind("microwave", p2); System.out.println ("Waiting for invocations from clients "); } catch(Exception e) { System.out.println("Error: " + e); } } } - Step 4: Create an application at client side: ProductClient.java import java.rmi.*; import java.rmi.server.*; public class ProductClient { public static void main(String[] args) { System.setSecurityManager(new RMISecurityManager()); String url = "rmi://localhost:4000/"; // change to "rmi://yourserver.com/" © 2009 FPT-Aptech Page / APJI-Lab7-RMI // when server runs on remote machine // yourserver.com try { Product c1 = (Product)Naming.lookup(url + "toaster"); Product c2 = (Product)Naming.lookup(url + "microwave"); System.out.println(c1.getDescription()); System.out.println(c2.getDescription()); } catch(Exception e) { System.out.println("Error " + e); } System.exit(0); } } - Step 5: Run ProductServer at server side - Step 6: Run ProductClient at client side Part – Workshops (30 minutes) • • Quickly look at workshops for Module for reviewing basic steps Try to compile, run and observe the output of sample code provided for related workshop Discuss with your class-mate and your instructor if needed Part – Lab Assignment (60 minutes) Do the assignment for Module carefully Discuss with your class-mates and your instructor if needed Part –Do it yourself Write an application like dictionary online All words and theirs meaning are stored on server application Client application can lookup for a word, add or update a new word of server © 2009 FPT-Aptech Page /