BAEL-8874 Merge ejb projects

-Merged ejb and spring-ejb modules into spring-ejb
This commit is contained in:
Dhawal Kapil
2018-11-13 19:45:52 +05:30
parent aa99f4afa8
commit 93f505f9a9
63 changed files with 91 additions and 372 deletions

View File

@@ -1,4 +1,8 @@
### Relevant Articles
- [Guide to EJB Set-up](http://www.baeldung.com/ejb-intro)
- [Java EE Session Beans](http://www.baeldung.com/ejb-session-beans)
- [Introduction to EJB JNDI Lookup on WildFly Application Server](http://www.baeldung.com/wildfly-ejb-jndi)
- [A Guide to Message Driven Beans in EJB](http://www.baeldung.com/ejb-message-driven-beans)
- [Integration Guide for Spring and EJB](http://www.baeldung.com/spring-ejb)
- [Singleton Session Bean in Java EE](http://www.baeldung.com/java-ee-singleton-session-bean)

View File

@@ -3,21 +3,31 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.singletonsession</groupId>
<artifactId>ejb-beans</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>EJB Beans</name>
<name>spring-ejb-beans</name>
<parent>
<groupId>com.baeldung.spring.ejb</groupId>
<artifactId>spring-ejb</artifactId>
<version>1.0.1</version>
<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>
<version>${javaee.version}</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.openejb/tomee-embedded -->
@@ -26,10 +36,49 @@
<artifactId>tomee-embedded</artifactId>
<version>${tomee-embedded.version}</version>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit</groupId>
<artifactId>arquillian-junit-container</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<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.1.13.Final</arquillian-bom.version>
<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>
</properties>
</project>

View File

@@ -0,0 +1,11 @@
package com.baeldung.ejb.stateful;
import javax.ejb.EJB;
public class EJBClient1 {
@EJB
public StatefulEJB statefulEJB;
}

View File

@@ -0,0 +1,11 @@
package com.baeldung.ejb.stateful;
import javax.ejb.EJB;
public class EJBClient2 {
@EJB
public StatefulEJB statefulEJB;
}

View File

@@ -0,0 +1,10 @@
package com.baeldung.ejb.stateful;
import javax.ejb.Stateful;
@Stateful
public class StatefulEJB {
public String name;
}

View File

@@ -0,0 +1,10 @@
package com.baeldung.ejb.stateless;
import javax.ejb.EJB;
public class EJBClient1 {
@EJB
public StatelessEJB statelessEJB;
}

View File

@@ -0,0 +1,11 @@
package com.baeldung.ejb.stateless;
import javax.ejb.EJB;
public class EJBClient2 {
@EJB
public StatelessEJB statelessEJB;
}

View File

@@ -0,0 +1,11 @@
package com.baeldung.ejb.stateless;
import javax.ejb.Stateless;
@Stateless
public class StatelessEJB {
public String name;
}

View File

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

View File

@@ -0,0 +1,62 @@
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.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)
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");
}
}

View File

@@ -4,7 +4,6 @@ import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertNotNull;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.ejb.embeddable.EJBContainer;
@@ -22,7 +21,6 @@ public class CountryStateCacheBeanUnitTest {
@Before
public void init() {
ejbContainer = EJBContainer.createEJBContainer();
context = ejbContainer.getContext();
}

View File

@@ -4,7 +4,6 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.spring.ejb</groupId>
<artifactId>spring-ejb</artifactId>
<version>1.0.1</version>
<packaging>pom</packaging>
<name>spring-ejb</name>
<description>Spring EJB Tutorial</description>
@@ -36,8 +35,8 @@
<dependencies>
<dependency>
<groupId>com.baeldung.spring.ejb</groupId>
<artifactId>ejb-remote-for-spring</artifactId>
<version>${ejb-remote-for-spring.version}</version>
<artifactId>spring-ejb-remote</artifactId>
<version>${spring-ejb-remote.version}</version>
<type>ejb</type>
</dependency>
<dependency>
@@ -71,13 +70,14 @@
</build>
<modules>
<module>ejb-remote-for-spring</module>
<module>spring-ejb-remote</module>
<module>ejb-beans</module>
<module>spring-ejb-client</module>
<module>wildfly</module>
</modules>
<properties>
<ejb-remote-for-spring.version>1.0.1</ejb-remote-for-spring.version>
<spring-ejb-remote.version>1.0.0-SNAPSHOT</spring-ejb-remote.version>
<javaee.version>8.0</javaee.version>
<wildfly-ejb.version>12.0.0.Final</wildfly-ejb.version>
<maven-ejb-plugin.version>2.4</maven-ejb-plugin.version>

View File

@@ -12,7 +12,7 @@
<parent>
<groupId>com.baeldung.spring.ejb</groupId>
<artifactId>spring-ejb</artifactId>
<version>1.0.1</version>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencyManagement>
@@ -36,14 +36,12 @@
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-ejb-client-bom</artifactId>
<version>${wildfly-ejb.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.baeldung.spring.ejb</groupId>
<artifactId>ejb-remote-for-spring</artifactId>
<version>${ejb-remote-for-spring.version}</version>
<artifactId>spring-ejb-remote</artifactId>
<type>ejb</type>
</dependency>
@@ -70,9 +68,4 @@
</plugins>
</build>
<properties>
<ejb-remote-for-spring.version>1.0.1</ejb-remote-for-spring.version>
<wildfly-ejb.version>12.0.0.Final</wildfly-ejb.version>
</properties>
</project>

View File

@@ -0,0 +1,70 @@
package com.baeldung.ejb.client;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.baeldung.ejb.tutorial.HelloWorld;
public class EJBClient {
public EJBClient() {
}
private Context context = null;
public String getEJBRemoteMessage() {
EJBClient main = new EJBClient();
try {
// 1. Obtaining Context
main.createInitialContext();
// 2. Generate JNDI Lookup name and caste
HelloWorld helloWorld = main.lookup();
return helloWorld.getHelloWorld();
} catch (NamingException e) {
e.printStackTrace();
return "";
} finally {
try {
main.closeContext();
} catch (NamingException e) {
e.printStackTrace();
}
}
}
public HelloWorld 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 = "spring-ejb-remote";
final String distinctName = "";
final String beanName = "HelloWorld";
final String viewClassName = HelloWorld.class.getName();
final String toLookup = String.format("ejb:%s/%s/%s/%s!%s", appName, moduleName, distinctName, beanName, viewClassName);
return (HelloWorld) 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,40 @@
package com.baeldung.ejb.wildfly;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Properties;
public class TextApplication {
public static void main(String[] args) throws NamingException {
TextProcessorRemote textProcessor = EJBFactory.createTextProcessorBeanFromJNDI("ejb:");
System.out.print(textProcessor.processText("sample text"));
}
private static class EJBFactory {
private static TextProcessorRemote createTextProcessorBeanFromJNDI(String namespace) throws NamingException {
return lookupTextProcessorBean(namespace);
}
private static TextProcessorRemote lookupTextProcessorBean(String namespace) throws NamingException {
Context ctx = createInitialContext();
final String appName = "";
final String moduleName = "spring-ejb-remote";
final String distinctName = "";
final String beanName = TextProcessorBean.class.getSimpleName();
final String viewClassName = TextProcessorRemote.class.getName();
return (TextProcessorRemote) ctx.lookup(namespace + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName);
}
private static Context createInitialContext() throws NamingException {
Properties jndiProperties = new Properties();
jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
jndiProperties.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");
jndiProperties.put("jboss.naming.client.ejb.context", true);
return new InitialContext(jndiProperties);
}
}
}

View File

@@ -37,7 +37,7 @@ public class SpringEjbClientApplication {
@SuppressWarnings("rawtypes")
private String getFullName(Class classType) {
String moduleName = "ejb-remote-for-spring/";
String moduleName = "spring-ejb-remote/";
String beanName = classType.getSimpleName();
String viewClassName = classType.getName();

View File

@@ -0,0 +1,8 @@
endpoint.name=client-endpoint
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
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.username=myusername
remote.connection.default.password=mypassword

View File

@@ -0,0 +1,18 @@
package com.baeldung.ejb.setup.test;
import com.baeldung.ejb.client.EJBClient;
import com.baeldung.ejb.tutorial.HelloWorldBean;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class EJBSetupIntegrationTest {
@Test
public void EJBClientTest() {
EJBClient ejbClient = new EJBClient();
HelloWorldBean bean = new HelloWorldBean();
assertEquals(bean.getHelloWorld(), ejbClient.getEJBRemoteMessage());
}
}

View File

@@ -0,0 +1,31 @@
package com.baeldung.ejb.wildfly;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import javax.naming.NamingException;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import static org.junit.Assert.*;
public class TextApplicationIntegrationTest {
private static ByteArrayOutputStream outContent;
@BeforeClass
public static void setUpPrintStreamInstance() {
outContent = new ByteArrayOutputStream();
System.setOut(new PrintStream(outContent));
}
@AfterClass
public static void tearDownByteArrayOutputStream() {
outContent = null;
}
@Test
public void givenInputString_whenCompareTtoStringPrintedToConsole_thenSuccessful() throws NamingException {
TextApplication.main(new String[]{});
assertEquals("SAMPLE TEXT", outContent.toString());
}
}

View File

@@ -1,14 +1,14 @@
<?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">
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>
<artifactId>ejb-remote-for-spring</artifactId>
<artifactId>spring-ejb-remote</artifactId>
<packaging>ejb</packaging>
<parent>
<groupId>com.baeldung.spring.ejb</groupId>
<artifactId>spring-ejb</artifactId>
<version>1.0.1</version>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
@@ -63,13 +63,36 @@
</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>${wildfly-maven-plugin.version}</version>
<configuration>
<hostname>127.0.0.1</hostname>
<port>9990</port>
<username>testUser</username>
<password>admin1234!</password>
<filename>${project.build.finalName}.jar</filename>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<properties>
<assertj.version>3.9.0</assertj.version>
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
<wildfly-maven-plugin.version>1.1.0.Alpha5</wildfly-maven-plugin.version>
</properties>
</project>
</project>

View File

@@ -0,0 +1,8 @@
package com.baeldung.ejb.tutorial;
import javax.ejb.Remote;
@Remote
public interface HelloWorld {
String getHelloWorld();
}

View File

@@ -0,0 +1,19 @@
package com.baeldung.ejb.tutorial;
import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
@Stateless(name = "HelloWorld")
public class HelloWorldBean implements HelloWorld {
@Resource
private SessionContext context;
@Override
public String getHelloWorld() {
return "Welcome to EJB Tutorial!";
}
}

View File

@@ -0,0 +1,10 @@
package com.baeldung.ejb.wildfly;
import javax.ejb.Stateless;
@Stateless
public class TextProcessorBean implements TextProcessorRemote {
public String processText(String text) {
return text.toUpperCase();
}
}

View File

@@ -0,0 +1,9 @@
package com.baeldung.ejb.wildfly;
import javax.ejb.Remote;
@Remote
public interface TextProcessorRemote {
String processText(String text);
}

View File

@@ -2,6 +2,6 @@
<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>ejb-remote-for-spring</module-name>
<module-name>spring-ejb-remote</module-name>
</ejb-jar>

View File

@@ -0,0 +1,93 @@
<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.wildfly</groupId>
<artifactId>wildfly-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>wildfly-example</name>
<parent>
<groupId>com.baeldung.spring.ejb</groupId>
<artifactId>spring-ejb</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modules>
<module>widlfly-web</module>
<module>wildfly-ear</module>
<module>wildfly-jpa</module>
<module>wildfly-ejb-interfaces</module>
<module>wildfly-ejb</module>
</modules>
<dependencyManagement>
<dependencies>
<!-- Dependencies for other libraries -->
<!-- Dependency for java ee -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>${javaee-api.version}</version>
<scope>provided</scope>
</dependency>
<!-- Dependency for wildfly -->
<dependency>
<groupId>org.wildfly.bom</groupId>
<artifactId>wildfly-javaee7</artifactId>
<version>${wildfly-javaee7.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
<!-- Dependency on Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate-core.version}</version>
<scope>provided</scope>
</dependency>
<!-- Dependency for ear module -->
<dependency>
<groupId>com.baeldung.wildfly</groupId>
<artifactId>wildlfy-ear</artifactId>
<version>${wildlfy.version}</version>
<type>ear</type>
</dependency>
<!-- Dependency for web module -->
<dependency>
<groupId>com.baeldung.wildfly</groupId>
<artifactId>wildlfy-web</artifactId>
<version>${wildlfy.version}</version>
<type>war</type>
</dependency>
<!-- Dependency for jpa module -->
<dependency>
<groupId>com.baeldung.wildfly</groupId>
<artifactId>wildlfy-jpa</artifactId>
<version>${wildlfy.version}</version>
</dependency>
<!-- Dependency for EJB -->
<dependency>
<groupId>com.baeldung.wildfly</groupId>
<artifactId>wildfly-ejb</artifactId>
<version>${wildlfy.version}</version>
</dependency>
<!-- Dependency for EJB interfaces -->
<dependency>
<groupId>com.baeldung.wildfly</groupId>
<artifactId>wildfly-ejb-interfaces</artifactId>
<version>${wildlfy.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<javaee-api.version>7.0</javaee-api.version>
<wildfly-javaee7.version>10.1.0.Final</wildfly-javaee7.version>
<hibernate-core.version>5.2.3.Final</hibernate-core.version>
<wildlfy.version>0.0.1-SNAPSHOT</wildlfy.version>
</properties>
</project>

View File

@@ -0,0 +1,40 @@
<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>
<artifactId>widlfly-web</artifactId>
<packaging>war</packaging>
<parent>
<groupId>com.baeldung.wildfly</groupId>
<artifactId>wildfly-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<!-- Dependency for java ee -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>${javaee-api.version}</version>
<scope>provided</scope>
</dependency>
<!-- Dependency for JPA module -->
<dependency>
<groupId>com.baeldung.wildfly</groupId>
<artifactId>wildfly-jpa</artifactId>
<version>${wildlfy.version}</version>
</dependency>
<!-- Dependency for EJB interface module -->
<dependency>
<groupId>com.baeldung.wildfly</groupId>
<artifactId>wildfly-ejb-interfaces</artifactId>
<version>${wildlfy.version}</version>
</dependency>
<!-- Dependency for EJB interface module -->
<dependency>
<groupId>com.baeldung.wildfly</groupId>
<artifactId>wildfly-ejb</artifactId>
<version>${wildlfy.version}</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,41 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.User;
import wildfly.beans.UserBeanLocal;
/**
* Servlet implementation class TestEJBServlet
*/
public class TestEJBServlet extends HttpServlet {
@EJB
private UserBeanLocal userBean;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<User> users = userBean.getUsers();
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
for (User user : users) {
out.print(user.getUsername());
out.print(" " + user.getEmail() + " <br>");
}
out.println("</body>");
out.println("</html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}

View File

@@ -0,0 +1,54 @@
import java.io.IOException;
import java.util.List;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.UserTransaction;
import model.User;
/**
* Servlet implementation class TestJPAServlet
*/
public class TestJPAServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@PersistenceContext(unitName = "wildfly-jpa")
EntityManager em;
@Resource
UserTransaction tx;
/**
* @see HttpServlet#HttpServlet()
*/
public TestJPAServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Query q = em.createNamedQuery("User.findAll");
List<User> users = q.getResultList();
response.getWriter()
.append("JPA users returned: " + users.size());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}

View File

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

View File

@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>widlfly-web</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>TestJPAServlet</display-name>
<servlet-name>TestJPAServlet</servlet-name>
<servlet-class>TestJPAServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestJPAServlet</servlet-name>
<url-pattern>/TestJPAServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>TestEJBServlet</display-name>
<servlet-name>TestEJBServlet</servlet-name>
<servlet-class>TestEJBServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestEJBServlet</servlet-name>
<url-pattern>/TestEJBServlet</url-pattern>
</servlet-mapping>
</web-app>

View File

@@ -0,0 +1,68 @@
<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>
<artifactId>wildfly-ear</artifactId>
<packaging>ear</packaging>
<parent>
<groupId>com.baeldung.wildfly</groupId>
<artifactId>wildfly-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<!-- Dependency for web module -->
<dependency>
<groupId>com.baeldung.wildfly</groupId>
<artifactId>widlfly-web</artifactId>
<version>${wildlfy.version}</version>
<type>war</type>
</dependency>
<!-- Dependency for web module -->
<dependency>
<groupId>com.baeldung.wildfly</groupId>
<artifactId>wildfly-jpa</artifactId>
<version>${wildlfy.version}</version>
</dependency>
<!-- Dependency for EJB -->
<dependency>
<groupId>com.baeldung.wildfly</groupId>
<artifactId>wildfly-ejb</artifactId>
</dependency>
<!-- Dependency for EJB -->
<dependency>
<groupId>com.baeldung.wildfly</groupId>
<artifactId>wildfly-ejb-interfaces</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>${maven-ear-plugin.version}</version>
<configuration>
<defaultLibBundleDir>lib/</defaultLibBundleDir>
<version>7</version>
<modules>
<webModule>
<groupId>com.baeldung.wildfly</groupId>
<artifactId>widlfly-web</artifactId>
<contextRoot>/wildfly</contextRoot>
</webModule>
</modules>
</configuration>
</plugin>
<plugin>
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>${wildfly-maven-plugin.version}</version>
</plugin>
</plugins>
</build>
<properties>
<maven-ear-plugin.version>2.10.1</maven-ear-plugin.version>
<wildfly-maven-plugin.version>1.2.0.Final</wildfly-maven-plugin.version>
</properties>
</project>

View File

@@ -0,0 +1,32 @@
<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>
<artifactId>wildfly-ejb-interfaces</artifactId>
<parent>
<groupId>com.baeldung.wildfly</groupId>
<artifactId>wildfly-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<!-- Dependency for EJB javax -->
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>javax.ejb-api</artifactId>
<version>${javax.ejb-api.version}</version>
<scope>provided</scope>
</dependency>
<!-- Dependency for JPA module -->
<dependency>
<groupId>com.baeldung.wildfly</groupId>
<artifactId>wildfly-jpa</artifactId>
<version>${wildlfy.version}</version>
</dependency>
</dependencies>
<properties>
<javax.ejb-api.version>3.2</javax.ejb-api.version>
</properties>
</project>

View File

@@ -0,0 +1,13 @@
package wildfly.beans;
import java.util.List;
import javax.ejb.Local;
import model.User;
@Local
public interface UserBeanLocal {
List<User> getUsers();
}

View File

@@ -0,0 +1,13 @@
package wildfly.beans;
import java.util.List;
import javax.ejb.Remote;
import model.User;
@Remote
public interface UserBeanRemote {
List<User> getUsers();
}

View File

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

View File

@@ -0,0 +1,54 @@
<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>
<artifactId>wildfly-ejb</artifactId>
<packaging>ejb</packaging>
<parent>
<groupId>com.baeldung.wildfly</groupId>
<artifactId>wildfly-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<!-- Dependency for EJB javax -->
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>javax.ejb-api</artifactId>
<version>${ejb.version}</version>
<scope>provided</scope>
</dependency>
<!-- Dependency for java ee -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- Dependency for JPA -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
</dependency>
<!-- Dependency for EJB Local and Remote -->
<dependency>
<groupId>com.baeldung.wildfly</groupId>
<artifactId>wildfly-ejb-interfaces</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<configuration>
<ejbVersion>${ejb.version}</ejbVersion>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<ejb.version>3.2</ejb.version>
</properties>
</project>

View File

@@ -0,0 +1,24 @@
package wildfly.beans;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import model.User;
/**
* Session Bean implementation class UserBean
*/
@Stateless
public class UserBean implements UserBeanRemote, UserBeanLocal {
@PersistenceContext(unitName = "wildfly-jpa")
private EntityManager em;
@Override
public List<User> getUsers() {
return em.createNamedQuery("User.findAll")
.getResultList();
}
}

View File

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

View File

@@ -0,0 +1,22 @@
<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>
<artifactId>wildfly-jpa</artifactId>
<parent>
<groupId>com.baeldung.wildfly</groupId>
<artifactId>wildfly-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<!-- Dependencies for other libraries -->
<!-- Dependency for JPA -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,51 @@
package model;
import java.io.Serializable;
import javax.persistence.*;
/**
* The persistent class for the users database table.
*
*/
@Entity
@Table(name = "users")
@NamedQuery(name = "User.findAll", query = "SELECT u FROM User u")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String username;
private String email;
@Column(name = "postal_number")
private Integer postalNumber;
public User() {
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getPostalNumber() {
return this.postalNumber;
}
public void setPostalNumber(Integer postalNumber) {
this.postalNumber = postalNumber;
}
}

View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="wildfly-jpa" transaction-type="JTA">
<jta-data-source>java:/H2DS</jta-data-source>
<class>model.User</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="hibernate.hbm2ddl.import_files" value="data.sql" />
</properties>
</persistence-unit>
</persistence>

View File

@@ -0,0 +1 @@
INSERT INTO users (username, email, postal_number) VALUES ('user1', 'user1@baeldung.com', 1000), ('user2', 'user2@baeldung.com', 2);

View File

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

View File

@@ -0,0 +1,22 @@
<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>
<artifactId>wildfly-mdb</artifactId>
<name>wildfly-mdb</name>
<parent>
<groupId>com.baeldung.wildfly</groupId>
<artifactId>wildfly-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<!-- Dependency for java ee -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>${javaee-api.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,28 @@
package com.baeldung.wildfly.mdb;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
/**
* Message-Driven Bean implementation class for: ReadMessageMDB
*/
@MessageDriven(
activationConfig = {
@ActivationConfigProperty(propertyName = "destination", propertyValue = "tutorialQueue"),
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class ReadMessageMDB implements MessageListener {
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("Message received: " + textMessage.getText());
} catch (JMSException e) {
System.out.println("Error while trying to consume messages: " + e.getMessage());
}
}
}

View File

@@ -0,0 +1,52 @@
package baeldung.com.example.servlet;
import java.io.IOException;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/SendMessageServlet")
public class SendMessageServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String text = req.getParameter("text") != null ? req.getParameter("text") : "Hello World";
try (
Context ic = new InitialContext();
ConnectionFactory cf = (ConnectionFactory) ic.lookup("/ConnectionFactory");
Queue queue = (Queue) ic.lookup("queue/tutorialQueue");
Connection connection = cf.createConnection();
) {
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer publisher = session.createProducer(queue);
connection.start();
TextMessage message = session.createTextMessage(text);
publisher.send(message);
} catch (NamingException | JMSException e) {
res.getWriter().println("Error while trying to send <" + text + "> message: " + e.getMessage());
}
res.getWriter().println("Message sent: " + text);
}
}