session beans
This commit is contained in:
@@ -1,27 +0,0 @@
|
||||
package com.baeldung.ejb.stateful.beans;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.ejb.Stateful;
|
||||
|
||||
@Stateful(name = "ItemStatefulRemote")
|
||||
public class ItemStateful implements ItemStatefulRemote {
|
||||
|
||||
private List<String> itemList;
|
||||
|
||||
public ItemStateful() {
|
||||
itemList = new ArrayList<String>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addItem(String itemName) {
|
||||
itemList.add(itemName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getItemList() {
|
||||
return itemList;
|
||||
}
|
||||
|
||||
}
|
||||
-14
@@ -1,14 +0,0 @@
|
||||
package com.baeldung.ejb.stateful.beans;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.ejb.Remote;
|
||||
|
||||
@Remote
|
||||
public interface ItemStatefulRemote {
|
||||
|
||||
void addItem(String itemName);
|
||||
|
||||
List<String> getItemList();
|
||||
|
||||
}
|
||||
-29
@@ -1,29 +0,0 @@
|
||||
package com.baeldung.ejb.stateless.beans;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
|
||||
@Stateless(name = "ItemStatelessRemote")
|
||||
public class ItemStateless implements ItemStatelessRemote {
|
||||
|
||||
private List<String> itemList;
|
||||
|
||||
public ItemStateless() {
|
||||
itemList = new ArrayList<String>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addItem(String itemName) {
|
||||
|
||||
itemList.add(itemName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getItemList() {
|
||||
|
||||
return itemList;
|
||||
}
|
||||
|
||||
}
|
||||
-13
@@ -1,13 +0,0 @@
|
||||
package com.baeldung.ejb.stateless.beans;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.ejb.Remote;
|
||||
|
||||
@Remote
|
||||
public interface ItemStatelessRemote {
|
||||
|
||||
void addItem(String itemName);
|
||||
|
||||
List<String> getItemList();
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.readlearncode.stateful;
|
||||
|
||||
import javax.ejb.EJB;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class EJBClient1 {
|
||||
|
||||
@EJB
|
||||
public StatefulEJB statefulEJB;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.readlearncode.stateful;
|
||||
|
||||
import javax.ejb.EJB;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class EJBClient2 {
|
||||
|
||||
@EJB
|
||||
public StatefulEJB statefulEJB;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.readlearncode.stateful;
|
||||
|
||||
import javax.ejb.Stateful;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
@Stateful
|
||||
public class StatefulEJB {
|
||||
|
||||
public String name;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.readlearncode.stateless;
|
||||
|
||||
import javax.ejb.EJB;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class EJBClient1 {
|
||||
|
||||
@EJB
|
||||
public StatelessEJB statelessEJB;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.readlearncode.stateless;
|
||||
|
||||
import javax.ejb.EJB;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
public class EJBClient2 {
|
||||
|
||||
@EJB
|
||||
public StatelessEJB statelessEJB;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.readlearncode.stateless;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
@Stateless
|
||||
public class StatelessEJB {
|
||||
|
||||
public String name;
|
||||
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ejb-jar xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/ejb-jar_3_2.xsd"
|
||||
version="3.2">
|
||||
<module-name>session-beans</module-name>
|
||||
</ejb-jar>
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.readlearncode.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 javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
@RunWith(Arquillian.class)
|
||||
public class StatefulEJBTest {
|
||||
|
||||
@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");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.readlearncode.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.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Source code github.com/readlearncode
|
||||
*
|
||||
* @author Alex Theedom www.readlearncode.com
|
||||
* @version 1.0
|
||||
*/
|
||||
@RunWith(Arquillian.class)
|
||||
public class StatelessEJBTest {
|
||||
|
||||
@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");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user