BAEL - 626 : EJB: Stateless and Stateful Session Beans(Initial Commit)

This commit is contained in:
Pritam Banerjee 2017-02-26 04:31:19 -08:00
parent db2b2d8929
commit 9c39406c16
13 changed files with 460 additions and 1 deletions

View File

@ -0,0 +1,54 @@
<?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

@ -0,0 +1,85 @@
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

@ -0,0 +1,88 @@
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

@ -0,0 +1,8 @@
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

@ -0,0 +1,17 @@
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

@ -0,0 +1,16 @@
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

@ -0,0 +1,95 @@
<?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>
<parent>
<groupId>com.baeldung.ejb</groupId>
<artifactId>ejb</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>ejb-session-beans</artifactId>
<packaging>ejb</packaging>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>${javaee-api.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<profiles>
<!-- mvn clean package cargo:run -->
<profile>
<id>wildfly-standalone</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>
</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>

View File

@ -0,0 +1,27 @@
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

@ -0,0 +1,14 @@
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

@ -0,0 +1,29 @@
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

@ -0,0 +1,13 @@
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,6 @@
<?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

@ -34,7 +34,12 @@
<version>1.0-SNAPSHOT</version>
<type>ejb</type>
</dependency>
<dependency>
<groupId>com.baeldung.ejb</groupId>
<artifactId>ejb-session-beans</artifactId>
<version>1.0-SNAPSHOT</version>
<type>ejb</type>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
@ -78,5 +83,7 @@
<modules>
<module>ejb-remote</module>
<module>ejb-client</module>
<module>ejb-session-beans</module>
<module>ejb-session-beans-client</module>
</modules>
</project>