Using RMI The Example of A Remote Calculator
















- Slides: 16

Using RMI -The Example of A Remote Calculator Dr M. E. AYDIN / Dr M. CONRAD 1

Parts of A Working RMI System • A working RMI system is composed of several parts. – Interface definitions for the remote services Implementations of the remote services – Stub and Skeleton files – A server to host the remote services – An RMI Naming service that allows clients to find the remote services – A class file provider (an HTTP or FTP server) – A client program that needs the remote services Dr M. E. AYDIN / Dr M. CONRAD 2

Steps to Build An RMI System • Assuming that the RMI system is already designed, you take the following steps to build a system: 1. Write and compile Java code for interfaces 2. Write and compile Java code for implementation classes 3. Generate Stub and Skeleton class files from the implementation classes 4. Write Java code for a remote service host program 5. Develop Java code for RMI client program 6. Install and run RMI system • Aren’t the steps similar to build an RPC system? Dr M. E. AYDIN / Dr M. CONRAD 3

Step 1. Interfaces • The first step is to write and compile the Java code for the service interface. The Calculator interface defines all of the remote features offered by the service: public interface Calculator extends java. rmi. Remote { public long add(long a, long b) throws java. rmi. Remote. Exception; public long sub(long a, long b) throws java. rmi. Remote. Exception; public long mul(long a, long b) throws java. rmi. Remote. Exception; public long div(long a, long b) throws java. rmi. Remote. Exception; } Dr M. E. AYDIN / Dr M. CONRAD 4

Step 1. Interfaces • Notice this interface extends Remote, and each method signature declares that it may throw a Remote. Exception object. • Copy this file to your directory and compile it with the Java compiler: u: /cis 69/rmi>javac Calculator. java Dr M. E. AYDIN / Dr M. CONRAD 5

Step 2. Implementation of The Interface • Implementations must have an explicit constructor in order to declare the Remote. Exception exception • Next, you write the implementation for the remote service. This is the Calculator. Impl class: public class Calculator. Impl extends java. rmi. server. Unicast. Remote. Object implements Calculator { public Calculator. Impl() throws java. rmi. Remote. Exception { super(); } Dr M. E. AYDIN / Dr M. CONRAD 6

Step 2. Implementation of The Interface public long add(long a, long b) throws java. rmi. Remote. Exception return a + b; } public long sub(long a, long b) throws java. rmi. Remote. Exception return a - b; } public long mul(long a, long b) throws java. rmi. Remote. Exception return a * b; } public long div(long a, long b) throws java. rmi. Remote. Exception return a / b; } { { } Dr M. E. AYDIN / Dr M. CONRAD 7

Step 2. Implementation of The Interface • Again, copy this code into your directory and compile it. • The implementation class uses Unicast. Remote. Object to link into the RMI system. • When a class extends Unicast. Remote. Object, it must provide a constructor that declares that it may throw a Remote. Exception object. When this constructor calls super(), it activates code in Unicast. Remote. Object that performs the RMI linking and remote object initialization. Dr M. E. AYDIN / Dr M. CONRAD 8

Step 3: Stubs and Skeletons • You next use the RMI compiler, rmic, to generate the stub and skeleton files. The compiler runs on the remote service implementation class file. u: /cis 69/rmi> rmic Calculator. Impl • Try this in your directory. After you run rmic you should find the file Calculator_Stub. class and Calculator_Skel. class. Dr M. E. AYDIN / Dr M. CONRAD 9

Step 4: Write Host Server Program • Remote RMI services must be hosted in a server process. The class Calculator. Server is a very simple server that provides the bare essentials for hosting. Dr M. E. AYDIN / Dr M. CONRAD 10

Step 4: Write Host Server Program import java. rmi. registry. *; public class Calculator. Server { public Calculator. Server() { try { Calculator c = new Calculator. Impl(); Registry reg = Locate. Registry. create. Registry(1099); reg. rebind("Calculator. Service", c); System. out. println( } catch (Exception e) { System. out. println("Trouble: " + e); } } public static void main(String args[]) { new Calculator. Server(); } } Dr M. E. AYDIN / Dr M. CONRAD 11

Step 5: Write Client Program import java. rmi. Naming; java. rmi. Remote. Exception; java. net. Malformed. URLException; java. rmi. Not. Bound. Exception; public class Calculator. Client { public static void main(String[] args) { try { Calculator c = (Calculator) Naming. lookup( "rmi: //localhost /Calculator. Service"); 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) ); } Dr M. E. AYDIN / Dr M. CONRAD 12

Step 5: Write Client Program catch (Malformed. URLException murle) { System. out. println(); System. out. println( "Malformed. URLException"); System. out. println(murle); } catch (Remote. Exception re) { System. out. println(); System. out. println( "Remote. Exception"); System. out. println(re); } catch (Not. Bound. Exception nbe) { System. out. println(); System. out. println( "Not. Bound. Exception"); System. out. println(nbe); } Dr M. E. AYDIN / Dr M. CONRAD 13

Step 5: Write Client Program catch(java. lang. Arithmetic. Exception ae) { System. out. println(); System. out. println( "java. lang. Arithmetic. Exception"); System. out. println(ae); } } } Dr M. E. AYDIN / Dr M. CONRAD 14

Step 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: u: /cis 69/rmi> 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 Calculator. Service, and enter the following: u: /cis 69/rmi> java Calculator. Server Dr M. E. AYDIN / Dr M. CONRAD 15

Step 6: Running the RMI System • It will start, load the implementation into memory and wait for a client connection. • In the last console, start the client program. u: /cis 69/rmi> java Calculator. Client • 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. Dr M. E. AYDIN / Dr M. CONRAD 16