Java Reference
In-Depth Information
LISTINGĀ 9-2: (continued)
public void testLoggers(){
System.out.println("call async");
logBean.logAsync("Log Async");
System.out.println("call sync");
logBean.logInfo("Log Sync");
System.out.println("finished");
}
}
A typical console output would be as follows:
> call async
> Entering async log
> call sync
> Entering sync log
> Log Sync
> finished
> Log Async
After you execute the testLoggers() method, call the logAsync() and logSync() methods. Both
methods let their execution thread sleep for the given length of time. As can be seen from the
console output, the async() method was called and went into a long sleep but did not lock the
execution of the sync() method. The sync() method sleeps for a while but returns control to the
caller method and prints finished . Finally, the async() method wakes up and i nishes logging by
printing Log Async to the console.
This example clearly shows that the asynchronous call does not stop the caller thread, nor does it stop
the sync() method. However, when the sync() method goes into the sleep state, the caller method
waits until the sleep ends. The @Asynchronous annotation is an easy way to implement asynchronous
behavior and can be added to almost any method at any time during and after development.
Asynchronous Servlets
So far, you have seen that you can convert any method of a bean to an asynchronous method. Now
you'll look at how to make a servlet act asynchronously. Without asynchronous support in servlets,
it is hard to respond to the asynchronous web challenge.
The Servlet 3.0 specii cation (JSR 315) made huge improvements to the Java web application
programming interfaces (APIs). With JSR 315, the servlet specii cation was updated (after a long
wait) to support an asynchronous execution model, easy coni guration, pluggability, and other
minor enhancements.
 
Search WWH ::




Custom Search