Introduction to EJB JNDI Lookup on WildFly Application Server - Alejandro Gervasio | alejandro.gervasio@gmail.com (#2417)
* Initial Commit * jboss-ejb-client.properties file * Updated jboss-ejb-client.properties file
This commit is contained in:
		
							parent
							
								
									9b2e8ec9f9
								
							
						
					
					
						commit
						e13204ca96
					
				| @ -1,8 +1,8 @@ | |||||||
|  | endpoint.name=client-endpoint | ||||||
|  | remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED=false | ||||||
| remote.connections=default | remote.connections=default | ||||||
| remote.connection.default.host=127.0.0.1 | remote.connection.default.host=127.0.0.1 | ||||||
| remote.connection.default.port=8080 | 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_NOANONYMOUS=false | ||||||
| remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT=false | remote.connection.default.username=myusername | ||||||
| remote.connection.default.connect.options.org.xnio.Options.SASL_DISALLOWED_MECHANISMS=${host.auth:JBOSS-LOCAL-USER} | remote.connection.default.password=mypassword | ||||||
| remote.connection.default.username=testUser |  | ||||||
| remote.connection.default.password=admin1234! |  | ||||||
| @ -0,0 +1,42 @@ | |||||||
|  | package com.baeldung.ejbclient.application; | ||||||
|  | 
 | ||||||
|  | import com.baeldung.ejbmodule.TextProcessorBean; | ||||||
|  | import com.baeldung.ejbmodule.TextProcessorRemote; | ||||||
|  | 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 = "EJBModule"; | ||||||
|  |             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,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,31 @@ | |||||||
|  | package com.baeldung.ejbclient.application; | ||||||
|  | 
 | ||||||
|  | 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 TextApplicationTest { | ||||||
|  | 
 | ||||||
|  |     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,10 @@ | |||||||
|  | package com.baeldung.ejbmodule; | ||||||
|  | 
 | ||||||
|  | import javax.ejb.Stateless; | ||||||
|  | 
 | ||||||
|  | @Stateless | ||||||
|  | public class TextProcessorBean implements TextProcessorRemote { | ||||||
|  |     public String processText(String text) { | ||||||
|  |         return text.toUpperCase(); | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -0,0 +1,9 @@ | |||||||
|  | package com.baeldung.ejbmodule; | ||||||
|  | 
 | ||||||
|  | import javax.ejb.Remote; | ||||||
|  | 
 | ||||||
|  | @Remote | ||||||
|  | public interface TextProcessorRemote { | ||||||
|  | 
 | ||||||
|  |     String processText(String text); | ||||||
|  | } | ||||||
| @ -0,0 +1,12 @@ | |||||||
|  | package com.baeldung.ejbmodule; | ||||||
|  | 
 | ||||||
|  | import org.junit.Test; | ||||||
|  | import static org.junit.Assert.assertEquals; | ||||||
|  | 
 | ||||||
|  | public class TextProcessorBeanTest { | ||||||
|  |     @Test | ||||||
|  |     public void givenInputString_whenComparedToStringParsedByBean_thenSuccessful() { | ||||||
|  |         TextProcessorBean textProcessor = new TextProcessorBean(); | ||||||
|  |         assertEquals("TEST", textProcessor.processText("test")); | ||||||
|  |     } | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user