BAEL-3592: Comparison of Spring Beans and Java Enterprise Beans (#8750)
This commit is contained in:
parent
a223680945
commit
0ae112cf3e
|
@ -37,6 +37,42 @@
|
|||
<artifactId>tomee-embedded</artifactId>
|
||||
<version>${tomee-embedded.version}</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>
|
||||
|
@ -81,6 +117,10 @@
|
|||
<tomee-embedded.version>1.7.5</tomee-embedded.version>
|
||||
<glassfish-embedded-all.version>3.1.2</glassfish-embedded-all.version>
|
||||
<arquillian-glassfish-embedded-3.1.version>1.0.0.CR4</arquillian-glassfish-embedded-3.1.version>
|
||||
<javax.ejb-api.version>3.2</javax.ejb-api.version>
|
||||
<springframework.version>5.2.3.RELEASE</springframework.version>
|
||||
<activemq.broker.version>5.10.2</activemq.broker.version>
|
||||
<activemq.junit.version>5.13.1</activemq.junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
package com.baeldung.ejb.spring.comparison.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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.ejb.spring.comparison.ejb.singleton;
|
||||
|
||||
import javax.ejb.Singleton;
|
||||
|
||||
@Singleton
|
||||
public class CounterEJB implements CounterEJBRemote {
|
||||
|
||||
private int count = 1;
|
||||
|
||||
public int count() {
|
||||
return count++;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.ejb.spring.comparison.ejb.singleton;
|
||||
|
||||
import javax.ejb.Remote;
|
||||
|
||||
@Remote
|
||||
public interface CounterEJBRemote {
|
||||
int count();
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.ejb.spring.comparison.ejb.stateful;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.ejb.Stateful;
|
||||
|
||||
@Stateful
|
||||
public class ShoppingCartEJB implements ShoppingCartEJBRemote {
|
||||
|
||||
private List<String> shoppingCart;
|
||||
|
||||
public ShoppingCartEJB() {
|
||||
shoppingCart = new ArrayList<String>();
|
||||
}
|
||||
|
||||
public void addItem(String item) {
|
||||
shoppingCart.add(item);
|
||||
}
|
||||
|
||||
public List<String> getItems() {
|
||||
return shoppingCart;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.ejb.spring.comparison.ejb.stateful;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.ejb.Remote;
|
||||
|
||||
@Remote
|
||||
public interface ShoppingCartEJBRemote {
|
||||
|
||||
void addItem(String item);
|
||||
|
||||
List<String> getItems();
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.ejb.spring.comparison.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);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.ejb.spring.comparison.ejb.stateless;
|
||||
|
||||
import javax.ejb.Remote;
|
||||
|
||||
@Remote
|
||||
public interface FinderEJBRemote {
|
||||
|
||||
String search(String keyword);
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.ejb.spring.comparison.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.ejb.spring.comparison.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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.ejb.spring.comparison.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");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.ejb.spring.comparison.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");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.ejb.spring.comparison.spring.singleton;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class CounterBean {
|
||||
private int count = 1;
|
||||
|
||||
public int count() {
|
||||
return count++;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.ejb.spring.comparison.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 List<String> shoppingCart;
|
||||
|
||||
public ShoppingCartBean() {
|
||||
shoppingCart = new ArrayList<String>();
|
||||
}
|
||||
|
||||
public void addItem(String item) {
|
||||
shoppingCart.add(item);
|
||||
}
|
||||
|
||||
public List<String> getItems() {
|
||||
return shoppingCart;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,142 @@
|
|||
package com.baeldung.ejb.spring.comparison.ejb;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.not;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
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.ejb.spring.comparison.ejb.singleton.CounterEJBRemote;
|
||||
import com.baeldung.ejb.spring.comparison.ejb.stateful.ShoppingCartEJBRemote;
|
||||
import com.baeldung.ejb.spring.comparison.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 counterEJB = (CounterEJBRemote) context.lookup("java:global/ejb-beans/CounterEJB");
|
||||
|
||||
for (int i = 0; i < 10; i++)
|
||||
count = counterEJB.count();
|
||||
|
||||
assertThat(count, is(not(1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSingletonBean_whenCounterInvokedAgain_thenCountIsIncremented() throws NamingException {
|
||||
|
||||
CounterEJBRemote counterEJB = (CounterEJBRemote) context.lookup("java:global/ejb-beans/CounterEJB");
|
||||
|
||||
int count = 0;
|
||||
for (int i = 0; i < 10; i++)
|
||||
count = counterEJB.count();
|
||||
|
||||
assertThat(count, is(not(1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStatefulBean_whenBathingCartWithThreeItemsAdded_thenItemsSizeIsThree() throws NamingException {
|
||||
ShoppingCartEJBRemote bathingCart = (ShoppingCartEJBRemote) context.lookup("java:global/ejb-beans/ShoppingCartEJB");
|
||||
|
||||
bathingCart.addItem("soap");
|
||||
bathingCart.addItem("shampoo");
|
||||
bathingCart.addItem("oil");
|
||||
|
||||
assertEquals(3, bathingCart.getItems()
|
||||
.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStatefulBean_whenFruitCartWithTwoItemsAdded_thenItemsSizeIsTwo() throws NamingException {
|
||||
ShoppingCartEJBRemote fruitCart = (ShoppingCartEJBRemote) context.lookup("java:global/ejb-beans/ShoppingCartEJB");
|
||||
|
||||
fruitCart.addItem("apples");
|
||||
fruitCart.addItem("oranges");
|
||||
|
||||
assertEquals(2, fruitCart.getItems()
|
||||
.size());
|
||||
}
|
||||
|
||||
@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 checkTotalCountAndcloseContext() throws NamingException {
|
||||
CounterEJBRemote counterEJB = (CounterEJBRemote) context.lookup("java:global/ejb-beans/CounterEJB");
|
||||
assertEquals(21, counterEJB.count());
|
||||
|
||||
context.close();
|
||||
ejbContainer.close();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,108 @@
|
|||
package com.baeldung.ejb.spring.comparison.spring;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.not;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
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.ejb.spring.comparison.spring.config.ApplicationConfig;
|
||||
import com.baeldung.ejb.spring.comparison.spring.messagedriven.Producer;
|
||||
import com.baeldung.ejb.spring.comparison.spring.singleton.CounterBean;
|
||||
import com.baeldung.ejb.spring.comparison.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 counterBean = context.getBean(CounterBean.class);
|
||||
|
||||
int count = 0;
|
||||
for (int i = 0; i < 10; i++)
|
||||
count = counterBean.count();
|
||||
|
||||
assertThat(count, is(not(1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCounterInvokedAgain_thenCountIsIncremented() throws NamingException {
|
||||
CounterBean counterBean = context.getBean(CounterBean.class);
|
||||
|
||||
int count = 0;
|
||||
for (int i = 0; i < 10; i++)
|
||||
count = counterBean.count();
|
||||
|
||||
assertThat(count, is(not(1)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBathingCartWithThreeItemsAdded_thenItemsSizeIsThree() throws NamingException {
|
||||
ShoppingCartBean bathingCart = context.getBean(ShoppingCartBean.class);
|
||||
|
||||
bathingCart.addItem("soap");
|
||||
bathingCart.addItem("shampoo");
|
||||
bathingCart.addItem("oil");
|
||||
|
||||
assertEquals(3, bathingCart.getItems()
|
||||
.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFruitCartWithTwoItemsAdded_thenItemsSizeIsTwo() throws NamingException {
|
||||
ShoppingCartBean fruitCart = context.getBean(ShoppingCartBean.class);
|
||||
|
||||
fruitCart.addItem("apples");
|
||||
fruitCart.addItem("oranges");
|
||||
|
||||
assertEquals(2, fruitCart.getItems()
|
||||
.size());
|
||||
}
|
||||
|
||||
@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 checkTotalCountAndcloseContext() throws NamingException {
|
||||
CounterBean counterBean = context.getBean(CounterBean.class);
|
||||
int count = counterBean.count();
|
||||
assertEquals(21, count);
|
||||
context.close();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue