Skip to content

Commit f1129ea

Browse files
gautamshettypauljervis
authored andcommitted
BAEL 717 - Singleton Session Bean (eugenp#4104)
* 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. * BAEL-717:Singleton Session Bean Removed volatile keyword for the variable countryStatesMap. Marking it as final. * BAEL 717 - Singleton EJB Bean Added new method setStates(). * BAEL-717: Singleton Session Bean. Renaming directory singleton-ejb-bean to ejb-beans. * BAEL-717: Singleton EJB Beans. Renaming directory name from singleton-ejb-bean to ejb-beans. * BAEL-717: Singleton Session Bean Renaming directory singleton-ejb-bean to ejb-beans.
1 parent 0f960f4 commit f1129ea

6 files changed

Lines changed: 46 additions & 7 deletions

File tree

spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryState.java renamed to spring-ejb/ejb-beans/src/main/java/com/baeldung/singletonbean/CountryState.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@
88
public interface CountryState {
99

1010
public List<String> getStates(String country);
11+
12+
public void setStates(String country, List<String> states);
1113

1214
}

spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryStateBeanManagedBean.java renamed to spring-ejb/ejb-beans/src/main/java/com/baeldung/singletonbean/CountryStateBeanManagedBean.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class CountryStateBeanManagedBean implements CountryState {
1919
private final Map<String, List<String>> countryStatesMap = new HashMap<String, List<String>>();
2020

2121
@PostConstruct
22-
public synchronized void initialize() {
22+
public void initialize() {
2323

2424
List<String> states = new ArrayList<String>();
2525
states.add("Texas");
@@ -34,4 +34,8 @@ public synchronized void initialize() {
3434
public List<String> getStates(String country) {
3535
return countryStatesMap.get(country);
3636
}
37+
38+
public synchronized void setStates(String country, List<String> states) {
39+
countryStatesMap.put(country, states);
40+
}
3741
}

spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryStateContainerManagedBean.java renamed to spring-ejb/ejb-beans/src/main/java/com/baeldung/singletonbean/CountryStateContainerManagedBean.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,9 @@ public void initialize() {
3838
public List<String> getStates(String country) {
3939
return countryStatesMap.get(country);
4040
}
41+
42+
@Lock(LockType.WRITE)
43+
public void setStates(String country, List<String> states) {
44+
countryStatesMap.put(country, states);
45+
}
4146
}

spring-ejb/singleton-ejb-bean/src/test/java/com/baeldung/singletonbean/CountryStateCacheBeanTest.java renamed to spring-ejb/ejb-beans/src/test/java/com/baeldung/singletonbean/CountryStateCacheBeanTest.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import static org.junit.Assert.assertArrayEquals;
44
import static org.junit.Assert.assertNotNull;
55

6+
import java.util.Arrays;
7+
import java.util.Collections;
68
import java.util.List;
79

810
import javax.ejb.embeddable.EJBContainer;
@@ -24,29 +26,55 @@ public void init() {
2426
ejbContainer = EJBContainer.createEJBContainer();
2527
context = ejbContainer.getContext();
2628
}
27-
29+
2830
@Test
2931
public void whenCallGetStatesFromContainerManagedBean_ReturnsStatesForCountry() throws Exception {
3032

3133
String[] expectedStates = { "Texas", "Alabama", "Alaska", "Arizona", "Arkansas" };
3234

33-
CountryState countryStateBean = (CountryState) context.lookup("java:global/singleton-ejb-bean/CountryStateContainerManagedBean");
35+
CountryState countryStateBean = (CountryState) context.lookup("java:global/ejb-beans/CountryStateContainerManagedBean");
3436
List<String> actualStates = countryStateBean.getStates("UnitedStates");
3537
assertNotNull(actualStates);
3638
assertArrayEquals(expectedStates, actualStates.toArray());
3739
}
38-
40+
3941
@Test
4042
public void whenCallGetStatesFromBeanManagedBean_ReturnsStatesForCountry() throws Exception {
4143

4244
String[] expectedStates = { "Texas", "Alabama", "Alaska", "Arizona", "Arkansas" };
4345

44-
CountryState countryStateBean = (CountryState) context.lookup("java:global/singleton-ejb-bean/CountryStateBeanManagedBean");
46+
CountryState countryStateBean = (CountryState) context.lookup("java:global/ejb-beans/CountryStateBeanManagedBean");
4547
List<String> actualStates = countryStateBean.getStates("UnitedStates");
4648
assertNotNull(actualStates);
4749
assertArrayEquals(expectedStates, actualStates.toArray());
4850
}
49-
51+
52+
@Test
53+
public void whenCallSetStatesFromContainerManagedBean_SetsStatesForCountry() throws Exception {
54+
55+
String[] expectedStates = { "California", "Florida", "Hawaii", "Pennsylvania", "Michigan" };
56+
57+
CountryState countryStateBean = (CountryState) context.lookup("java:global/ejb-beans/CountryStateContainerManagedBean");
58+
countryStateBean.setStates("UnitedStates", Arrays.asList(expectedStates));
59+
60+
List<String> actualStates = countryStateBean.getStates("UnitedStates");
61+
assertNotNull(actualStates);
62+
assertArrayEquals(expectedStates, actualStates.toArray());
63+
}
64+
65+
@Test
66+
public void whenCallSetStatesFromBeanManagedBean_SetsStatesForCountry() throws Exception {
67+
68+
String[] expectedStates = { "California", "Florida", "Hawaii", "Pennsylvania", "Michigan" };
69+
70+
CountryState countryStateBean = (CountryState) context.lookup("java:global/ejb-beans/CountryStateBeanManagedBean");
71+
countryStateBean.setStates("UnitedStates", Arrays.asList(expectedStates));
72+
73+
List<String> actualStates = countryStateBean.getStates("UnitedStates");
74+
assertNotNull(actualStates);
75+
assertArrayEquals(expectedStates, actualStates.toArray());
76+
}
77+
5078
@After
5179
public void close() {
5280
if (ejbContainer != null)

‎spring-ejb/pom.xml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@
7373
<modules>
7474
<module>ejb-remote-for-spring</module>
7575
<module>spring-ejb-client</module>
76-
<module>singleton-ejb-bean</module>
76+
<module>ejb-beans</module>
7777
</modules>
7878
</project>

0 commit comments

Comments
 (0)