Java Reference
In-Depth Information
logger.info(msg);
}
@Asynchronous
public void logAsync(String msg){
logger.info(msg);
}
}
The
logAsync()
method, unlike its
logInfo()
counterpart, is executed asynchronously. To observe
asynchronous behavior, add
Thread.sleep()
calls:
public void logInfo(String msg) {
logger.info(“Entering sync log”);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
logger.info(msg);
}
@Asynchronous
public void logAsync(String msg {
logger.info(“Entering async log”);
try {
Thread.sleep(13000);
} catch (InterruptedException e) {}
logger.info(msg);
}
Finally, create a new bean to call both functions in order, as shown in Listing 9‐2.
LISTING 9‐2:
Refactor of Listing 9.1 to include both functions
package com.devchronicles.asynchronous;
import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
@Startup
@Singleton
public class TestLogging {
@EJB
MyLoggingBean logBean;
continues
@PostConstruct