Java IDL: Example with Callback Object


Client programs often react to changes or updates that occur in a server. For example, a client graph or spreadsheet program might need to be updated with each stock price update on a stock market server. The client has two options in this scenario:

The example in this document illustrates how a client program can pass a callback object to a server, and the server can then callback to notify changes to the client. At this time, we have provided the code for an extension of a simple application. Notes about simplifying the application are contained within the code.

This document provides the code for:

Instructions for compiling and running the example are also provided.

Callback Example: Intermediate Level

Writing the IDL file

For the example application, the file callback.idl looks like this:

Writing the Server Code

For the example application, the Server.java file looks like as follows.

Writing the MessageServer Implementation

The file registers new clients, accepts messages, then relays the messages to the registered clients. For the example application, the file MessageServerImpl.java looks like the following example.

Writing the Client Code

For the example application, the file Client.java file looks like the following example.

Writing the Listener Implementation

When the Listener identifies that a message has been received from the server, it displays the message on the client. For the example application, the ListenerImpl.java file looks like the following example.

Instructions for compiling and running the example

To run the example application, you would follow these instructions on the SERVER machine:

  1. If you've run the example previously, remove any existing generated files. In the Unix operating environment, this command would be as follows:
      rm -rf ./classes ./orb.db
      

  2. Generate the stubs and skeletons. First, you need to create the directory where the stubs and skeletons will reside, then run the idlj compiler to generate the stubs and skeletons. This command would look like this:
      mkdir -p ./classes
      <path_to_java>/bin/idlj -fall -td ./classes callback.idl
      

  3. Compile the .java files, as follows:
      <path_to_java>/bin/javac -classpath ./classes -d ./classes *.java
      

  4. Start the orbd naming service, as follows:
      <path_to_java>/bin/orbd -ORBInitialPort 1050 -ORBInitialHost <host_name> &
      

  5. Run the server, as follows:
      <path_to_java>/bin/java -classpath ./classes Server -ORBInitialPort 1050
      

After you have started the server, you will see output such as the following in the terminal window:

At this prompt, you will send messages to the client, but first you need to start the client application. From the CLIENT terminal, follow these steps to run the client. Make sure that you used the host name on which the server is running in the properties section of the client code.

The output from the client will display in a format similar to this:

To display the functionality of using a callback, you would enter a data on the server terminal, at the prompt, and it will display on all client terminals. This could be anything where clients are interested in some notifications.

One example is a football game score where clients are notified whenever the score changes.

When messages such as those shown above are sent, the client display looks similar to the following:


Home

Copyright © 2002-2004 Sun Microsystems, Inc. All Rights Reserved.