Skip to content

Commit 22207e6

Browse files
BAEL 317: Setting up EJB
EJB Client and EJB Remote
1 parent 67b3d62 commit 22207e6

9 files changed

Lines changed: 264 additions & 0 deletions

File tree

‎ejb/ejb-client/pom.xml‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.baeldung.ejb</groupId>
7+
<artifactId>ejb</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<artifactId>ejb-client</artifactId>
11+
<name>EJB3 Client Maven</name>
12+
<description>EJB3 Client Maven</description>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.wildfly</groupId>
17+
<artifactId>wildfly-ejb-client-bom</artifactId>
18+
<type>pom</type>
19+
<scope>import</scope>
20+
</dependency>
21+
<dependency>
22+
<groupId>com.baeldung.ejb</groupId>
23+
<artifactId>ejb-remote</artifactId>
24+
<type>ejb</type>
25+
</dependency>
26+
</dependencies>
27+
28+
</project>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.baeldung.ejb.client;
2+
3+
import java.util.Properties;
4+
5+
import javax.naming.Context;
6+
import javax.naming.InitialContext;
7+
import javax.naming.NamingException;
8+
9+
import com.baeldung.ejb.tutorial.HelloWorld;
10+
11+
public class EJBClient {
12+
13+
public EJBClient() {
14+
}
15+
16+
private Context context = null;
17+
18+
public String getEJBRemoteMessage() {
19+
EJBClient main = new EJBClient();
20+
try {
21+
// 1. Obtaining Context
22+
main.createInitialContext();
23+
// 2. Generate JNDI Lookup name and caste
24+
HelloWorld helloWorld = main.lookup();
25+
return helloWorld.getHelloWorld();
26+
} catch (NamingException e) {
27+
e.printStackTrace();
28+
return "";
29+
} finally {
30+
try {
31+
main.closeContext();
32+
} catch (NamingException e) {
33+
e.printStackTrace();
34+
}
35+
}
36+
}
37+
38+
public HelloWorld lookup() throws NamingException {
39+
40+
// The app name is the EAR name of the deployed EJB without .ear suffix.
41+
// Since we haven't deployed the application as a .ear, the app name for
42+
// us will be an empty string
43+
final String appName = "";
44+
final String moduleName = "remote";
45+
final String distinctName = "";
46+
final String beanName = "HelloWorld";
47+
final String viewClassName = HelloWorld.class.getName();
48+
final String toLookup = "ejb:" + appName + "/" + moduleName
49+
+ "/" + distinctName + "/" + beanName + "!" + viewClassName;
50+
return (HelloWorld) context.lookup(toLookup);
51+
}
52+
53+
public void createInitialContext() throws NamingException {
54+
Properties prop = new Properties();
55+
prop.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
56+
prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
57+
prop.put(Context.PROVIDER_URL, "http-remoting://127.0.0.1:8080");
58+
prop.put(Context.SECURITY_PRINCIPAL, "pritamtest");
59+
prop.put(Context.SECURITY_CREDENTIALS, "iamtheki9g");
60+
prop.put("jboss.naming.client.ejb.context", false);
61+
62+
context = new InitialContext(prop);
63+
}
64+
65+
public void closeContext() throws NamingException {
66+
if (context != null) {
67+
context.close();
68+
}
69+
}
70+
71+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
remote.connections=default
2+
remote.connection.default.host=127.0.0.1
3+
remote.connection.default.port=8080
4+
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=pritamtest
8+
remote.connection.default.password=iamtheki9g
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.ejb.setup.test;
2+
3+
import static org.junit.Assert.*;
4+
import org.junit.Test;
5+
import com.baeldung.ejb.client.EJBClient;
6+
import com.baeldung.ejb.tutorial.HelloWorldBean;
7+
8+
public class EJBSetupTest {
9+
10+
@Test
11+
public void testEJBClient() {
12+
EJBClient ejbClient = new EJBClient();
13+
HelloWorldBean bean = new HelloWorldBean();
14+
assertEquals(bean.getHelloWorld(), ejbClient.getEJBRemoteMessage());
15+
}
16+
}

‎ejb/ejb-remote/pom.xml‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.baeldung.ejb</groupId>
7+
<artifactId>ejb</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<artifactId>ejb-remote</artifactId>
11+
<packaging>ejb</packaging>
12+
13+
<name>ejb-remote</name>
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.jboss.spec.javax.ejb</groupId>
17+
<artifactId>jboss-ejb-api_3.2_spec</artifactId>
18+
<scope>provided</scope>
19+
</dependency>
20+
</dependencies>
21+
22+
<build>
23+
<finalName>ejb-remote</finalName>
24+
</build>
25+
</project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.ejb.tutorial;
2+
3+
import javax.ejb.Remote;
4+
5+
@Remote
6+
public interface HelloWorld {
7+
String getHelloWorld();
8+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.ejb.tutorial;
2+
3+
import javax.annotation.Resource;
4+
import javax.ejb.SessionContext;
5+
import javax.ejb.Stateless;
6+
7+
@Stateless(name = "HelloWorld")
8+
public class HelloWorldBean implements HelloWorld {
9+
10+
@Resource
11+
private SessionContext context;
12+
13+
@Override
14+
public String getHelloWorld() {
15+
return "Welcome to EJB Tutorial!";
16+
}
17+
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ejb-jar xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/ejb-jar_3_2.xsd"
4+
version="3.2">
5+
<module-name>remote</module-name>
6+
</ejb-jar>
7+

‎ejb/pom.xml‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.baeldung.ejb</groupId>
6+
<artifactId>ejb</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>pom</packaging>
9+
<name>ejb</name>
10+
<description>EJB Tutorial</description>
11+
12+
<repositories>
13+
<repository>
14+
<id>jboss-public-repository-group</id>
15+
<name>JBoss Public Maven Repository Group</name>
16+
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
17+
<layout>default</layout>
18+
<releases>
19+
<enabled>true</enabled>
20+
<updatePolicy>never</updatePolicy>
21+
</releases>
22+
<snapshots>
23+
<enabled>true</enabled>
24+
<updatePolicy>never</updatePolicy>
25+
</snapshots>
26+
</repository>
27+
</repositories>
28+
29+
<dependencyManagement>
30+
<dependencies>
31+
<dependency>
32+
<groupId>com.baeldung.ejb</groupId>
33+
<artifactId>ejb-remote</artifactId>
34+
<version>1.0-SNAPSHOT</version>
35+
<type>ejb</type>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.jboss.spec</groupId>
40+
<artifactId>jboss-javaee-7.0</artifactId>
41+
<version>1.0.1.Final</version>
42+
<type>pom</type>
43+
<scope>import</scope>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>org.wildfly</groupId>
48+
<artifactId>wildfly-ejb-client-bom</artifactId>
49+
<version>10.1.0.Final</version>
50+
<type>pom</type>
51+
<scope>import</scope>
52+
</dependency>
53+
</dependencies>
54+
</dependencyManagement>
55+
56+
<build>
57+
<pluginManagement>
58+
<plugins>
59+
<plugin>
60+
<artifactId>maven-compiler-plugin</artifactId>
61+
<version>3.1</version>
62+
<configuration>
63+
<source>1.7</source>
64+
<target>1.7</target>
65+
</configuration>
66+
</plugin>
67+
68+
<plugin>
69+
<artifactId>maven-ejb-plugin</artifactId>
70+
<version>2.4</version>
71+
<configuration>
72+
<ejbVersion>3.2</ejbVersion>
73+
</configuration>
74+
</plugin>
75+
</plugins>
76+
</pluginManagement>
77+
</build>
78+
79+
<modules>
80+
<module>ejb-remote</module>
81+
<module>ejb-client</module>
82+
</modules>
83+
</project>

0 commit comments

Comments
 (0)