Example of Using an Observer Interface

Demonstration

This example demonstrates the use of calling a JavaCGIBridge URL with the asynchronous callOneWay method. This method sends data to the URL and returns immediately. This can be useful in the case where you want to send an update to a database through a CGI script but do not care about the reliability.

However, there is a second purpose for callOneWay. If you do care about the results, but do not wish to block your application while waiting for results, you can implement an Observer interface in your applet. Then, whenever results are parsed from the JavaCGIBridge, your applet will be notified at pre-specified intervals. In the case of this sample applet, the specified interval is set to notify every parsed record (setParsedNotifyInterval(1)).

This applet simply calls a CGI script the returns 100 records. Then, an observer update() interface method is called for each record returned in the applet. This update() method is coded to update a Progress Bar Panel which draws the percentage complete. Note that this drawing is dependant on how your browser buffers data before sending it to Jave as well as your web server. Most web server's buffer CGI data and send it in chunks. So depending on your web server/browser mix, the progress bar may grow steadily but quickly, or may just complete all at once as one big data chunk gets sent and parsed by the JavaCGIBridge.

The advantage of using an observer interface, of course, is that your application will not block waiting for all the data to be parsed. Thus, after calling the script, your application can go on doing what other processing it needs to. It only gets "interrupted" whenever new data comes in from the JavaCGIBridge thread which actually performs the processing. In addition, if you have a LOT of records to download you can do fancy stuff like adding a "Cancel" button to the Progress Bar which allows the user to stop receiving the data if it looks like the query is taking too long.

HINT: If you have many JavaCGIBridge objects you wish to observe, you may wish to use separate Inner Classes to accomplish each observing within the applet. Of course, this increases the number of class files the browser needs to load, but your code may be more readable than using many separate classes that have to know about the applet's private variables.

NOTE: In order to make the progress bar painting simulate a longer download time, I added a delay of 10 milliseconds for each update of the progress bar.

Applet Demonstrating Observer Interface to JavaCGIBridge

View The Source To Example7Applet.java

View The Source To Example7ProgressPanel.java

View The Source To JavaCGI Bridge.java

View The JavaCGIBridge JavaDoc File

View The Source To JavaCGIBridgeNotify.java

View The JavaCGIBridgeNotify JavaDoc File

View The Source To JavaCGIBridgeTimeOutException.java

View The JavaCGIBridgeTimeOutException JavaDoc File

View The Source To get_100_records.cgi

Plain CGI script retrieving 100 records

Run get_100_records.cgi CGI/Perl Script

Gunther Birznieks <gunther@clark.net>