Lab10 Exercise 2 solution - Acronym Server using RMI
Acronym Server using RMI
The remote object (server) interface
import java.rmi.*; import java.rmi.server.*; public interface ITAcronymServer extends Remote { public String getFullForm(String acronym) throws RemoteException; }
The remote object (server) implementation
import java.rmi.*; import java.rmi.server.*; import java.net.*; import java.util.*; public class ITAcronymServerImpl extends UnicastRemoteObject implements ITAcronymServer { public ITAcronymServerImpl() throws RemoteException { this.initAcronymMap(); } public String getFullForm(String requestString) throws RemoteException { String fullForm = null; if (acronymMap.get(requestString) == null) fullForm = requestString + " - acronym not found"; else fullForm = requestString + " - " + acronymMap.get(requestString); return fullForm; } public static void main(String args[]){ try{ //create an instance of the remote object ITAcronymServerImpl acroServer = new ITAcronymServerImpl(); // register this remote object with the registry as AcronymServer Naming.bind("rmi://localhost:3000/AcronymServer", acroServer); }catch(AlreadyBoundException abex){ abex.printStackTrace(); }catch(RemoteException rex){ rex.printStackTrace(); }catch(MalformedURLException mex){ mex.printStackTrace(); } } private void initAcronymMap() { acronymMap.put("JVM", "Java Virtual Machine"); acronymMap.put("RMI", "Remote Method Invocation"); acronymMap.put("CAS", "Compare And Swap"); acronymMap.put("TCP", "Transmission Control Protocol"); } private Map acronymMap = new HashMap(); }
The client
import java.net.*; import java.rmi.*; public class AcronymClient { public static void main(String args[]) { try{ //get a reference to the remote object from the registry ITAcronymServer acroServer = (ITAcronymServer)Naming.lookup("rmi://localhost:3000/AcronymServer"); //Invoke the remote method on the remote object String fullForm = acroServer.getFullForm(args[0]); System.out.println(fullForm); }catch(NotBoundException nbex){ nbex.printStackTrace(); }catch(RemoteException rex){ rex.printStackTrace(); }catch(MalformedURLException mex){ mex.printStackTrace(); } } }
page revision: 1, last edited: 23 May 2011 10:27