session beans

This commit is contained in:
Pritam Banerjee 2017-05-18 23:56:44 -07:00
parent 9c39406c16
commit 0ebaefcb95
20 changed files with 301 additions and 433 deletions

View File

@ -1,54 +0,0 @@
<?xml version="1.0"?>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.baeldung.ejb</groupId>
<artifactId>ejb</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>ejb-session-beans-client</artifactId>
<name>EJB3 Client Maven</name>
<description>EJB3 Client Maven</description>
<properties>
<junit.version>4.12</junit.version>
<maven-surefire-plugin.version>2.19</maven-surefire-plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-ejb-client-bom</artifactId>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.baeldung.ejb</groupId>
<artifactId>ejb-session-beans</artifactId>
<type>ejb</type>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19</version>
<configuration>
<excludes>
<exclude>**/*EJBSetupTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,85 +0,0 @@
package com.baeldung.ejb.session.client;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.baeldung.ejb.stateful.beans.ItemStatefulRemote;
public class EJBStatefulClient {
public EJBStatefulClient() {
}
private Context context = null;
public Boolean getEJBRemoteMessage() {
EJBStatefulClient ejb = new EJBStatefulClient();
Boolean result = true;
try {
// 1. Obtaining Context
ejb.createInitialContext();
// 2. Generate JNDI Lookup name and caste
ItemStatefulRemote itemStatefulOne = ejb.lookup();
ItemStatefulRemote itemStatefulTwo = ejb.lookup();
itemStatefulOne.addItem("Book");
itemStatefulOne.addItem("Pen");
itemStatefulOne.addItem("Copy");
itemStatefulOne.addItem("Pencil");
result = itemStatefulOne.getItemList().equals(itemStatefulTwo.getItemList());
return result;
} catch (NamingException e) {
e.printStackTrace();
return false;
} finally {
try {
ejb.closeContext();
} catch (NamingException e) {
e.printStackTrace();
}
}
}
public ItemStatefulRemote lookup() throws NamingException {
// The app name is the EAR name of the deployed EJB without .ear suffix.
// Since we haven't deployed the application as a .ear, the app name for
// us will be an empty string
final String appName = "";
final String moduleName = "session-beans";
final String distinctName = "";
final String beanName = "ItemStatefulRemote";
final String viewClassName = ItemStatefulRemote.class.getName() + "?stateful";
final String toLookup = String.format("ejb:%s/%s/%s/%s!%s", appName, moduleName, distinctName, beanName,
viewClassName);
return (ItemStatefulRemote) context.lookup(toLookup);
}
public void createInitialContext() throws NamingException {
Properties prop = new Properties();
prop.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
prop.put(Context.PROVIDER_URL, "http-remoting://127.0.0.1:8080");
prop.put(Context.SECURITY_PRINCIPAL, "testUser");
prop.put(Context.SECURITY_CREDENTIALS, "admin1234!");
prop.put("jboss.naming.client.ejb.context", false);
context = new InitialContext(prop);
}
public void closeContext() throws NamingException {
if (context != null) {
context.close();
}
}
}

View File

@ -1,88 +0,0 @@
package com.baeldung.ejb.session.client;
import java.util.List;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.baeldung.ejb.stateless.beans.ItemStatelessRemote;
public class EJBStatelessClient {
public EJBStatelessClient() {
}
private Context context = null;
public static void main(String[] arg) {
EJBStatelessClient ejb = new EJBStatelessClient();
System.out.println(ejb.getEJBRemoteMessage());
}
public Boolean getEJBRemoteMessage() {
EJBStatelessClient main = new EJBStatelessClient();
Boolean result = true;
try {
// 1. Obtaining Context
main.createInitialContext();
// 2. Generate JNDI Lookup name and caste
ItemStatelessRemote itemStatelessOne = main.lookup();
ItemStatelessRemote itemStatelessTwo = main.lookup();
itemStatelessOne.addItem("Book");
itemStatelessOne.addItem("Pen");
itemStatelessOne.addItem("Pencil");
itemStatelessOne.addItem("Eraser");
result = itemStatelessOne.getItemList().equals(itemStatelessTwo.getItemList());
return result;
} catch (NamingException e) {
e.printStackTrace();
return false;
} finally {
try {
main.closeContext();
} catch (NamingException e) {
e.printStackTrace();
}
}
}
public ItemStatelessRemote lookup() throws NamingException {
// The app name is the EAR name of the deployed EJB without .ear suffix.
// Since we haven't deployed the application as a .ear, the app name for
// us will be an empty string
final String appName = "";
final String moduleName = "session-beans";
final String distinctName = "";
final String beanName = "ItemStatelessRemote";
final String viewClassName = ItemStatelessRemote.class.getName();
final String toLookup = String.format("ejb:%s/%s/%s/%s!%s", appName, moduleName, distinctName, beanName, viewClassName);
return (ItemStatelessRemote) context.lookup(toLookup);
}
public void createInitialContext() throws NamingException {
Properties prop = new Properties();
prop.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
prop.put(Context.PROVIDER_URL, "http-remoting://127.0.0.1:8080");
prop.put(Context.SECURITY_PRINCIPAL, "testUser");
prop.put(Context.SECURITY_CREDENTIALS, "admin1234!");
prop.put("jboss.naming.client.ejb.context", false);
context = new InitialContext(prop);
}
public void closeContext() throws NamingException {
if (context != null) {
context.close();
}
}
}

View File

@ -1,8 +0,0 @@
remote.connections=default
remote.connection.default.host=127.0.0.1
remote.connection.default.port=8080
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT=false
remote.connection.default.connect.options.org.xnio.Options.SASL_DISALLOWED_MECHANISMS=${host.auth:JBOSS-LOCAL-USER}
remote.connection.default.username=testUser
remote.connection.default.password=admin1234!

View File

@ -1,17 +0,0 @@
package com.baeldung.ejb.session.client.test;
import static org.junit.Assert.*;
import org.junit.Test;
import com.baeldung.ejb.session.client.EJBStatefulClient;
public class EJBStatefulClientTest {
@Test
public void EJBClientTest() {
EJBStatefulClient ejbStatefulClient = new EJBStatefulClient();
assertFalse(ejbStatefulClient.getEJBRemoteMessage());
}
}

View File

@ -1,16 +0,0 @@
package com.baeldung.ejb.session.client.test;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import com.baeldung.ejb.session.client.EJBStatelessClient;
public class EJBStatelessClientTest {
@Test
public void EJBClientTest() {
EJBStatelessClient ejbStatelessClient = new EJBStatelessClient();
assertTrue(ejbStatelessClient.getEJBRemoteMessage());
}
}

View File

@ -1,95 +1,106 @@
<?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>
<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>
<parent>
<groupId>com.baeldung.ejb</groupId>
<artifactId>ejb</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>ejb-session-beans</artifactId>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<arquillian-bom.version>1.1.12.Final</arquillian-bom.version>
<shrinkwrap-resolver-impl-maven.version>2.2.6</shrinkwrap-resolver-impl-maven.version>
<arquillian-junit-container.version>1.1.12.Final</arquillian-junit-container.version>
<arquillian-glassfish-embedded-3.1.version>1.0.0.Final</arquillian-glassfish-embedded-3.1.version>
<junit.version>4.12</junit.version>
<javaee-api.version>7.0</javaee-api.version>
</properties>
<parent>
<groupId>com.baeldung.ejb</groupId>
<artifactId>ejb</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>ejb-session-beans</artifactId>
<packaging>ejb</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.1.13.Final</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>${javaee-api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<!-- mvn clean package cargo:run -->
<profile>
<id>wildfly-standalone</id>
<id>arquillian-glassfish-embedded</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>${cargo-maven2-plugin.version}</version>
<configuration>
<container>
<containerId>wildfly10x</containerId>
<zipUrlInstaller>
<url>http://download.jboss.org/wildfly/10.1.0.Final/wildfly-10.1.0.Final.zip</url>
</zipUrlInstaller>
</container>
<configuration>
<properties>
<cargo.hostname>127.0.0.1</cargo.hostname>
<cargo.jboss.management-http.port>9990</cargo.jboss.management-http.port>
<cargo.servlet.users>testUser:admin1234!</cargo.servlet.users>
</properties>
</configuration>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-glassfish-embedded-3.1</artifactId>
<version>1.0.0.CR4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</profile>
<!--mvn clean install wildfly:deploy -Pwildfly-runtime-->
<profile>
<id>wildfly-runtime</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>1.1.0.Alpha5</version>
<configuration>
<hostname>127.0.0.1</hostname>
<port>9990</port>
<username>testUser</username>
<password>admin1234!</password>
<filename>${build.finalName}.jar</filename>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<properties>
<javaee-api.version>7.0</javaee-api.version>
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
</properties>
</project>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
</plugin>
</plugins>
</build>
</project>

View File

@ -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;
}
}

View File

@ -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();
}

View File

@ -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;
}
}

View File

@ -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();
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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>

View File

@ -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");
}
}

View File

@ -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");
}
}