Java Reference
In-Depth Information
The concurrency framework JSR‐133 offers great tools for threads and concurrent
programming, such as BlockingQueues . These topics are beyond the scope of this chapter.
See the topic Java Concurrency in Practice
4 for further reading. The Fork/Join Framework,
which was introduced in Java 7, also offers a huge change in favor of asynchronous and parallel
programming in Java.
ASYNCHRONOUS PROGRAMMING IN JAVA EE
Because J2EE failed to offer built‐in support for the asynchronous programming paradigms
(except for Timer), third‐party frameworks, such as Spring and Quartz, stepped in to i ll this gap.
This dei cit was corrected in Java EE 5; it was the i rst Java version to support the asynchronous
programming pattern out of the box.
Asynchronous Beans
Java EE supports asynchronous programming in several ways. The simplest way to
implement the asynchronous pattern in Java EE is, not surprisingly, via the application
of an annotation. Annotating a method with @Asynchronous is enough to advise the Java
EE container to asynchronously execute the invoked method in a separate thread. To see
the asynchronous annotation in action, go back to the singleton logging bean example in
Chapter 4, “Singleton Pattern” and add the asynchronous annotation to change its default behavior.
Listing 9-1 shows an example of an asynchronous bean.
LISTING 9‐1: An example of an asynchronous bean
package com.devchronicles.asynchronous;
import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import java.util.logging.Logger;
import javax.ejb.Asynchronous;
@Startup
@Singleton
public class MyLoggingBean {
private Logger logger;
@PostConstruct
public void start(){
logger = Logger.getLogger("MyGlobalLogger");
logger.info("Well, I started first!!!");
}
public void logInfo(String msg){
 
Search WWH ::




Custom Search