238
General Considerations
ground, by invoking the Web service in a different thread, and then updating the user
interface when the information is received. The client is able to continue its interac
tion with the user until the service returns the information. With other types of cli
ents, the user is often left with what appears to be a frozen screen or a nonfunctional
or locked application, since the client application blocks during the call to the Web
service. In short, the user does not know whether the application is still alive.
A J2SE client can use the
SwingUtilities.invokeLater
method to make the
call to the service in a separate thread, and thus to achieve a better user experi
ence. (See Code Example 5.19.) This method takes a single argument of an object
implementing the
Runnable
interface. When the
invokeLater
method is invoked,
the J2SE platform creates another thread to run in background mode by invoking
the
run
method on the
Runnable
class.
private void trackOrder() {
setStatus("Tracking Order " + getOrderId());
GetOrderDetails gd = new GetOrderDetails(this);
SwingUtilities.invokeLater(gd);
}
class GetOrderDetails implements Runnable {
private OTClientGUI gui;
private String orderId;
boolean done = false;
GetOrderDetails(OTClientGUI gui) {
this.gui = gui;
this.orderId = gui.getOrderId();
}
public void run() {
if (!done) {
try {
gui. setStatus("Looking for Order " + orderId);
OrderDetails od = WSProcessor.getOrderDetails(orderId);
if (od != null) {
gui.setDetails(od);
}
} catch (OrderNotFoundException ex) {
gui.clearDetails();
New Page 1