Java Reference
In-Depth Information
Java EE offers two types of concurrency management: container‐managed concurrency and
bean‐managed concurrency . In container‐managed concurrency, the container is responsible for
y
handling anything related to read and write access, whereas bean‐managed concurrency expects
the developer to handle concurrency using traditional Java methods such as synchronization. You
can enable bean‐managed concurrency via the ConcurrencyManagementType.BEAN annotation.
Java EE uses container‐managed concurrency by default, but you can explicitly declare it with the
ConcurrencyManagementType.CONTAINER annotation:
@Startup
@DependsOn("MyLoggingBean")
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
@Singleton
public class CacheSingletonBean {
// Implementation code here.
}
Now you'll get back to the example and use the @Lock annotations to control the access. See Listing 4‐11.
LISTING 4‐11: Managing concurrency using @locktype
package com.devchronicles.singleton;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.ConcurrencyManagement;
import javax.ejb.ConcurrencyManagementType;
import javax.ejb.DependsOn;
import javax.ejb.EJB;
import javax.ejb.Lock;
import javax.ejb.LockType;
import javax.ejb.Singleton;
import javax.ejb.Startup;
@Startup
@DependsOn("MyLoggingBean")
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
@Singleton
public class CacheSingletonBean {
private Map<Integer, String> myCache;
@EJB
MyLoggingBean loggingBean;
@PostConstruct
public void start(){
loggingBean.logInfo("Started!");
myCache = new HashMap<Integer, String>();
}
continues
Search WWH ::




Custom Search