BAEL - 717 : Singleton EJB Beans. (#3883)
* BAEL-717: Singleton EJB Bean Files for BAEL-717:Singleton EJB Bean. * BAEL-717: Singleton EJB Bean Corrected Indentation. * BAEL-717: Singleton EJB Bean Corrected Indentation. * BAEL-717: Singleton EJB Bean Corrected Indentation. * BAEL-717: Singleton EJB Bean Corrected Indentation. * BAEL-717: Singleton EJB Bean Changed artifactId value. * BAEL-717: Singleton EJB Bean. Added module for Singleton EJB Bean. * BAEL-717: Singleton EJB Bean. Removed Singleton EJB Bean Module. * BAEL-717: Singleton EJB Bean Changed the JNDI Lookup name. * BAEL-717: Singleton EJB Bean. Added the "singleton-ejb-bean" module. * BAEL-717: Singleton EJB Bean. Corrected Indentation. * BAEL-717: Singleton EJB Bean Corrected Indentation. * BAEL-717: Singleton EJB Bean. Corrected Indentation. * BAEL-717: Singleton EJB Bean. Corrected Indentation. * BAEL-717:Singleton EJB Bean. Corrected Indentation. * BAEL-717:Singleton EJB Bean. Corrected Indentation.
This commit is contained in:
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.singletonbean;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.ejb.Local;
|
||||
|
||||
@Local
|
||||
public interface CountryState {
|
||||
|
||||
public List<String> getStates(String country);
|
||||
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.singletonbean;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.ejb.ConcurrencyManagement;
|
||||
import javax.ejb.ConcurrencyManagementType;
|
||||
import javax.ejb.Lock;
|
||||
import javax.ejb.LockType;
|
||||
import javax.ejb.Singleton;
|
||||
import javax.ejb.Startup;
|
||||
|
||||
@Singleton
|
||||
@Startup
|
||||
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
|
||||
public class CountryStateCacheBean implements CountryState {
|
||||
|
||||
private Map<String, List<String>> countryStatesMap = new HashMap<String, List<String>>();
|
||||
|
||||
@Lock(LockType.WRITE)
|
||||
@PostConstruct
|
||||
public void initialize() {
|
||||
|
||||
List<String> states = new ArrayList<String>();
|
||||
states.add("Texas");
|
||||
states.add("Alabama");
|
||||
states.add("Alaska");
|
||||
states.add("Arizona");
|
||||
states.add("Arkansas");
|
||||
|
||||
countryStatesMap.put("UnitedStates", states);
|
||||
}
|
||||
|
||||
@Lock(LockType.READ)
|
||||
public List<String> getStates(String country) {
|
||||
return countryStatesMap.get(country);
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.singletonbean;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.ejb.embeddable.EJBContainer;
|
||||
import javax.naming.Context;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.singletonbean.CountryState;
|
||||
|
||||
public class CountryStateCacheBeanTest {
|
||||
|
||||
private EJBContainer ejbContainer = null;
|
||||
|
||||
private Context context = null;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
|
||||
ejbContainer = EJBContainer.createEJBContainer();
|
||||
context = ejbContainer.getContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallGetStates_ReturnsStatesForCountry() throws Exception {
|
||||
|
||||
String[] actualStates = { "Texas", "Alabama", "Alaska", "Arizona", "Arkansas" };
|
||||
|
||||
CountryState countryStateBean = (CountryState) context.lookup("java:global/singleton-ejb-bean/CountryStateCacheBean");
|
||||
List<String> states = countryStateBean.getStates("UnitedStates");
|
||||
if (states != null) {
|
||||
assertArrayEquals(states.toArray(), actualStates);
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (ejbContainer != null)
|
||||
ejbContainer.close();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user