Java Reference
In-Depth Information
LISTING 4‐11: (continued)
public void addUser(Integer id, String name){
myCache.put(id, name);
public String getName(Integer id){
return myCache.get(id);
Two lock types control access to the bean's business methods: @Lock(LockType.WRITE) , which locks
the bean to other clients while the method is being invoked, and @Lock(LockType.READ) , which
allows concurrent access to the method and does not lock the bean to other clients. Methods that
affect change to data are usually annotated with WRITE access to prevent access to the data as it
updates. In this example, the addUser() method is annotated with the WRITE lock type so that if any
client calls the getName() method, it has to wait until the addUser() method returns before it can
complete its call. This may result in the container throwing a ConcurrentAccessTimeoutException
if the addUser() method does not complete in the specii ed timeout period. You can coni gure the
timeout period via an annotation, which you will see an example of in Listing 4‐12.
You can set the LockType annotation at the class level. It applies to all business methods that don't
explicitly coni gure their own LockType . Because the default LockType is WRITE , it is normally
sufi cient to coni gure just the methods that require concurrent access.
LISTING 4‐12: Dei ning the singleton's concurrent timeout access
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.DependsOn;
import javax.ejb.ConcurrencyManagement;
import javax.ejb.ConcurrencyManagementType;
import javax.ejb.AccessTimeout;
import java.util.Map;
import javax.ejb.EJB;
import java.util.HashMap;
import javax.ejb.Lock;
import javax.ejb.LockType;
import java.util.concurrent.TimeUnit;
@AccessTimeout(value=120000) // default in milliseconds
Search WWH ::

Custom Search