Write high-performance RMI servers and Swing clients
Learn to implement asynchronous callbacks to Swing-based applications
By and Andy Krumel, JavaWorld.com, 04/01/99
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Sometime when you have a few minutes to burn, cruise into your local discount brokerage firm and watch the brokers operating
the service counter. During a typical lunch hour, customers crowd the service counter like software engineers ravaging a convention
booth giving away free t-shirts. The most effective brokers help multiple customers fill out forms, process checks, direct
customers to the appropriate prospectus, and answer the phone simultaneously. The brokers not destined to make a living in
the world of "churn and burn" simply serialize all of their tasks: help a customer fill out a form, or handle a phone call,
or process a check, or -- you get the idea. The efficient broker keeps the line moving by handling multiple tasks concurrently,
while the inefficient broker lets the tasks queue up as he or she works on a single task.
Which broker type best resembles your RMI, CORBA, or DCOM server process? Does your process perform synchronous writes to
the log while executing client requests? Are client callbacks executed within the same method (process thread) that forced
the notification? Does your server process perform callbacks in synchronized blocks? If you answered affirmatively to any
of these questions, your process may resemble that of the inefficient broker. This article will provide techniques for improving
the efficiency of your server, enabling it to handle more clients and return control to client processes more quickly.
Our example distributed program, SouthBay, provides a client application that allows users to bid on products and provides
realtime updates on the current high bids. The client application interacts with a server application that maintains the current
product bids and notifies clients when it receives a new high bid. This application uses RMI to illustrate the programming
techniques, but the techniques are equally valid for CORBA-, DCOM-, or TCP-based applications.
SouthBay application overview
The SouthBay distributed application provides the user an application that displays the current list of products for sale
and a server application that maintains the current high bid and informs registered clients when a price changes. Figure 1
shows the appearance of SouthBay's Swing-based, client user interface.

Figure 1. SouthBay client screenshot
Selecting an item in the Products list causes the product detail to be displayed in the Current Product panel, which shows
the product ID, description, current bid, and current bid acceptance time. This being a startup e-commerce venture, we don't
have to make a profit for the foreseeable future, so no provision has been made to track users or allow them to pay for a
product; after all, there has to be some incentive for them to upgrade. Clicking on the Bid on Item button pops up an input
message panel into which the user can enter a new bid (Figure 2).

Figure 2. Enter a new bid dialog
Pressing the OK button causes the client application to submit the bid to the server. If the new bid is higher than all previous
bids, the new bid replaces the previous high bid and the server broadcasts the new bid to all registered clients. Our client
registers with the server when it starts up and unregisters when the user closes the application.
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- Download the complete source in zip format. You will need Java 2 or JDK 1.1.X with the latest version of Swing 1.1.1 (currently
in beta) to run the code http://www.javaworld.com/jw-04-1999/enterprise/jw-04-enterprise.zip
- For a look at advanced features involving callbacks, see Andy's previous column "Revolutionary RMIDynamic class loading and
behavior objects" http://www.javaworld.com/javaworld/jw-12-1998/jw-12-enterprise.html
- Bryan Morgan's "Java 1.2 extends Java's distributed object capabilities" provides an excellent step-by-step RMI example http://www.javaworld.com/javaworld/jw-04-1998/jw-04-distributed.html
- Sun's online RMI tutorial offers an excellent starting point for those needing a little RMI brush-up http://java.sun.com/docs/books/tutorial/rmi/index.html
- Bill Venners's "Design for thread safety" offers guidelines to help you decide when thread safety is appropriate and how best
to achieve it http://www.javaworld.com/javaworld/jw-08-1998/jw-08-techniques.html
- Allan Hollub's "Programming Java threads in the real world, Part 2" is an excellent article on the problems we create for
ourselves when using threads http://www.javaworld.com/javaworld/jw-10-1998/jw-10-toolbox.html
- For a vendor's perspective of why its RMI implementation is better than JavaSoft's, see BEA WebLogic's white paper "Overview
of RMI in the WebLogic Framework" http://www.weblogic.com/docs/techoverview/rmi.html
- The Swing Connection contains excellent articles on effectively using Swing. This area of JavaSoft's site is definitely worth
swinging by every now and then http://java.sun.com/products/jfc/tsc/
- The Swing Connection archives contains two excellent articles relating to using Swing in a multithreaded environment:
-