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:
parent
de06e4f6be
commit
757f634467
|
@ -73,5 +73,6 @@
|
|||
<modules>
|
||||
<module>ejb-remote-for-spring</module>
|
||||
<module>spring-ejb-client</module>
|
||||
<module>singleton-ejb-bean</module>
|
||||
</modules>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.singletonsession</groupId>
|
||||
<artifactId>singleton-ejb-bean</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<name>EJB Singleton Session Bean</name>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/javax/javaee-api -->
|
||||
<dependency>
|
||||
<groupId>javax</groupId>
|
||||
<artifactId>javaee-api</artifactId>
|
||||
<version>8.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/org.apache.openejb/tomee-embedded -->
|
||||
<dependency>
|
||||
<groupId>org.apache.openejb</groupId>
|
||||
<artifactId>tomee-embedded</artifactId>
|
||||
<version>1.7.5</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/junit/junit -->
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -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);
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue