JAVA-12424 Renamed spring-ejb to spring-ejb-modules
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
## Spring EJB
|
||||
|
||||
This module contains articles about Spring with EJB
|
||||
|
||||
### Relevant Articles
|
||||
|
||||
- [Guide to EJB Set-up](https://www.baeldung.com/ejb-intro)
|
||||
- [Java EE Session Beans](https://www.baeldung.com/ejb-session-beans)
|
||||
- [A Guide to Message Driven Beans in EJB](https://www.baeldung.com/ejb-message-driven-beans)
|
||||
- [Integration Guide for Spring and EJB](https://www.baeldung.com/spring-ejb)
|
||||
- [Singleton Session Bean in Jakarta EE](https://www.baeldung.com/java-ee-singleton-session-bean)
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Spring Bean vs. EJB – A Feature Comparison](https://www.baeldung.com/spring-bean-vs-ejb)
|
||||
@@ -0,0 +1,209 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>ejb-beans</artifactId>
|
||||
<name>ejb-beans</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung.spring.ejb</groupId>
|
||||
<artifactId>spring-ejb-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jboss.arquillian</groupId>
|
||||
<artifactId>arquillian-bom</artifactId>
|
||||
<version>${arquillian-bom.version}</version>
|
||||
<scope>import</scope>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- https://mvnrepository.com/artifact/javax/javaee-api -->
|
||||
<dependency>
|
||||
<groupId>javax</groupId>
|
||||
<artifactId>javaee-api</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.apache.tomee/tomee-embedded -->
|
||||
<dependency>
|
||||
<groupId>org.apache.tomee</groupId>
|
||||
<artifactId>tomee-embedded</artifactId>
|
||||
<version>${tomee-embedded.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>com.sun.xml.messaging.saaj</groupId>
|
||||
<artifactId>saaj-impl</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.xml.messaging.saaj</groupId>
|
||||
<artifactId>saaj-impl</artifactId>
|
||||
<version>3.0.0-M2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${springframework.version}</version>
|
||||
</dependency>
|
||||
<!-- Dependency for EJB javax -->
|
||||
<dependency>
|
||||
<groupId>javax.ejb</groupId>
|
||||
<artifactId>javax.ejb-api</artifactId>
|
||||
<version>${javax.ejb-api.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- Spring JMS -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jms</artifactId>
|
||||
<version>${springframework.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<!-- ActiveMQ -->
|
||||
<dependency>
|
||||
<groupId>org.apache.activemq</groupId>
|
||||
<artifactId>activemq-broker</artifactId>
|
||||
<version>${activemq.broker.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.activemq.tooling</groupId>
|
||||
<artifactId>activemq-junit</artifactId>
|
||||
<version>${activemq.junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jboss.arquillian.junit</groupId>
|
||||
<artifactId>arquillian-junit-container</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
|
||||
<dependency>
|
||||
<groupId>joda-time</groupId>
|
||||
<artifactId>joda-time</artifactId>
|
||||
<version>${joda-time.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>wildfly-managed-arquillian</id>
|
||||
<activation>
|
||||
<activeByDefault>true</activeByDefault>
|
||||
</activation>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.wildfly.arquillian</groupId>
|
||||
<artifactId>wildfly-arquillian-container-managed</artifactId>
|
||||
<version>5.0.0.Alpha3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-dependency-plugin</artifactId>
|
||||
<version>${maven-dependency-plugin.version}</version>
|
||||
<configuration>
|
||||
<skip>${maven.test.skip}</skip>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>unpack</id>
|
||||
<phase>process-test-classes</phase>
|
||||
<goals>
|
||||
<goal>unpack</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<artifactItems>
|
||||
<artifactItem>
|
||||
<groupId>org.wildfly</groupId>
|
||||
<artifactId>wildfly-dist</artifactId>
|
||||
<version>${wildfly.version}</version>
|
||||
<type>zip</type>
|
||||
<overWrite>false</overWrite>
|
||||
<outputDirectory>${project.build.directory}</outputDirectory>
|
||||
</artifactItem>
|
||||
</artifactItems>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
<!-- Fork every test because it will launch a separate AS instance -->
|
||||
<forkMode>always</forkMode>
|
||||
<systemProperties>
|
||||
<property>
|
||||
<name>java.util.logging.config.file</name>
|
||||
<value>src/test/resources/logging.properties</value>
|
||||
</property>
|
||||
</systemProperties>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>arquillian-glassfish-embedded</id>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jboss.arquillian.container</groupId>
|
||||
<artifactId>arquillian-glassfish-embedded-3.1</artifactId>
|
||||
<version>${arquillian-glassfish-embedded-3.1.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.main.extras</groupId>
|
||||
<artifactId>glassfish-embedded-all</artifactId>
|
||||
<version>${glassfish-embedded-all.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>${maven-war-plugin.version}</version>
|
||||
<configuration>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<arquillian-bom.version>1.6.0.Final</arquillian-bom.version>
|
||||
<tomee-embedded.version>8.0.11</tomee-embedded.version>
|
||||
<glassfish-embedded-all.version>6.2.2</glassfish-embedded-all.version>
|
||||
<arquillian-glassfish-embedded-3.1.version>1.0.2</arquillian-glassfish-embedded-3.1.version>
|
||||
<wildfly.version>8.2.1.Final</wildfly.version>
|
||||
<javax.ejb-api.version>3.2</javax.ejb-api.version>
|
||||
<springframework.version>5.2.3.RELEASE</springframework.version>
|
||||
<activemq.broker.version>5.16.3</activemq.broker.version>
|
||||
<activemq.junit.version>5.16.3</activemq.junit.version>
|
||||
<mockito.version>2.21.0</mockito.version>
|
||||
<maven-dependency-plugin.version>2.8</maven-dependency-plugin.version>
|
||||
<wildfly-ejb.version>8.2.1.Final</wildfly-ejb.version>
|
||||
<joda-time.version>2.10.12</joda-time.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.ejb.stateful;
|
||||
|
||||
import javax.ejb.EJB;
|
||||
|
||||
public class EJBClient1 {
|
||||
|
||||
@EJB
|
||||
public StatefulEJB statefulEJB;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.ejb.stateful;
|
||||
|
||||
import javax.ejb.EJB;
|
||||
|
||||
public class EJBClient2 {
|
||||
|
||||
@EJB
|
||||
public StatefulEJB statefulEJB;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.ejb.stateful;
|
||||
|
||||
import javax.ejb.Stateful;
|
||||
|
||||
@Stateful
|
||||
public class StatefulEJB {
|
||||
|
||||
public String name;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.ejb.stateless;
|
||||
|
||||
import javax.ejb.EJB;
|
||||
|
||||
public class EJBClient1 {
|
||||
|
||||
@EJB
|
||||
public StatelessEJB statelessEJB;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.ejb.stateless;
|
||||
|
||||
import javax.ejb.EJB;
|
||||
|
||||
public class EJBClient2 {
|
||||
|
||||
@EJB
|
||||
public StatelessEJB statelessEJB;
|
||||
|
||||
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.ejb.stateless;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
|
||||
|
||||
@Stateless
|
||||
public class StatelessEJB {
|
||||
|
||||
public String name;
|
||||
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package com.baeldung.ejbspringcomparison.ejb.messagedriven;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.ejb.ActivationConfigProperty;
|
||||
import javax.ejb.MessageDriven;
|
||||
import javax.jms.Connection;
|
||||
import javax.jms.ConnectionFactory;
|
||||
import javax.jms.DeliveryMode;
|
||||
import javax.jms.JMSException;
|
||||
import javax.jms.Message;
|
||||
import javax.jms.MessageListener;
|
||||
import javax.jms.MessageProducer;
|
||||
import javax.jms.Queue;
|
||||
import javax.jms.Session;
|
||||
import javax.jms.TextMessage;
|
||||
|
||||
@MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName = "destination", propertyValue = "myQueue"), @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") })
|
||||
public class RecieverMDB implements MessageListener {
|
||||
|
||||
@Resource
|
||||
private ConnectionFactory connectionFactory;
|
||||
|
||||
@Resource(name = "ackQueue")
|
||||
private Queue ackQueue;
|
||||
|
||||
public void onMessage(Message message) {
|
||||
try {
|
||||
|
||||
TextMessage textMessage = (TextMessage) message;
|
||||
String producerPing = textMessage.getText();
|
||||
|
||||
if (producerPing.equals("marco")) {
|
||||
acknowledge("polo");
|
||||
}
|
||||
} catch (JMSException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void acknowledge(String text) throws JMSException {
|
||||
|
||||
Connection connection = null;
|
||||
Session session = null;
|
||||
|
||||
try {
|
||||
connection = connectionFactory.createConnection();
|
||||
connection.start();
|
||||
|
||||
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
||||
|
||||
MessageProducer ackSender = session.createProducer(ackQueue);
|
||||
ackSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
|
||||
|
||||
TextMessage message = session.createTextMessage(text);
|
||||
|
||||
ackSender.send(message);
|
||||
} finally {
|
||||
session.close();
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.ejbspringcomparison.ejb.singleton;
|
||||
|
||||
import javax.ejb.Singleton;
|
||||
|
||||
@Singleton
|
||||
public class CounterEJB implements CounterEJBRemote {
|
||||
|
||||
private int count = 1;
|
||||
private String name;
|
||||
|
||||
public int count() {
|
||||
return count++;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.ejbspringcomparison.ejb.singleton;
|
||||
|
||||
import javax.ejb.Remote;
|
||||
|
||||
@Remote
|
||||
public interface CounterEJBRemote {
|
||||
int count();
|
||||
String getName();
|
||||
void setName(String name);
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.ejbspringcomparison.ejb.stateful;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.ejb.Stateful;
|
||||
|
||||
@Stateful
|
||||
public class ShoppingCartEJB implements ShoppingCartEJBRemote {
|
||||
private String name;
|
||||
private List<String> shoppingCart;
|
||||
|
||||
public ShoppingCartEJB() {
|
||||
shoppingCart = new ArrayList<String>();
|
||||
}
|
||||
|
||||
public void addItem(String item) {
|
||||
shoppingCart.add(item);
|
||||
}
|
||||
|
||||
public List<String> getItems() {
|
||||
return shoppingCart;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.ejbspringcomparison.ejb.stateful;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.ejb.Remote;
|
||||
|
||||
@Remote
|
||||
public interface ShoppingCartEJBRemote {
|
||||
|
||||
void addItem(String item);
|
||||
|
||||
List<String> getItems();
|
||||
|
||||
void setName(String name);
|
||||
|
||||
String getName();
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.ejbspringcomparison.ejb.stateless;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
|
||||
@Stateless
|
||||
public class FinderEJB implements FinderEJBRemote {
|
||||
|
||||
private Map<String, String> alphabet;
|
||||
|
||||
public FinderEJB() {
|
||||
alphabet = new HashMap<String, String>();
|
||||
alphabet.put("A", "Apple");
|
||||
alphabet.put("B", "Ball");
|
||||
alphabet.put("C", "Cat");
|
||||
alphabet.put("D", "Dog");
|
||||
}
|
||||
|
||||
public String search(String keyword) {
|
||||
return alphabet.get(keyword);
|
||||
}
|
||||
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.ejbspringcomparison.ejb.stateless;
|
||||
|
||||
import javax.ejb.Remote;
|
||||
|
||||
@Remote
|
||||
public interface FinderEJBRemote {
|
||||
|
||||
String search(String keyword);
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.ejbspringcomparison.spring.config;
|
||||
|
||||
import javax.jms.ConnectionFactory;
|
||||
|
||||
import org.apache.activemq.ActiveMQConnectionFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.jms.annotation.EnableJms;
|
||||
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
|
||||
import org.springframework.jms.core.JmsTemplate;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = "com.baeldung.ejbspringcomparison.spring")
|
||||
@EnableJms
|
||||
public class ApplicationConfig {
|
||||
|
||||
@Bean
|
||||
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
|
||||
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
|
||||
factory.setConnectionFactory(connectionFactory());
|
||||
return factory;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ConnectionFactory connectionFactory() {
|
||||
return new ActiveMQConnectionFactory("tcp://localhost:61616");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JmsTemplate jmsTemplate() {
|
||||
JmsTemplate template = new JmsTemplate(connectionFactory());
|
||||
template.setConnectionFactory(connectionFactory());
|
||||
return template;
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.ejbspringcomparison.spring.messagedriven;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jms.core.JmsTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class Producer {
|
||||
@Autowired
|
||||
private JmsTemplate jmsTemplate;
|
||||
|
||||
public void sendMessageToDefaultDestination(final String message) {
|
||||
jmsTemplate.convertAndSend("myQueue", message);
|
||||
}
|
||||
|
||||
public String receiveAck() {
|
||||
return (String) jmsTemplate.receiveAndConvert("ackQueue");
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.ejbspringcomparison.spring.messagedriven;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jms.annotation.JmsListener;
|
||||
import org.springframework.jms.core.JmsTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class Receiver {
|
||||
@Autowired
|
||||
private JmsTemplate jmsTemplate;
|
||||
|
||||
@JmsListener(destination = "myQueue")
|
||||
public void receiveMessage(String msg) {
|
||||
sendAck();
|
||||
}
|
||||
|
||||
private void sendAck() {
|
||||
jmsTemplate.convertAndSend("ackQueue", "polo");
|
||||
}
|
||||
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung.ejbspringcomparison.spring.singleton;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class CounterBean {
|
||||
private int count = 1;
|
||||
private String name;
|
||||
|
||||
public int count() {
|
||||
return count++;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.baeldung.ejbspringcomparison.spring.stateful;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
||||
public class ShoppingCartBean {
|
||||
|
||||
private String name;
|
||||
private List<String> shoppingCart;
|
||||
|
||||
public ShoppingCartBean() {
|
||||
shoppingCart = new ArrayList<String>();
|
||||
}
|
||||
|
||||
public void addItem(String item) {
|
||||
shoppingCart.add(item);
|
||||
}
|
||||
|
||||
public List<String> getItems() {
|
||||
return shoppingCart;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.singletonbean;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.ejb.Local;
|
||||
|
||||
@Local
|
||||
public interface CountryState {
|
||||
|
||||
public List<String> getStates(String country);
|
||||
|
||||
public void setStates(String country, List<String> states);
|
||||
|
||||
}
|
||||
+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.Singleton;
|
||||
import javax.ejb.Startup;
|
||||
|
||||
@Singleton
|
||||
@Startup
|
||||
@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
|
||||
public class CountryStateBeanManagedBean implements CountryState {
|
||||
|
||||
private final Map<String, List<String>> countryStatesMap = new HashMap<String, List<String>>();
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
public List<String> getStates(String country) {
|
||||
return countryStatesMap.get(country);
|
||||
}
|
||||
|
||||
public synchronized void setStates(String country, List<String> states) {
|
||||
countryStatesMap.put(country, states);
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
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 CountryStateContainerManagedBean 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);
|
||||
}
|
||||
|
||||
@Lock(LockType.WRITE)
|
||||
public void setStates(String country, List<String> states) {
|
||||
countryStatesMap.put(country, states);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.ejb.stateful;
|
||||
|
||||
import org.jboss.arquillian.container.test.api.Deployment;
|
||||
import org.jboss.arquillian.junit.Arquillian;
|
||||
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
|
||||
import org.jboss.shrinkwrap.api.spec.JavaArchive;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import com.baeldung.ejb.stateful.EJBClient1;
|
||||
import com.baeldung.ejb.stateful.EJBClient2;
|
||||
import com.baeldung.ejb.stateful.StatefulEJB;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
|
||||
@RunWith(Arquillian.class)
|
||||
public class StatefulEJBIntegrationTest {
|
||||
|
||||
@Inject
|
||||
private EJBClient1 ejbClient1;
|
||||
|
||||
@Inject
|
||||
private EJBClient2 ejbClient2;
|
||||
|
||||
@Test
|
||||
public void givenOneStatefulBean_whenTwoClientsSetValueOnBean_thenClientStateIsMaintained() {
|
||||
|
||||
// act
|
||||
ejbClient1.statefulEJB.name = "Client 1";
|
||||
ejbClient2.statefulEJB.name = "Client 2";
|
||||
|
||||
// assert
|
||||
Assert.assertNotEquals(ejbClient1.statefulEJB.name, ejbClient2.statefulEJB.name);
|
||||
Assert.assertEquals("Client 1", ejbClient1.statefulEJB.name);
|
||||
Assert.assertEquals("Client 2", ejbClient2.statefulEJB.name);
|
||||
|
||||
}
|
||||
|
||||
@Deployment
|
||||
public static JavaArchive createDeployment() {
|
||||
return ShrinkWrap.create(JavaArchive.class)
|
||||
.addClass(StatefulEJB.class)
|
||||
.addClass(EJBClient1.class)
|
||||
.addClass(EJBClient2.class)
|
||||
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
|
||||
}
|
||||
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package com.baeldung.ejb.stateless;
|
||||
|
||||
import org.jboss.arquillian.container.test.api.Deployment;
|
||||
import org.jboss.arquillian.junit.Arquillian;
|
||||
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
|
||||
import org.jboss.shrinkwrap.api.spec.JavaArchive;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import com.baeldung.ejb.stateless.EJBClient1;
|
||||
import com.baeldung.ejb.stateless.EJBClient2;
|
||||
import com.baeldung.ejb.stateless.StatelessEJB;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
|
||||
@RunWith(Arquillian.class)
|
||||
@Ignore("Will be fixed in BAEL-4422")
|
||||
public class StatelessEJBIntegrationTest {
|
||||
|
||||
@Inject
|
||||
private EJBClient1 ejbClient1;
|
||||
|
||||
@Inject
|
||||
private EJBClient2 ejbClient2;
|
||||
|
||||
@Test
|
||||
public void givenOneStatelessBean_whenStateIsSetInOneBean_secondBeanShouldHaveSameState() {
|
||||
|
||||
// act
|
||||
ejbClient1.statelessEJB.name = "Client 1";
|
||||
|
||||
// assert
|
||||
Assert.assertEquals("Client 1", ejbClient1.statelessEJB.name);
|
||||
Assert.assertEquals("Client 1", ejbClient2.statelessEJB.name);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void givenOneStatelessBean_whenStateIsSetInBothBeans_secondBeanShouldHaveSecondBeanState() {
|
||||
|
||||
// act
|
||||
ejbClient1.statelessEJB.name = "Client 1";
|
||||
ejbClient2.statelessEJB.name = "Client 2";
|
||||
|
||||
// assert
|
||||
Assert.assertEquals("Client 2", ejbClient2.statelessEJB.name);
|
||||
|
||||
}
|
||||
|
||||
@Deployment
|
||||
public static JavaArchive createDeployment() {
|
||||
return ShrinkWrap.create(JavaArchive.class)
|
||||
.addClass(StatelessEJB.class)
|
||||
.addClass(EJBClient1.class)
|
||||
.addClass(EJBClient2.class)
|
||||
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
|
||||
}
|
||||
|
||||
}
|
||||
+140
@@ -0,0 +1,140 @@
|
||||
package com.baeldung.ejbspringcomparison.ejb;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.ejb.EJB;
|
||||
import javax.ejb.embeddable.EJBContainer;
|
||||
import javax.jms.Connection;
|
||||
import javax.jms.ConnectionFactory;
|
||||
import javax.jms.JMSException;
|
||||
import javax.jms.MessageConsumer;
|
||||
import javax.jms.MessageProducer;
|
||||
import javax.jms.Queue;
|
||||
import javax.jms.Session;
|
||||
import javax.jms.TextMessage;
|
||||
import javax.naming.Context;
|
||||
import javax.naming.NamingException;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.ejbspringcomparison.ejb.singleton.CounterEJBRemote;
|
||||
import com.baeldung.ejbspringcomparison.ejb.stateful.ShoppingCartEJBRemote;
|
||||
import com.baeldung.ejbspringcomparison.ejb.stateless.FinderEJBRemote;
|
||||
|
||||
public class EJBUnitTest {
|
||||
|
||||
private static EJBContainer ejbContainer = null;
|
||||
|
||||
private static Context context = null;
|
||||
|
||||
@Resource
|
||||
private ConnectionFactory connectionFactory;
|
||||
|
||||
@EJB
|
||||
private FinderEJBRemote alphabetFinder;
|
||||
|
||||
@Resource(name = "myQueue")
|
||||
private Queue myQueue;
|
||||
|
||||
@Resource(name = "ackQueue")
|
||||
private Queue ackQueue;
|
||||
|
||||
@BeforeClass
|
||||
public static void start() throws NamingException {
|
||||
ejbContainer = EJBContainer.createEJBContainer();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void initializeContext() throws NamingException {
|
||||
context = ejbContainer.getContext();
|
||||
context.bind("inject", this);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSingletonBean_whenCounterInvoked_thenCountIsIncremented() throws NamingException {
|
||||
|
||||
int count = 0;
|
||||
CounterEJBRemote firstCounter = (CounterEJBRemote) context.lookup("java:global/ejb-beans/CounterEJB");
|
||||
firstCounter.setName("first");
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
count = firstCounter.count();
|
||||
}
|
||||
|
||||
assertEquals(10, count);
|
||||
assertEquals("first", firstCounter.getName());
|
||||
|
||||
CounterEJBRemote secondCounter = (CounterEJBRemote) context.lookup("java:global/ejb-beans/CounterEJB");
|
||||
|
||||
int count2 = 0;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
count2 = secondCounter.count();
|
||||
}
|
||||
|
||||
assertEquals(20, count2);
|
||||
assertEquals("first", secondCounter.getName());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStatefulBean_whenBathingCartWithThreeItemsAdded_thenItemsSizeIsThree() throws NamingException {
|
||||
ShoppingCartEJBRemote bathingCart = (ShoppingCartEJBRemote) context.lookup("java:global/ejb-beans/ShoppingCartEJB");
|
||||
|
||||
bathingCart.setName("bathingCart");
|
||||
|
||||
bathingCart.addItem("soap");
|
||||
bathingCart.addItem("shampoo");
|
||||
bathingCart.addItem("oil");
|
||||
|
||||
assertEquals(3, bathingCart.getItems()
|
||||
.size());
|
||||
assertEquals("bathingCart", bathingCart.getName());
|
||||
|
||||
ShoppingCartEJBRemote fruitCart = (ShoppingCartEJBRemote) context.lookup("java:global/ejb-beans/ShoppingCartEJB");
|
||||
|
||||
fruitCart.addItem("apples");
|
||||
fruitCart.addItem("oranges");
|
||||
|
||||
assertEquals(2, fruitCart.getItems()
|
||||
.size());
|
||||
assertNull(fruitCart.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStatelessBean_whenSearchForA_thenApple() throws NamingException {
|
||||
|
||||
assertEquals("Apple", alphabetFinder.search("A"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMDB_whenMessageSent_thenAcknowledgementReceived() throws InterruptedException, JMSException, NamingException {
|
||||
|
||||
Connection connection = connectionFactory.createConnection();
|
||||
connection.start();
|
||||
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
|
||||
MessageProducer producer = session.createProducer(myQueue);
|
||||
producer.send(session.createTextMessage("marco"));
|
||||
MessageConsumer response = session.createConsumer(ackQueue);
|
||||
|
||||
assertEquals("polo", ((TextMessage) response.receive(1000)).getText());
|
||||
|
||||
}
|
||||
|
||||
@After
|
||||
public void reset() throws NamingException {
|
||||
context.unbind("inject");
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void closeContext() throws NamingException {
|
||||
context.close();
|
||||
ejbContainer.close();
|
||||
}
|
||||
|
||||
}
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
package com.baeldung.ejbspringcomparison.spring;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
import javax.naming.NamingException;
|
||||
|
||||
import org.apache.activemq.junit.EmbeddedActiveMQBroker;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Assert;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
|
||||
import com.baeldung.ejbspringcomparison.spring.config.ApplicationConfig;
|
||||
import com.baeldung.ejbspringcomparison.spring.messagedriven.Producer;
|
||||
import com.baeldung.ejbspringcomparison.spring.singleton.CounterBean;
|
||||
import com.baeldung.ejbspringcomparison.spring.stateful.ShoppingCartBean;
|
||||
|
||||
public class SpringUnitTest {
|
||||
|
||||
private static AnnotationConfigApplicationContext context = null;
|
||||
|
||||
@ClassRule
|
||||
public static EmbeddedActiveMQBroker broker = new EmbeddedActiveMQBroker() {
|
||||
@Override
|
||||
protected void configure() {
|
||||
this.getBrokerService()
|
||||
.setUseJmx(true);
|
||||
try {
|
||||
this.getBrokerService()
|
||||
.addConnector("tcp://localhost:61616");
|
||||
} catch (Exception e) {
|
||||
Assert.fail(e.getMessage());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@BeforeClass
|
||||
public static void init() {
|
||||
context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCounterInvoked_thenCountIsIncremented() throws NamingException {
|
||||
|
||||
CounterBean firstCounter = context.getBean(CounterBean.class);
|
||||
firstCounter.setName("first");
|
||||
int count = 0;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
count = firstCounter.count();
|
||||
}
|
||||
|
||||
assertEquals(10, count);
|
||||
assertEquals("first", firstCounter.getName());
|
||||
|
||||
CounterBean secondCounter = context.getBean(CounterBean.class);
|
||||
|
||||
int count2 = 0;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
count2 = secondCounter.count();
|
||||
}
|
||||
|
||||
assertEquals(20, count2);
|
||||
assertEquals("first", secondCounter.getName());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBathingCartWithThreeItemsAdded_thenItemsSizeIsThree() throws NamingException {
|
||||
ShoppingCartBean bathingCart = context.getBean(ShoppingCartBean.class);
|
||||
|
||||
bathingCart.setName("bathingCart");
|
||||
|
||||
bathingCart.addItem("soap");
|
||||
bathingCart.addItem("shampoo");
|
||||
bathingCart.addItem("oil");
|
||||
|
||||
assertEquals(3, bathingCart.getItems()
|
||||
.size());
|
||||
|
||||
assertEquals("bathingCart", bathingCart.getName());
|
||||
|
||||
ShoppingCartBean fruitCart = context.getBean(ShoppingCartBean.class);
|
||||
|
||||
fruitCart.addItem("apples");
|
||||
fruitCart.addItem("oranges");
|
||||
|
||||
assertEquals(2, fruitCart.getItems()
|
||||
.size());
|
||||
assertNull(fruitCart.getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenJMSBean_whenMessageSent_thenAcknowledgementReceived() throws NamingException {
|
||||
Producer producer = context.getBean(Producer.class);
|
||||
producer.sendMessageToDefaultDestination("marco");
|
||||
|
||||
assertEquals("polo", producer.receiveAck());
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void closeContext() throws NamingException {
|
||||
context.close();
|
||||
}
|
||||
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package com.baeldung.singletonbean;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
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;
|
||||
|
||||
public class CountryStateCacheBeanUnitTest {
|
||||
|
||||
private EJBContainer ejbContainer = null;
|
||||
|
||||
private Context context = null;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
ejbContainer = EJBContainer.createEJBContainer();
|
||||
context = ejbContainer.getContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallGetStatesFromContainerManagedBean_ReturnsStatesForCountry() throws Exception {
|
||||
|
||||
String[] expectedStates = { "Texas", "Alabama", "Alaska", "Arizona", "Arkansas" };
|
||||
|
||||
CountryState countryStateBean = (CountryState) context.lookup("java:global/ejb-beans/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/ejb-beans/CountryStateBeanManagedBean");
|
||||
List<String> actualStates = countryStateBean.getStates("UnitedStates");
|
||||
assertNotNull(actualStates);
|
||||
assertArrayEquals(expectedStates, actualStates.toArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallSetStatesFromContainerManagedBean_SetsStatesForCountry() throws Exception {
|
||||
|
||||
String[] expectedStates = { "California", "Florida", "Hawaii", "Pennsylvania", "Michigan" };
|
||||
|
||||
CountryState countryStateBean = (CountryState) context.lookup("java:global/ejb-beans/CountryStateContainerManagedBean");
|
||||
countryStateBean.setStates("UnitedStates", Arrays.asList(expectedStates));
|
||||
|
||||
List<String> actualStates = countryStateBean.getStates("UnitedStates");
|
||||
assertNotNull(actualStates);
|
||||
assertArrayEquals(expectedStates, actualStates.toArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCallSetStatesFromBeanManagedBean_SetsStatesForCountry() throws Exception {
|
||||
|
||||
String[] expectedStates = { "California", "Florida", "Hawaii", "Pennsylvania", "Michigan" };
|
||||
|
||||
CountryState countryStateBean = (CountryState) context.lookup("java:global/ejb-beans/CountryStateBeanManagedBean");
|
||||
countryStateBean.setStates("UnitedStates", Arrays.asList(expectedStates));
|
||||
|
||||
List<String> actualStates = countryStateBean.getStates("UnitedStates");
|
||||
assertNotNull(actualStates);
|
||||
assertArrayEquals(expectedStates, actualStates.toArray());
|
||||
}
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (ejbContainer != null)
|
||||
ejbContainer.close();
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user