From 757f63446707ac945694517fc8bd7c4771799a28 Mon Sep 17 00:00:00 2001 From: gautamshetty Date: Wed, 28 Mar 2018 07:35:53 -0500 Subject: [PATCH] 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. --- spring-ejb/pom.xml | 1 + spring-ejb/singleton-ejb-bean/pom.xml | 40 ++++++++++++++++ .../baeldung/singletonbean/CountryState.java | 12 +++++ .../singletonbean/CountryStateCacheBean.java | 41 +++++++++++++++++ .../CountryStateCacheBeanTest.java | 46 +++++++++++++++++++ 5 files changed, 140 insertions(+) create mode 100644 spring-ejb/singleton-ejb-bean/pom.xml create mode 100644 spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryState.java create mode 100644 spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryStateCacheBean.java create mode 100644 spring-ejb/singleton-ejb-bean/src/test/java/com/baeldung/singletonbean/CountryStateCacheBeanTest.java diff --git a/spring-ejb/pom.xml b/spring-ejb/pom.xml index 0b2a8445c5..6d88099bc5 100755 --- a/spring-ejb/pom.xml +++ b/spring-ejb/pom.xml @@ -73,5 +73,6 @@ ejb-remote-for-spring spring-ejb-client + singleton-ejb-bean diff --git a/spring-ejb/singleton-ejb-bean/pom.xml b/spring-ejb/singleton-ejb-bean/pom.xml new file mode 100644 index 0000000000..3f0a8ff9cd --- /dev/null +++ b/spring-ejb/singleton-ejb-bean/pom.xml @@ -0,0 +1,40 @@ + + 4.0.0 + com.baeldung.singletonsession + singleton-ejb-bean + 1.0.0-SNAPSHOT + EJB Singleton Session Bean + + + UTF-8 + + + + + + + javax + javaee-api + 8.0 + provided + + + + + org.apache.openejb + tomee-embedded + 1.7.5 + + + + + junit + junit + 4.12 + test + + + + + diff --git a/spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryState.java b/spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryState.java new file mode 100644 index 0000000000..a9a426ae32 --- /dev/null +++ b/spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryState.java @@ -0,0 +1,12 @@ +package com.baeldung.singletonbean; + +import java.util.List; + +import javax.ejb.Local; + +@Local +public interface CountryState { + + public List getStates(String country); + +} diff --git a/spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryStateCacheBean.java b/spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryStateCacheBean.java new file mode 100644 index 0000000000..3b4d80c88c --- /dev/null +++ b/spring-ejb/singleton-ejb-bean/src/main/java/com/baeldung/singletonbean/CountryStateCacheBean.java @@ -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> countryStatesMap = new HashMap>(); + + @Lock(LockType.WRITE) + @PostConstruct + public void initialize() { + + List states = new ArrayList(); + states.add("Texas"); + states.add("Alabama"); + states.add("Alaska"); + states.add("Arizona"); + states.add("Arkansas"); + + countryStatesMap.put("UnitedStates", states); + } + + @Lock(LockType.READ) + public List getStates(String country) { + return countryStatesMap.get(country); + } +} diff --git a/spring-ejb/singleton-ejb-bean/src/test/java/com/baeldung/singletonbean/CountryStateCacheBeanTest.java b/spring-ejb/singleton-ejb-bean/src/test/java/com/baeldung/singletonbean/CountryStateCacheBeanTest.java new file mode 100644 index 0000000000..988a4eeb6d --- /dev/null +++ b/spring-ejb/singleton-ejb-bean/src/test/java/com/baeldung/singletonbean/CountryStateCacheBeanTest.java @@ -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 states = countryStateBean.getStates("UnitedStates"); + if (states != null) { + assertArrayEquals(states.toArray(), actualStates); + } + } + + @After + public void close() { + if (ejbContainer != null) + ejbContainer.close(); + } +}