From cf32e14a9bc8796facda3dbdb68135691491f8c5 Mon Sep 17 00:00:00 2001 From: Allan Vital Date: Sun, 7 Jan 2018 21:59:53 -0200 Subject: [PATCH] [BAEL-1218] Guide to Spring EJB Integration (#3266) * Evaluation article about types of bean injection in spring * BAEL-1319 Quick Guide on Data.sql and Schema.sql Files in Spring * Revert "Evaluation article about types of bean injection in spring" This reverts commit eb071171673e0b8fa2b7ecffdad86f596e5fb114. * BAEL-1218: adding spring-ejb and configuring ejb start and deploy * BAEL-1218: adding spring * BAEL-1218: wrapping all together * BAEL-1218: new spring-ejb module * BAEL-1218: tests and improvements * BAEL-1218: removing moved files from old article * BAEL-1218: code review requested changes * BAEL-1218: test methods nomenclature correction * BAEL-1418: removing tabs * BAEL-1218: removing tabs * BAEL-1218: correcting boot parent module path --- pom.xml | 1 + spring-ejb/ejb-remote-for-spring/pom.xml | 76 +++++++++++++ .../ejb/tutorial/HelloStatefulWorld.java | 11 ++ .../ejb/tutorial/HelloStatefulWorldBean.java | 19 ++++ .../ejb/tutorial/HelloStatelessWorld.java | 10 ++ .../ejb/tutorial/HelloStatelessWorldBean.java | 12 +++ .../src/main/resources/META-INF/ejb-jar.xml | 7 ++ .../HelloStatefulWorldTestUnitTest.java | 32 ++++++ .../HelloStatelessWorldTestUnitTest.java | 24 +++++ spring-ejb/pom.xml | 77 ++++++++++++++ spring-ejb/spring-ejb-client/pom.xml | 100 ++++++++++++++++++ .../SpringEjbClientApplication.java | 50 +++++++++ .../endpoint/HomeEndpoint.java | 30 ++++++ .../src/main/resources/application.properties | 3 + ...ngEjbClientApplicationIntegrationTest.java | 11 ++ 15 files changed, 463 insertions(+) create mode 100755 spring-ejb/ejb-remote-for-spring/pom.xml create mode 100644 spring-ejb/ejb-remote-for-spring/src/main/java/com/baeldung/ejb/tutorial/HelloStatefulWorld.java create mode 100644 spring-ejb/ejb-remote-for-spring/src/main/java/com/baeldung/ejb/tutorial/HelloStatefulWorldBean.java create mode 100755 spring-ejb/ejb-remote-for-spring/src/main/java/com/baeldung/ejb/tutorial/HelloStatelessWorld.java create mode 100755 spring-ejb/ejb-remote-for-spring/src/main/java/com/baeldung/ejb/tutorial/HelloStatelessWorldBean.java create mode 100755 spring-ejb/ejb-remote-for-spring/src/main/resources/META-INF/ejb-jar.xml create mode 100644 spring-ejb/ejb-remote-for-spring/src/test/java/com/baeldung/ejb/tutorial/HelloStatefulWorldTestUnitTest.java create mode 100644 spring-ejb/ejb-remote-for-spring/src/test/java/com/baeldung/ejb/tutorial/HelloStatelessWorldTestUnitTest.java create mode 100755 spring-ejb/pom.xml create mode 100644 spring-ejb/spring-ejb-client/pom.xml create mode 100644 spring-ejb/spring-ejb-client/src/main/java/com/baeldung/springejbclient/SpringEjbClientApplication.java create mode 100644 spring-ejb/spring-ejb-client/src/main/java/com/baeldung/springejbclient/endpoint/HomeEndpoint.java create mode 100644 spring-ejb/spring-ejb-client/src/main/resources/application.properties create mode 100644 spring-ejb/spring-ejb-client/src/test/java/com/baeldung/springejbclient/SpringEjbClientApplicationIntegrationTest.java diff --git a/pom.xml b/pom.xml index b018fdd778..0d26188082 100644 --- a/pom.xml +++ b/pom.xml @@ -161,6 +161,7 @@ spring-cloud spring-core spring-cucumber + spring-ejb spring-aop persistence-modules/spring-data-cassandra spring-data-couchbase-2 diff --git a/spring-ejb/ejb-remote-for-spring/pom.xml b/spring-ejb/ejb-remote-for-spring/pom.xml new file mode 100755 index 0000000000..fd1095420c --- /dev/null +++ b/spring-ejb/ejb-remote-for-spring/pom.xml @@ -0,0 +1,76 @@ + + + 4.0.0 + + + com.baeldung.spring.ejb + ejb-for-spring + 1.0.1 + + + ejb-remote-for-spring + ejb + + + + javax + javaee-api + provided + + + org.assertj + assertj-core + 3.9.0 + test + + + + + + + + wildfly-standalone + + false + + + + + org.codehaus.cargo + cargo-maven2-plugin + ${cargo-maven2-plugin.version} + + + + + wildfly10x + + http://download.jboss.org/wildfly/10.1.0.Final/wildfly-10.1.0.Final.zip + + + + + + 127.0.0.1 + standalone-full + 9990 + testUser:admin1234! + + + + + + + + + + + + 7.0 + 1.6.1 + + + + + diff --git a/spring-ejb/ejb-remote-for-spring/src/main/java/com/baeldung/ejb/tutorial/HelloStatefulWorld.java b/spring-ejb/ejb-remote-for-spring/src/main/java/com/baeldung/ejb/tutorial/HelloStatefulWorld.java new file mode 100644 index 0000000000..6d1c26ef4a --- /dev/null +++ b/spring-ejb/ejb-remote-for-spring/src/main/java/com/baeldung/ejb/tutorial/HelloStatefulWorld.java @@ -0,0 +1,11 @@ +package com.baeldung.ejb.tutorial; + +import javax.ejb.Remote; + +@Remote +public interface HelloStatefulWorld { + + int howManyTimes(); + String getHelloWorld(); + +} diff --git a/spring-ejb/ejb-remote-for-spring/src/main/java/com/baeldung/ejb/tutorial/HelloStatefulWorldBean.java b/spring-ejb/ejb-remote-for-spring/src/main/java/com/baeldung/ejb/tutorial/HelloStatefulWorldBean.java new file mode 100644 index 0000000000..0619f5593a --- /dev/null +++ b/spring-ejb/ejb-remote-for-spring/src/main/java/com/baeldung/ejb/tutorial/HelloStatefulWorldBean.java @@ -0,0 +1,19 @@ +package com.baeldung.ejb.tutorial; + +import javax.ejb.Stateful; + +@Stateful(name = "HelloStatefulWorld") +public class HelloStatefulWorldBean implements HelloStatefulWorld { + + private int howManyTimes = 0; + + public int howManyTimes() { + return howManyTimes; + } + + public String getHelloWorld() { + howManyTimes++; + return "Hello Stateful World!"; + } + +} diff --git a/spring-ejb/ejb-remote-for-spring/src/main/java/com/baeldung/ejb/tutorial/HelloStatelessWorld.java b/spring-ejb/ejb-remote-for-spring/src/main/java/com/baeldung/ejb/tutorial/HelloStatelessWorld.java new file mode 100755 index 0000000000..6b4db29e95 --- /dev/null +++ b/spring-ejb/ejb-remote-for-spring/src/main/java/com/baeldung/ejb/tutorial/HelloStatelessWorld.java @@ -0,0 +1,10 @@ +package com.baeldung.ejb.tutorial; + +import javax.ejb.Remote; + +@Remote +public interface HelloStatelessWorld { + + String getHelloWorld(); + +} diff --git a/spring-ejb/ejb-remote-for-spring/src/main/java/com/baeldung/ejb/tutorial/HelloStatelessWorldBean.java b/spring-ejb/ejb-remote-for-spring/src/main/java/com/baeldung/ejb/tutorial/HelloStatelessWorldBean.java new file mode 100755 index 0000000000..7de499c618 --- /dev/null +++ b/spring-ejb/ejb-remote-for-spring/src/main/java/com/baeldung/ejb/tutorial/HelloStatelessWorldBean.java @@ -0,0 +1,12 @@ +package com.baeldung.ejb.tutorial; + +import javax.ejb.Stateless; + +@Stateless(name = "HelloStatelessWorld") +public class HelloStatelessWorldBean implements HelloStatelessWorld { + + public String getHelloWorld() { + return "Hello Stateless World!"; + } + +} diff --git a/spring-ejb/ejb-remote-for-spring/src/main/resources/META-INF/ejb-jar.xml b/spring-ejb/ejb-remote-for-spring/src/main/resources/META-INF/ejb-jar.xml new file mode 100755 index 0000000000..f51523ac14 --- /dev/null +++ b/spring-ejb/ejb-remote-for-spring/src/main/resources/META-INF/ejb-jar.xml @@ -0,0 +1,7 @@ + + + ejb-remote-for-spring + + diff --git a/spring-ejb/ejb-remote-for-spring/src/test/java/com/baeldung/ejb/tutorial/HelloStatefulWorldTestUnitTest.java b/spring-ejb/ejb-remote-for-spring/src/test/java/com/baeldung/ejb/tutorial/HelloStatefulWorldTestUnitTest.java new file mode 100644 index 0000000000..61373079f6 --- /dev/null +++ b/spring-ejb/ejb-remote-for-spring/src/test/java/com/baeldung/ejb/tutorial/HelloStatefulWorldTestUnitTest.java @@ -0,0 +1,32 @@ +package com.baeldung.ejb.tutorial; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Before; +import org.junit.Test; + +public class HelloStatefulWorldTestUnitTest { + + private HelloStatefulWorldBean statefulBean; + + @Before + public void setup() { + statefulBean = new HelloStatefulWorldBean(); + } + + @Test + public void whenGetHelloWorld_thenHelloStatefulWorldIsReturned() { + String helloWorld = statefulBean.getHelloWorld(); + + assertThat(helloWorld).isEqualTo("Hello Stateful World!"); + } + + @Test + public void whenGetHelloWorldIsCalledTwice_thenCounterIs2() { + statefulBean.getHelloWorld(); + statefulBean.getHelloWorld(); + + assertThat(statefulBean.howManyTimes()).isEqualTo(2); + } + +} diff --git a/spring-ejb/ejb-remote-for-spring/src/test/java/com/baeldung/ejb/tutorial/HelloStatelessWorldTestUnitTest.java b/spring-ejb/ejb-remote-for-spring/src/test/java/com/baeldung/ejb/tutorial/HelloStatelessWorldTestUnitTest.java new file mode 100644 index 0000000000..b95618e4d4 --- /dev/null +++ b/spring-ejb/ejb-remote-for-spring/src/test/java/com/baeldung/ejb/tutorial/HelloStatelessWorldTestUnitTest.java @@ -0,0 +1,24 @@ +package com.baeldung.ejb.tutorial; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Before; +import org.junit.Test; + +public class HelloStatelessWorldTestUnitTest { + + private HelloStatelessWorldBean statelessBean; + + @Before + public void setup() { + statelessBean = new HelloStatelessWorldBean(); + } + + @Test + public void whenGetHelloWorld_thenHelloStatelessWorldIsReturned() { + String helloWorld = statelessBean.getHelloWorld(); + + assertThat(helloWorld).isEqualTo("Hello Stateless World!"); + } + +} diff --git a/spring-ejb/pom.xml b/spring-ejb/pom.xml new file mode 100755 index 0000000000..0b2a8445c5 --- /dev/null +++ b/spring-ejb/pom.xml @@ -0,0 +1,77 @@ + + + 4.0.0 + com.baeldung.spring.ejb + ejb-for-spring + 1.0.1 + pom + ejb + Spring EJB Tutorial + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + jboss-public-repository-group + JBoss Public Maven Repository Group + http://repository.jboss.org/nexus/content/groups/public/ + default + + true + never + + + true + never + + + + + + + + com.baeldung.spring.ejb + ejb-remote-for-spring + 1.0.1 + ejb + + + javax + javaee-api + 7.0 + provided + + + org.wildfly + wildfly-ejb-client-bom + 10.1.0.Final + pom + import + + + + + + + + + maven-ejb-plugin + 2.4 + + 3.2 + + + + + + + + ejb-remote-for-spring + spring-ejb-client + + diff --git a/spring-ejb/spring-ejb-client/pom.xml b/spring-ejb/spring-ejb-client/pom.xml new file mode 100644 index 0000000000..c77ce09a2d --- /dev/null +++ b/spring-ejb/spring-ejb-client/pom.xml @@ -0,0 +1,100 @@ + + + 4.0.0 + + spring-ejb-client + jar + + spring-ejb-client + Spring EJB Client + + + com.baeldung + parent-boot-5 + 0.0.1-SNAPSHOT + ../../parent-boot-5 + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.wildfly + wildfly-ejb-client-bom + 10.1.0.Final + pom + + + + com.baeldung.spring.ejb + ejb-remote-for-spring + 1.0.1 + ejb + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + diff --git a/spring-ejb/spring-ejb-client/src/main/java/com/baeldung/springejbclient/SpringEjbClientApplication.java b/spring-ejb/spring-ejb-client/src/main/java/com/baeldung/springejbclient/SpringEjbClientApplication.java new file mode 100644 index 0000000000..d3542a2158 --- /dev/null +++ b/spring-ejb/spring-ejb-client/src/main/java/com/baeldung/springejbclient/SpringEjbClientApplication.java @@ -0,0 +1,50 @@ +package com.baeldung.springejbclient; + +import java.util.Properties; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +import com.baeldung.ejb.tutorial.HelloStatefulWorld; +import com.baeldung.ejb.tutorial.HelloStatelessWorld; + +@SpringBootApplication +public class SpringEjbClientApplication { + + @Bean + public Context context() throws NamingException { + Properties jndiProps = new Properties(); + jndiProps.put("java.naming.factory.initial", "org.jboss.naming.remote.client.InitialContextFactory"); + jndiProps.put("jboss.naming.client.ejb.context", true); + jndiProps.put("java.naming.provider.url", "http-remoting://localhost:8080"); + return new InitialContext(jndiProps); + } + + @Bean + public HelloStatelessWorld helloStatelessWorld(Context context) throws NamingException { + return (HelloStatelessWorld) context.lookup(this.getFullName(HelloStatelessWorld.class)); + } + + @Bean + public HelloStatefulWorld helloStatefulWorld(Context context) throws NamingException { + return (HelloStatefulWorld) context.lookup(this.getFullName(HelloStatefulWorld.class)); + } + + @SuppressWarnings("rawtypes") + private String getFullName(Class classType) { + String moduleName = "ejb-remote-for-spring/"; + String beanName = classType.getSimpleName(); + String viewClassName = classType.getName(); + + return moduleName + beanName + "!" + viewClassName; + } + + public static void main(String[] args) { + SpringApplication.run(SpringEjbClientApplication.class, args); + } +} diff --git a/spring-ejb/spring-ejb-client/src/main/java/com/baeldung/springejbclient/endpoint/HomeEndpoint.java b/spring-ejb/spring-ejb-client/src/main/java/com/baeldung/springejbclient/endpoint/HomeEndpoint.java new file mode 100644 index 0000000000..e72e3b310e --- /dev/null +++ b/spring-ejb/spring-ejb-client/src/main/java/com/baeldung/springejbclient/endpoint/HomeEndpoint.java @@ -0,0 +1,30 @@ +package com.baeldung.springejbclient.endpoint; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.ejb.tutorial.HelloStatefulWorld; +import com.baeldung.ejb.tutorial.HelloStatelessWorld; + +@RestController +public class HomeEndpoint { + + private HelloStatelessWorld helloStatelessWorld; + private HelloStatefulWorld helloStatefulWorld; + + public HomeEndpoint(HelloStatelessWorld helloStatelessWorld, HelloStatefulWorld helloStatefulWorld) { + this.helloStatelessWorld = helloStatelessWorld; + this.helloStatefulWorld = helloStatefulWorld; + } + + @GetMapping("/stateless") + public String getStateless() { + return helloStatelessWorld.getHelloWorld(); + } + + @GetMapping("/stateful") + public String getStateful() { + return helloStatefulWorld.getHelloWorld() + " called " + helloStatefulWorld.howManyTimes() + " times"; + } + +} diff --git a/spring-ejb/spring-ejb-client/src/main/resources/application.properties b/spring-ejb/spring-ejb-client/src/main/resources/application.properties new file mode 100644 index 0000000000..d564d40356 --- /dev/null +++ b/spring-ejb/spring-ejb-client/src/main/resources/application.properties @@ -0,0 +1,3 @@ +server.port=8081 + +#logging.level.root=DEBUG \ No newline at end of file diff --git a/spring-ejb/spring-ejb-client/src/test/java/com/baeldung/springejbclient/SpringEjbClientApplicationIntegrationTest.java b/spring-ejb/spring-ejb-client/src/test/java/com/baeldung/springejbclient/SpringEjbClientApplicationIntegrationTest.java new file mode 100644 index 0000000000..89c88fe5a5 --- /dev/null +++ b/spring-ejb/spring-ejb-client/src/test/java/com/baeldung/springejbclient/SpringEjbClientApplicationIntegrationTest.java @@ -0,0 +1,11 @@ +package com.baeldung.springejbclient; + +import org.junit.Test; + +public class SpringEjbClientApplicationIntegrationTest { + + @Test + public void contextLoads() { + } + +}