20-CS-694 Advanced Programming Techniques Spring 2012

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

     Previous     Next      All lectures

Remote Method Invocation

It is possible for clients to remotely add class servers to an rmi registry. This is accomplished by means of a meta server that receives the name of the server to register, the binding name, constructor argument types, and specific arguments from the client. The meta server registers the named server class, creating an instance with the passed arguments. This allows the same server class to be used in infinitely many ways. Therefore, free-form binding names are supported.

The following is an example. The meta server is given in Server.java. Classes it serves in this example are SuccessorImpl, TimesImpl, MergeImpl, and HammingImpl. All Impl classes implement the Stream interface, which requires implementing a next() method, and use an object of class Monitor to implement the putIt() and next() methods. Using the Monitor class is necessary because it is not possible to extend from an abstract class as before (recall the old "Stream" class) since it is required for all Impl classes to extend the UnicastRemoteObject class. Supplying the Thread for use with putIt() and next() methods is a bit different from normal: the Impl classes implement the Runnable interface but the runnable Thread itself is supplied by the Monitor class (a little easier on the user's eyes this way).

An object of any one of the Impl classes delivers tokens from a possibly infinite stream of tokens, one at a time, when requested using next(). Particular streams are specified using constructor arguments. The following table explains what services the above classes provide and what arguments the constructors take.

Class Constructor Description
SuccessorImpl SuccessorImpl(String n)
e.g. Stream a = new SuccessorImpl("1");
Serves tokens from the stream 1, 2, 3, ... (that is, n is ignored).
TimesImpl TimesImpl(String n, Stream s)
e.g. Stream a = new TimesImpl("3", stream);
Assumes Stream s serves BigInteger tokens. Multiplies each token by the BigInteger n.
MergeImpl MergeImpl(Stream s1, Stream s2)
e.g. Stream a = new MergeImpl(stream1,stream2);
Assumes Stream s1 and Stream s2 serve BigInteger tokens in increasing order. Produces a single stream containing all tokens from s1 and s2 in increasing order.
HammingImpl HammingImpl(BigInteger p[], BigInteger m)
e.g. Stream a = new HammingImpl(primes, b);
where BigInteger b = new BigInteger("1");
and BigInteger primes[] = new BigInteger[3];
and primes[0] = new BigInteger("11");, etc.
Serves the Hamming sequence associated with the list of prime numbers given by BigInteger p[]. The BigInteger m is technically not needed: it is used to help establish a unique binding name for Hamming subsequences and is the multiplier of tokens in that sequence.

Examples of the use of these classes is shown in the following table. All classes use an object of the GetStream class to connect to the RMI registry. This happens when the getStream() method is invoked with arguments for the respective constructors. All client classes below are applets containing a textarea for viewing stream tokens and a "Get Next" button for demanding the next token in the stream. The hostname of the RMI registry is hardwired into the code.

Class html Description
SuccessorClient successor.html Gets tokens 1, 2, 3, ... from a Successor stream.
TimesClient times.html Creates two Successor streams, multiplies tokens of one by 7 and one by 5. Outputs both in the textarea.
MergeClient merge.html Creates two Successor streams, a Times stream which multiplies by 7 and a Times stream that multiplies by 5. Each Times stream connects to a Successor stream. Serves the Merge of the two Times streams.
HammingClient hamming.html Serves tokens from the hamming sequence for the primes list 3,5,11.


  1. Collect all Impl and Client files along with Server.java, Monitor.java, GetStream.java, Stream.java, IntObject.java, and Makefile. Change the Makefile to suit your computer.

  2. Type make all

  3. Copy all Stub, Skel, Impl class files to ~java/jre/lib or the usual place where the jar files for your java distribution reside. Also copy Server.class and Monitor.class there.

  4. Leave Stub and Client class files as well as the Stream.class, and IntObject.class in the user's directory hierarchy.

  5. Type make rmi to start the registry and the server.

  6. Type make run_ham to run the Hamming sequence applet. Hit the "Get Next" button to get the next token in the stream. The servers that become registered are listed in the shell when they are registered.