Skip to content

Commit e13204c

Browse files
Alejandro Gervasiozhendrikse
authored andcommitted
Introduction to EJB JNDI Lookup on WildFly Application Server - Alejandro Gervasio | [email protected] (eugenp#2417)
* Initial Commit * jboss-ejb-client.properties file * Updated jboss-ejb-client.properties file
1 parent 9b2e8ec commit e13204c

7 files changed

Lines changed: 116 additions & 4 deletions

File tree

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
endpoint.name=client-endpoint
2+
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
13
remote.connections=default
24
remote.connection.default.host=127.0.0.1
35
remote.connection.default.port=8080
46
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
5-
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT=false
6-
remote.connection.default.connect.options.org.xnio.Options.SASL_DISALLOWED_MECHANISMS=${host.auth:JBOSS-LOCAL-USER}
7-
remote.connection.default.username=testUser
8-
remote.connection.default.password=admin1234!
7+
remote.connection.default.username=myusername
8+
remote.connection.default.password=mypassword
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.ejbclient.application;
2+
3+
import com.baeldung.ejbmodule.TextProcessorBean;
4+
import com.baeldung.ejbmodule.TextProcessorRemote;
5+
import javax.naming.Context;
6+
import javax.naming.InitialContext;
7+
import javax.naming.NamingException;
8+
import java.util.Properties;
9+
10+
public class TextApplication {
11+
12+
public static void main(String[] args) throws NamingException {
13+
TextProcessorRemote textProcessor = EJBFactory.createTextProcessorBeanFromJNDI("ejb:");
14+
System.out.print(textProcessor.processText("sample text"));
15+
}
16+
17+
private static class EJBFactory {
18+
19+
private static TextProcessorRemote createTextProcessorBeanFromJNDI(String namespace) throws NamingException {
20+
return lookupTextProcessorBean(namespace);
21+
}
22+
23+
private static TextProcessorRemote lookupTextProcessorBean(String namespace) throws NamingException {
24+
Context ctx = createInitialContext();
25+
final String appName = "";
26+
final String moduleName = "EJBModule";
27+
final String distinctName = "";
28+
final String beanName = TextProcessorBean.class.getSimpleName();
29+
final String viewClassName = TextProcessorRemote.class.getName();
30+
return (TextProcessorRemote) ctx.lookup(namespace + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName);
31+
}
32+
33+
private static Context createInitialContext() throws NamingException {
34+
Properties jndiProperties = new Properties();
35+
jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
36+
jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
37+
jndiProperties.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");
38+
jndiProperties.put("jboss.naming.client.ejb.context", true);
39+
return new InitialContext(jndiProperties);
40+
}
41+
}
42+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
endpoint.name=client-endpoint
2+
remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false
3+
remote.connections=default
4+
remote.connection.default.host=127.0.0.1
5+
remote.connection.default.port=8080
6+
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS=false
7+
remote.connection.default.username=myusername
8+
remote.connection.default.password=mypassword
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.ejbclient.application;
2+
3+
import org.junit.AfterClass;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
import javax.naming.NamingException;
7+
import java.io.ByteArrayOutputStream;
8+
import java.io.PrintStream;
9+
import static org.junit.Assert.*;
10+
11+
public class TextApplicationTest {
12+
13+
private static ByteArrayOutputStream outContent;
14+
15+
@BeforeClass
16+
public static void setUpPrintStreamInstance() {
17+
outContent = new ByteArrayOutputStream();
18+
System.setOut(new PrintStream(outContent));
19+
}
20+
21+
@AfterClass
22+
public static void tearDownByteArrayOutputStream() {
23+
outContent = null;
24+
}
25+
26+
@Test
27+
public void givenInputString_whenCompareTtoStringPrintedToConsole_thenSuccessful() throws NamingException {
28+
TextApplication.main(new String[]{});
29+
assertEquals("SAMPLE TEXT", outContent.toString());
30+
}
31+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.ejbmodule;
2+
3+
import javax.ejb.Stateless;
4+
5+
@Stateless
6+
public class TextProcessorBean implements TextProcessorRemote {
7+
public String processText(String text) {
8+
return text.toUpperCase();
9+
}
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.ejbmodule;
2+
3+
import javax.ejb.Remote;
4+
5+
@Remote
6+
public interface TextProcessorRemote {
7+
8+
String processText(String text);
9+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.ejbmodule;
2+
3+
import org.junit.Test;
4+
import static org.junit.Assert.assertEquals;
5+
6+
public class TextProcessorBeanTest {
7+
@Test
8+
public void givenInputString_whenComparedToStringParsedByBean_thenSuccessful() {
9+
TextProcessorBean textProcessor = new TextProcessorBean();
10+
assertEquals("TEST", textProcessor.processText("test"));
11+
}
12+
}

0 commit comments

Comments
 (0)