CONTAINER Managed
Açıklaması şöyle
If nothing else is specified, by default singleton session bean uses container managed concurrency. Further, if not specified, every business and timeout method have by default LockType.WRITE. Result is that there is not multiple threads concurrently executing methods in singleton and as consequence using regular java.util.HashMap is perfectly fine.
Örnek
Şöyle yaparız
import javax.ejb.ConcurrencyManagement;import javax.ejb.ConcurrencyManagementType;import javax.ejb.Local;import javax.ejb.Lock;import javax.ejb.LockType;import javax.ejb.Singleton;import java.util.HashMap;import java.util.Map;@Singleton@Local(MapsAndQueuesSingletonBeanLocal.class)public class MapsAndQueuesSingletonBean implements MapsAndQueuesSingletonBeanLocal {private Map<String String> maps = new HashMap<>();@Overridepublic String getMap(String key) {return maps.computeIfAbsent(key, k -> ...);}}
Açıklaması şöyle
If nothing else is specified, by default singleton session bean uses container managed concurrency. Further, if not specified, every business and timeout method have by default LockType.WRITE. Result is that there is not multiple threads concurrently executing methods in singleton and as consequence using regular java.util.HashMap is perfectly fine.
BEAN Managed
Açıklaması şöyle
@Singleton in the default configuration is the perfect bottleneck. Only one thread a time can access a @Singleton instance.The annotation @ConcurrencyManagement(ConcurrencyManagementType.BEAN) deactivates this limitation and makes a Singleton instance accessible from multiple threads:
Örnek
Şöyle yaparız
import javax.ejb.Singleton; import javax.annotation.PostConstruct; import javax.ejb.ConcurrencyManagement; import java.util.concurrent.ConcurrentHashMap; import javax.ejb.ConcurrencyManagementType; @Singleton @ConcurrencyManagement(ConcurrencyManagementType.BEAN) public class Hits { private ConcurrentHashMap hits = null; @PostConstruct public void initialize() { this.hits = new ConcurrentHashMap(); } //cache accessors omitted }
Hiç yorum yok:
Yorum Gönder