11.07.2015 Views

jGuru: Remote Method Invocation (RMI) - Dse.disco.unimib.it

jGuru: Remote Method Invocation (RMI) - Dse.disco.unimib.it

jGuru: Remote Method Invocation (RMI) - Dse.disco.unimib.it

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>jGuru</strong>: <strong>Remote</strong> <strong>Method</strong> <strong>Invocation</strong> (<strong>RMI</strong>)Pagina 6The Java 2 platform version of rmic add three new options:-v1.1 Create stubs/skeletonsfor JDK 1.1 stubprotocol version-vcompat (default)Create stubs/skeletons compatiblew<strong>it</strong>h both JDK 1.1 and Java 2stub protocol versions-v1.2 Create stubs for Java 2 stub protocolversion only4. Host Server<strong>Remote</strong> <strong>RMI</strong> services must be hosted in a server process. The class CalculatorServer is a very simple server that provides the bare essentials for hosting.import java.rmi.Naming;public class CalculatorServer {public CalculatorServer() {try {Calculator c = new CalculatorImpl();Naming.rebind("rmi://localhost:1099/CalculatorService", c);} catch (Exception e) {System.out.println("Trouble: " + e);}}public static void main(String args[]) {new CalculatorServer();}}5. ClientThe source code for the client follows:Back to Topimport java.rmi.Naming;import java.rmi.<strong>Remote</strong>Exception;import java.net.MalformedURLException;import java.rmi.NotBoundException;public class CalculatorClient {public static void main(String[] args) {try {Calculator c = (Calculator)Naming.lookup("rmi://localhost/CalculatorService");System.out.println( c.sub(4, 3) );System.out.println( c.add(4, 5) );System.out.println( c.mul(3, 6) );System.out.println( c.div(9, 3) );}catch (MalformedURLException murle) {System.out.println();System.out.println("MalformedURLException");System.out.println(murle);}catch (<strong>Remote</strong>Exception re) {System.out.println();System.out.println("<strong>Remote</strong>Exception");System.out.println(re);}catch (NotBoundException nbe) {System.out.println();System.out.println("NotBoundException");System.out.println(nbe);}catch (java.lang.Ar<strong>it</strong>hmeticExceptionae) {System.out.println();System.out.println("java.lang.Ar<strong>it</strong>hmeticException");http://java.sun.com/developer/onlineTraining/rmi/<strong>RMI</strong>.html11/04/2006 16.47.37

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!