Get smart with proxies and RMI

Use dynamic class loading to implement smart proxies in RMI

1 2 3 Page 3
Page 3 of 3

Using the Server menu, select Get DoorProxy. That replaces the list of DoorImpl_Stubs with DoorProxy objects in the client. Repeat your experimentation with the Door list or window. Since DoorProxy objects cache the location property, repainting the list doesn't generate calls to DoorImpl.getLocation().

To run the example

Download the zip file into a new directory and unzip it. The following files and directories should be created:,, and com/.

The com/ directory contains both the source code of the applications and the already compiled class files. To run the example, you first need to edit the file. Look for the following lines in that file: and java.rmi.server.codebase=file:/C:\\rmi\\example/.

Change the value of the property to the IP number or hostname of the computer on which you will be running those programs. The java.rmi.server.codebase property should contain the file URL location of the com/ directory. Make those changes and save the file.

To run the example, first start the RMI registry:


     unset CLASSPATH
     start rmiregistry


     unsetenv CLASSPATH
     rmiregistry &

Once the registry is running, start the server. First, cd to the directory where you copied the zip file. Enter this command:


     start java com.mjeffwilson.smartproxies.server.DoorServerImpl


     java com.mjeffwilson.smartproxies.server.DoorServerImpl &

You should see the following messages on the console:

Figure 6. Windows console running the DoorServer

At that time, the DoorServerImpl has been bound to the RMI registry and is ready for client connections. Now you can start the client with the following command:


     start java com.mjeffwilson.smartproxies.client.DoorClient


     java com.mjeffwilson.smartproxies.client.DoorClient &

"But what... is it good for?"

"But what... is it good for?" --Engineer at the Advanced Computing Systems Division of IBM, 1968, commenting on the microchip.

The example illustrates the benefits of using the smart proxy approach in caching frequently read, seldom-updated data of remote objects. But the possibilities are not limited to that scenario. You might use smart proxies to monitor the performance of RMI calls (logging the time at invocation and return from a method call) or to handle remote exceptions in a standard way on the client.

Another good idea is to use smart proxies to prevent returning multiple copies of the same remote object to client code. A smart proxy could create a singleton object that wrapped a hashtable. Whenever a smart proxy method is called that would return a remote object (either a stub or a proxy) to the client, the hashtable could be checked for an identical object. If an equivalent object existed, it could be returned to the client instead of a new copy of the object. If an identical object didn't exist, the smart proxy could store it in the hashtable.

Obviously, a great number of other uses for smart proxies exist -- you can now start dreaming them up!

M. Jeff Wilson develops network management systems in Java for BellSouth Telecommunications' FastAccess Internet services. A Sun-certified Java programmer, he has more than 16 years of experience in software development, coming to Java by way of C++ and Objective-C. Jeff is currently working on his Master's degree in software systems at Atlanta's Mercer University.

Learn more about this topic

This story, "Get smart with proxies and RMI" was originally published by JavaWorld.


Copyright © 2000 IDG Communications, Inc.

1 2 3 Page 3
Page 3 of 3