Java Reference
In-Depth Information
The alternative is to create a system that allows a client to register for server notification. When the server has
completed the requested operation, it actively notifies the client. In the meantime, both the client and server are
free to use their resources for more productive purposes than maintaining this specific communication link.
The Callback pattern provides this capability, allowing for asynchronous client-server communication. The
process consists of three simple steps:
Client registration - The client makes a request, providing contact information to the server.
For instance, the client contacts the server and makes a request. Normally, the client requests information, such as
all the sales figures for the 2001 fiscal year, or action, like entering the user in the Frito-lay trip to Elbonia
sweepstakes. Since the client doesn't expect an immediate response, it provides contact information to the server.
Server processing - The server processes the client's request and formats a response if required. During this time,
the client can be involved in other tasks, and the server can handle communication requests for other clients.
Server callback - When the server has completed the client's request, it sends a notification message to the client.
The notification generally takes one of two forms:
The information requested by the client. This approach is generally used when the client definitely needs all the
data or when the data is relatively low-bandwidth to send.
A message informing the client that the data or parts of the data is available. This is generally done for larger
amounts of information, so that the client may retrieve parts of the data, either as the parts become available or if
not all the data is needed by the client and the client just requests the data it needs.
For an example of the Callback pattern, consider a father and his three sons on a shopping trip. Number 1 son
wants a new Robot Laser Geek action figure; Number 2 son wants a laptop, and Number 3 son wants the latest
Soft Core Java book. However:
These things can take a long time to find, especially since they're not sold in the same stores.
The sons have 5-minute attention spans and get cranky if they have to go shopping for a long time.
The father can only shop for one o r two things at a time; if he has to do more, his performance plummets.
Luckily, the father can drop his sons at the mall's arcade to play games or fight amongst themselves, or do whatever else they want. The
father then goes shopping for one of the items, buys it, drops it off, sets off to find another item, and so on.
You can use Callback in a number of applications.
Agent software, now popular on the Web, can use Callback to notify a client when the request is complete. For instance, consider a job
searc h agent on Monster.com. A user can enter search criteria for a desired job; for example, they might be
looking for jobs; one requiring a good nose for a good lager, for example. Subsequently, the server notifies the
user when a tasting position at the local microbrewery becomes available.
Applications requiring costly database operations, such as data mining, frequently use Callback to increase the number of clients that
they can service effectively.
You can use the Callback pattern for applications that ha ve a detailed server-side workflow. For example, a server involved
in order processing often performs a variety of operations once a client has submitted an order. The server often
checks inventory, validates payment and shipping information, and coordinates with other information systems,
such as warehouse, manufacturing, invoicing, and shipping. The Callback pattern allows the server to notify the
client about the status of the order after these steps have been performed. Since these op erations can take hours or days,
most customers prefer a Callback solution as well.
Implementation
The Callback component diagram is shown in Figure 4.6 .
Figure 4.6. Callback component diagram
 
Search WWH ::




Custom Search