BAEL 317: Setting up EJB
EJB Client and EJB Remote
This commit is contained in:
		
							parent
							
								
									67b3d62b91
								
							
						
					
					
						commit
						22207e646b
					
				
							
								
								
									
										28
									
								
								ejb/ejb-client/pom.xml
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										28
									
								
								ejb/ejb-client/pom.xml
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,28 @@ | ||||
| <?xml version="1.0"?> | ||||
| <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/maven-v4_0_0.xsd"> | ||||
| 	<modelVersion>4.0.0</modelVersion> | ||||
| 	<parent> | ||||
| 		<groupId>com.baeldung.ejb</groupId> | ||||
| 		<artifactId>ejb</artifactId> | ||||
| 		<version>1.0-SNAPSHOT</version> | ||||
| 	</parent> | ||||
| 	<artifactId>ejb-client</artifactId> | ||||
| 	<name>EJB3 Client Maven</name> | ||||
| 	<description>EJB3 Client Maven</description> | ||||
| 
 | ||||
| 	<dependencies> | ||||
| 		<dependency> | ||||
| 			<groupId>org.wildfly</groupId> | ||||
| 			<artifactId>wildfly-ejb-client-bom</artifactId> | ||||
| 			<type>pom</type> | ||||
| 			<scope>import</scope> | ||||
| 		</dependency> | ||||
| 		<dependency> | ||||
| 			<groupId>com.baeldung.ejb</groupId> | ||||
| 			<artifactId>ejb-remote</artifactId> | ||||
| 			<type>ejb</type> | ||||
| 		</dependency> | ||||
| 	</dependencies> | ||||
| 
 | ||||
| </project> | ||||
							
								
								
									
										71
									
								
								ejb/ejb-client/src/main/java/com/baeldung/ejb/client/EJBClient.java
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										71
									
								
								ejb/ejb-client/src/main/java/com/baeldung/ejb/client/EJBClient.java
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,71 @@ | ||||
| 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 = "remote"; | ||||
|         final String distinctName = ""; | ||||
|         final String beanName = "HelloWorld"; | ||||
|         final String viewClassName = HelloWorld.class.getName(); | ||||
|         final String toLookup = "ejb:" + 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, "pritamtest"); | ||||
|         prop.put(Context.SECURITY_CREDENTIALS, "iamtheki9g"); | ||||
|         prop.put("jboss.naming.client.ejb.context", false); | ||||
| 
 | ||||
|         context = new InitialContext(prop); | ||||
|     } | ||||
| 
 | ||||
|     public void closeContext() throws NamingException { | ||||
|         if (context != null) { | ||||
|             context.close(); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| } | ||||
							
								
								
									
										8
									
								
								ejb/ejb-client/src/main/resources/jboss-ejb-client.properties
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										8
									
								
								ejb/ejb-client/src/main/resources/jboss-ejb-client.properties
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,8 @@ | ||||
| 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.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT=false | ||||
| remote.connection.default.connect.options.org.xnio.Options.SASL_DISALLOWED_MECHANISMS=${host.auth:JBOSS-LOCAL-USER} | ||||
| remote.connection.default.username=pritamtest | ||||
| remote.connection.default.password=iamtheki9g | ||||
							
								
								
									
										16
									
								
								ejb/ejb-client/src/test/java/com/baeldung/ejb/setup/test/EJBSetupTest.java
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										16
									
								
								ejb/ejb-client/src/test/java/com/baeldung/ejb/setup/test/EJBSetupTest.java
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,16 @@ | ||||
| package com.baeldung.ejb.setup.test; | ||||
| 
 | ||||
| import static org.junit.Assert.*; | ||||
| import org.junit.Test; | ||||
| import com.baeldung.ejb.client.EJBClient; | ||||
| import com.baeldung.ejb.tutorial.HelloWorldBean; | ||||
| 
 | ||||
| public class EJBSetupTest { | ||||
| 
 | ||||
|     @Test | ||||
|     public void testEJBClient() { | ||||
|         EJBClient ejbClient = new EJBClient(); | ||||
|         HelloWorldBean bean = new HelloWorldBean(); | ||||
|         assertEquals(bean.getHelloWorld(), ejbClient.getEJBRemoteMessage()); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										25
									
								
								ejb/ejb-remote/pom.xml
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										25
									
								
								ejb/ejb-remote/pom.xml
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,25 @@ | ||||
| <?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> | ||||
| 	<parent> | ||||
| 		<groupId>com.baeldung.ejb</groupId> | ||||
| 		<artifactId>ejb</artifactId> | ||||
| 		<version>1.0-SNAPSHOT</version> | ||||
| 	</parent> | ||||
| 	<artifactId>ejb-remote</artifactId> | ||||
| 	<packaging>ejb</packaging> | ||||
| 
 | ||||
| 	<name>ejb-remote</name> | ||||
| 	<dependencies> | ||||
| 		<dependency> | ||||
| 			<groupId>org.jboss.spec.javax.ejb</groupId> | ||||
| 			<artifactId>jboss-ejb-api_3.2_spec</artifactId> | ||||
| 			<scope>provided</scope> | ||||
| 		</dependency> | ||||
| 	</dependencies> | ||||
| 
 | ||||
| 	<build> | ||||
| 		<finalName>ejb-remote</finalName> | ||||
| 	</build> | ||||
| </project> | ||||
							
								
								
									
										8
									
								
								ejb/ejb-remote/src/main/java/com/baeldung/ejb/tutorial/HelloWorld.java
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										8
									
								
								ejb/ejb-remote/src/main/java/com/baeldung/ejb/tutorial/HelloWorld.java
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,8 @@ | ||||
| package com.baeldung.ejb.tutorial; | ||||
| 
 | ||||
| import javax.ejb.Remote; | ||||
| 
 | ||||
| @Remote | ||||
| public interface HelloWorld { | ||||
|     String getHelloWorld(); | ||||
| } | ||||
							
								
								
									
										18
									
								
								ejb/ejb-remote/src/main/java/com/baeldung/ejb/tutorial/HelloWorldBean.java
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										18
									
								
								ejb/ejb-remote/src/main/java/com/baeldung/ejb/tutorial/HelloWorldBean.java
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,18 @@ | ||||
| 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!"; | ||||
|     } | ||||
| 
 | ||||
| } | ||||
							
								
								
									
										7
									
								
								ejb/ejb-remote/src/main/resources/META-INF/ejb-jar.xml
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										7
									
								
								ejb/ejb-remote/src/main/resources/META-INF/ejb-jar.xml
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,7 @@ | ||||
| <?xml version="1.0" encoding="UTF-8"?> | ||||
| <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>remote</module-name> | ||||
| </ejb-jar> | ||||
| 
 | ||||
							
								
								
									
										83
									
								
								ejb/pom.xml
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										83
									
								
								ejb/pom.xml
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,83 @@ | ||||
| <?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> | ||||
| 	<groupId>com.baeldung.ejb</groupId> | ||||
| 	<artifactId>ejb</artifactId> | ||||
| 	<version>1.0-SNAPSHOT</version> | ||||
| 	<packaging>pom</packaging> | ||||
| 	<name>ejb</name> | ||||
| 	<description>EJB Tutorial</description> | ||||
| 
 | ||||
| 	<repositories> | ||||
| 		<repository> | ||||
| 			<id>jboss-public-repository-group</id> | ||||
| 			<name>JBoss Public Maven Repository Group</name> | ||||
| 			<url>http://repository.jboss.org/nexus/content/groups/public/</url> | ||||
| 			<layout>default</layout> | ||||
| 			<releases> | ||||
| 				<enabled>true</enabled> | ||||
| 				<updatePolicy>never</updatePolicy> | ||||
| 			</releases> | ||||
| 			<snapshots> | ||||
| 				<enabled>true</enabled> | ||||
| 				<updatePolicy>never</updatePolicy> | ||||
| 			</snapshots> | ||||
| 		</repository> | ||||
| 	</repositories> | ||||
| 
 | ||||
| 	<dependencyManagement> | ||||
| 		<dependencies> | ||||
| 			<dependency> | ||||
| 				<groupId>com.baeldung.ejb</groupId> | ||||
| 				<artifactId>ejb-remote</artifactId> | ||||
| 				<version>1.0-SNAPSHOT</version> | ||||
| 				<type>ejb</type> | ||||
| 			</dependency> | ||||
| 
 | ||||
| 			<dependency> | ||||
| 				<groupId>org.jboss.spec</groupId> | ||||
| 				<artifactId>jboss-javaee-7.0</artifactId> | ||||
| 				<version>1.0.1.Final</version> | ||||
| 				<type>pom</type> | ||||
| 				<scope>import</scope> | ||||
| 			</dependency> | ||||
| 
 | ||||
| 			<dependency> | ||||
| 				<groupId>org.wildfly</groupId> | ||||
| 				<artifactId>wildfly-ejb-client-bom</artifactId> | ||||
| 				<version>10.1.0.Final</version> | ||||
| 				<type>pom</type> | ||||
| 				<scope>import</scope> | ||||
| 			</dependency> | ||||
| 		</dependencies> | ||||
| 	</dependencyManagement> | ||||
| 
 | ||||
| 	<build> | ||||
| 		<pluginManagement> | ||||
| 			<plugins> | ||||
| 				<plugin> | ||||
| 					<artifactId>maven-compiler-plugin</artifactId> | ||||
| 					<version>3.1</version> | ||||
| 					<configuration> | ||||
| 						<source>1.7</source> | ||||
| 						<target>1.7</target> | ||||
| 					</configuration> | ||||
| 				</plugin> | ||||
| 
 | ||||
| 				<plugin> | ||||
| 					<artifactId>maven-ejb-plugin</artifactId> | ||||
| 					<version>2.4</version> | ||||
| 					<configuration> | ||||
| 						<ejbVersion>3.2</ejbVersion> | ||||
| 					</configuration> | ||||
| 				</plugin> | ||||
| 			</plugins> | ||||
| 		</pluginManagement> | ||||
| 	</build> | ||||
| 
 | ||||
| 	<modules> | ||||
| 		<module>ejb-remote</module> | ||||
| 		<module>ejb-client</module> | ||||
| 	</modules> | ||||
| </project> | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user