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: (1) periodically
ask for the stock price via a method request on the stock server or
(2) ask to be notified by the server whenever a price change occurs.
The second option is referred to as a "callback".
Example 3 illustrates how a client program can pass a callback object
to a server. The server then issues a method request on the callback
object and thereby notifies the client.
Example 3 is identical to Example 1 except for the callback enhancements.
This page only discusses the code necessary to
these enhancements.
This page contains:
The IDL for a "Hello world" program with a callback.
A server implementation that callsback to a client.
A client that sends a callback object reference to a server.
Instructions for compiling and running
the example are provided.
A HelloCallback is defined, which the client will implement.
The sayHello method is modified to take an object reference
argument and string argument. The object reference argument provides a means
for the client to pass a callback object to the server, which the server can
invoke. The string argument is the string that the server will send back to
the client.
The following instructions assume you can use port 1050
for the Java IDL name server. Substitute a different
port if necessary. Note that for ports below 1024,
you need root access on UNIX machines, and administrator
privileges on Windows95 and NT. Note also that the instructions use
a slash (/) for path names. Windows95 and NT users should
subtitute a backslash (\).
Create the source code files as shown above.
Run idlj on the IDL file to
create stubs and skeletons:
idlj -fall Hello.idl
Compile the .java files, including the stubs and skeletons:
javac *.java HelloApp/*.java
Make sure the name server is running:
tnameserv -ORBInitialPort 1050&
Start the Hello server:
java HelloServer -ORBInitialPort 1050
Run the Hello application client from a different shell than the
server: