This commit is contained in:
Jonathan Cook
2019-10-23 15:01:44 +02:00
parent db85c8f275
commit 684ec0d2e3
20486 changed files with 1642483 additions and 0 deletions
+69
View File
@@ -0,0 +1,69 @@
<?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>
<artifactId>spring-ejb-client</artifactId>
<name>spring-ejb-client</name>
<packaging>jar</packaging>
<description>Spring EJB Client</description>
<parent>
<groupId>com.baeldung.spring.ejb</groupId>
<artifactId>spring-ejb</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.4.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-ejb-client-bom</artifactId>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.baeldung.spring.ejb</groupId>
<artifactId>spring-ejb-remote</artifactId>
<type>ejb</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.undertow</groupId>
<artifactId>undertow-servlet</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.4.RELEASE</version>
</plugin>
</plugins>
</build>
</project>
@@ -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();
}
}
}
@@ -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);
}
}
}
@@ -0,0 +1,51 @@
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 = "spring-ejb-remote/";
String beanName = classType.getSimpleName();
String viewClassName = classType.getName();
return moduleName + beanName + "!" + viewClassName;
}
public static void main(String[] args) {
SpringApplication.run(SpringEjbClientApplication.class, args);
}
}
@@ -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";
}
}
@@ -0,0 +1,3 @@
server.port=8081
#logging.level.root=DEBUG
@@ -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
@@ -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>
@@ -0,0 +1,22 @@
package com.baeldung;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.springejbclient.SpringEjbClientApplication;
/**
* This Live Test requires:
* * run the `spring-ejb-remote` module with the following command: `mvn clean package cargo:run -Pwildfly-standalone`
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringEjbClientApplication.class)
public class SpringContextLiveTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}
@@ -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());
}
}
@@ -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());
}
}
@@ -0,0 +1,11 @@
package com.baeldung.springejbclient;
import org.junit.Test;
public class SpringEjbClientApplicationIntegrationTest {
@Test
public void contextLoads() {
}
}