RMI Notes

2 create stubs for java 2 stub protocol version only

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: .1 stub protocol version -vcompat (default) Create stubs/skeletons compatible with both JDK 1.1 and Java 2 stub protocol versions -v1.2 Create stubs for Java 2 stub protocol version only 4. Host Server Remote RMI 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) { 10 Manisha Akolkar INTERNATIONAL INSTITUTE OF INFORMATION TECHNOLOGY P-14, Pune Infotech Park, Hinjaw adi, Pune – 411 027 System.out.println("Trouble: " + e); } } public static void main(String args) { new CalculatorServer(); } } 5. Client The source code for the client follows: import java.rmi.Naming; import java.rmi.RemoteException; 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 (RemoteException re) { System.out.println(); System.out.println( "RemoteException"); System.out.println(re); } catch (NotBoundException nbe) { System.out.println(); System.out.println( "NotBoundException"); System.out.println(nbe); } catch ( java.lang.ArithmeticException ae) { System.out.println(); System.out.println( 11 Manisha Akolkar INTERNATIONAL INSTITUTE OF INFORMATION TECHNOLOGY P-14, Pune Infotech Park, Hinjaw adi, Pune – 411 027 "java.lang.ArithmeticException"); System.out.println(ae); } } } 6. Running the RMI System You are now ready to run the system! You need to start three consoles, one for the server, one for the client, and one for the RMIRegistry. Start with the Registry. You must be in the directory that contains the classes you have written. From there, enter the following: rmiregistry If all goes well, the registry will start running and you can switch to the next console. In the second console start the server hosting the CalculatorService, and enter the following: >java CalculatorServer It will start, load the implementation into memory and wait for a client connection. In the last console, start the client program. >java CalculatorClient If all goes well you will see the following output: 1 9 18 3 That's it; you have created a working RMI system. Even though you ran the three consoles on the same computer, RMI uses your network stack and TCP/IP to communicate between the three separate JVMs. This is a full-fledged RMI system. Parameters in RMI You have seen that RMI supports method calls to rem...
View Full Document

This note was uploaded on 07/15/2011 for the course ECO 2023 taught by Professor Mr.raza during the Summer '10 term at FAU.

Ask a homework question - tutors are online