Java Reference
In-Depth Information
Another benefit is that the client does not have to wait for the full processing by the server and it can continue
with other tasks. The client can go about its business while it's waiting for the server response. When results are
available, they can be displayed immediately.
Depending on the implementation of the pattern, Callback can queue client requests, allowing the server to
organize and prioritize its workload. It also potentially allows the server to notify clients of changes beyond the
typical lifetime of a client. Web agents are a good example of this, since they allow a client to enter a query in
one Web session, and be notified of the results in another.
One challenge of the Callback is that it requires a client to listen for the callback from the server. This often
makes client code more complex, and increases the load on client systems. An additional drawback stems from
the fact that Callback decouples the request from the client. This often makes it difficult to cancel or modify a
request once it has been sent to the server.
Pattern Variants
Variations of the Callback pattern generally center on server processing strategies and approaches to client
notification. Two major approaches are common in server-side processing:
Direct processing - With this approach, the server creates a worker thread to fulfill each client's request. This is
straightforward to implement, but is sometimes difficult to scale to large numbers of service requesters.
Request queue - The server maintains a queue of client requests and a pool of worker threads. The worker
threads (see “ Worker Thread ” on page 231) are assigned to perform client processing on an ongoing basis.
A few options are available for client notification, depending on the application requirements:
Active callback - A client uses server-like process to listen for incoming communications. This allows the client
to directly receive server notification.
Client polling - Also known as client pull , this requires a client to periodically check on the status of its request.
When the request or parts of the request are complete, the client will request that information from the server.
Explicit acknowledgment - A server may retransmit a message until it receives client confirmation. This is som etimes used for
cases where the server processing can take longer than the client application's lifetime. Although this is not
relevant in TCP since the socket won't open unless the client is there to do its part, it is meaningful when using
unreliable communication technologies like UDP.
Related Patterns
Related patterns include Worker Thread (page 231). The Worker Thread pattern is used to help schedule client
requests. The requests are put in a queue and the worker threads process them.
Example
Note:
For a full working example of this code example, with additional supporting classes and/or a RunPattern class,
see “ Callback ” on page 525 of the “ Full Code Examples ” appendix.
In the Personal Information Manager, one of the items that can vary most in size is a project. A project might
consist of only a few tasks, or it could be made up of hundreds or even thousands of individual work steps. This
example demonstrates how the Callback pattern could be used to retrieve a project object stored on a server
machine.
The interface CallbackServer defines a single server-side method, getProject . Note that the method requires
callback information—the client machine name and the name of the RMI client object—in addition to the project
ID. The class CallbackServerImpl implements this interface.
Example 4.15 CallbackServer.java
1. import java.rmi.Remote;
2. import java.rmi.RemoteException;
Search WWH ::




Custom Search