|
| 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 | +} |
0 commit comments