[BAEL 717]-Singleton Session Bean. (#4001)

* 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.

* BAEL-717: Singleton Session Bean.

Added class for Bean-Managed concurrrency.
Changed class name from CountryStateCacheBean to CountryStateContainerManagedBean.

* BAEL-717: Singleton Session Bean.

Changing the name of the class to CountryStateContainerManagedBean.

* BAEL-717: Singleton Session Bean.

Added method to test Bean-Managed concurrency.

* Get Latest.

* deleting CountryStateBeanManagedBean for new file.

* deleting CountryStateCacheBean for new file.

* deleting CountryStateContainerManagedBean for new file.

* BAEL-717: Singleton Session Bean.

Adding file for Bean with Bean-Managed concurrency.
Changing file name for original file to CountryStateContainerManagedBean with Container-Managed concurrency.

* Deleting file for new checkin.

* BAEL-717: Singleton Session Bean.

Added test case for Bean-Manged concurrency.
Change in JNDI names.

* BAEL-717: Singleton Session Bean.

Changed the assert method parameter order and null check in test cases.
This commit is contained in:
gautamshetty 2018-04-15 14:26:30 -05:00 committed by pauljervis
parent 9adb3e359b
commit 87e4c25f45
3 changed files with 58 additions and 10 deletions

View File

@ -0,0 +1,39 @@
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.BEAN)
public class CountryStateBeanManagedBean implements CountryState {
private volatile Map<String, List<String>> countryStatesMap = new HashMap<String, List<String>>();
@PostConstruct
public synchronized 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);
}
public List<String> getStates(String country) {
return countryStatesMap.get(country);
}
}

View File

@ -16,7 +16,7 @@ import javax.ejb.Startup;
@Singleton
@Startup
@ConcurrencyManagement(ConcurrencyManagementType.CONTAINER)
public class CountryStateCacheBean implements CountryState {
public class CountryStateContainerManagedBean implements CountryState {
private Map<String, List<String>> countryStatesMap = new HashMap<String, List<String>>();

View File

@ -1,6 +1,7 @@
package com.baeldung.singletonbean;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertNotNull;
import java.util.List;
@ -11,8 +12,6 @@ import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.baeldung.singletonbean.CountryState;
public class CountryStateCacheBeanTest {
private EJBContainer ejbContainer = null;
@ -27,15 +26,25 @@ public class CountryStateCacheBeanTest {
}
@Test
public void whenCallGetStates_ReturnsStatesForCountry() throws Exception {
public void whenCallGetStatesFromContainerManagedBean_ReturnsStatesForCountry() throws Exception {
String[] actualStates = { "Texas", "Alabama", "Alaska", "Arizona", "Arkansas" };
String[] expectedStates = { "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);
CountryState countryStateBean = (CountryState) context.lookup("java:global/singleton-ejb-bean/CountryStateContainerManagedBean");
List<String> actualStates = countryStateBean.getStates("UnitedStates");
assertNotNull(actualStates);
assertArrayEquals(expectedStates, actualStates.toArray());
}
@Test
public void whenCallGetStatesFromBeanManagedBean_ReturnsStatesForCountry() throws Exception {
String[] expectedStates = { "Texas", "Alabama", "Alaska", "Arizona", "Arkansas" };
CountryState countryStateBean = (CountryState) context.lookup("java:global/singleton-ejb-bean/CountryStateBeanManagedBean");
List<String> actualStates = countryStateBean.getStates("UnitedStates");
assertNotNull(actualStates);
assertArrayEquals(expectedStates, actualStates.toArray());
}
@After