我正在为一个简单的EJB项目编写一个客户端,我已经在Eclipse中运行的Wildfly 8上本地部署了它。
我的界面:
package com.jwt.ejb.business;
import javax.ejb.Remote;
@Remote
public interface Hello {
public String sayHello();
}
我的实施:
package com.jwt.ejb.businesslogic;
import javax.ejb.Stateless;
import com.jwt.ejb.business.Hello;
@Stateless
public class HelloBean implements Hello {
public HelloBean() {
}
public String sayHello() {
return "Hello Boss Welcome to EJB";
}
}
我的客户:
package com.jwt.ejb.test;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.jwt.ejb.business.Hello;
import com.jwt.ejb.businesslogic.HelloBean;
public class Client {
public static void main(String[] args) {
Hello bean = doLookup();
if (bean != null)
System.out.println(bean.sayHello());
}
private static Hello doLookup() {
Context context = null;
Hello bean = null;
try {
final Hashtable jndiProperties = new Hashtable();
jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
context = new InitialContext(jndiProperties);
bean = (Hello) context.lookup(getLookupString());
} catch (NamingException e) {
e.printStackTrace();
}
return bean;
}
private static String getLookupString() throws NamingException {
final String appName = "";
final String moduleName = "EJBTest";
final String distinctName = "";
final String beanName = HelloBean.class.getSimpleName();
final String viewClassName = Hello.class.getName();
return "ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + beanName + "!" + viewClassName;
}
}
当我运行它时,我得到了这个例外:
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at com.jwt.ejb.test.Client.doLookup(Client.java:26)
at com.jwt.ejb.test.Client.main(Client.java:15)
Exception in thread "main" java.lang.NullPointerException
at com.jwt.ejb.test.Client.main(Client.java:16)
官方文档也是这样的: https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI
问题出在哪里?
我在谈论boss-ejb-client.properties的帖子时,但我不知道该放什么。据我所知,我可以拥有这个属性文件,或者像我一样以编程方式声明这些属性。
答案 0 :(得分:2)
除了您使用Wildfly 8并引用JBoss AS 7.1的文档之外,您还缺少JNDI属性上的InitialContextFactory
。
Wildfly关于通过JNDI进行EJB调用的文档是here。因此,您应该执行以下操作:
void doBeanLookup() {
Properties jndiProperties = new Properties();
jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
jndiProperties.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");
// This is an important property to set if you want to do EJB invocations via the remote-naming project
jndiProps.put("jboss.naming.client.ejb.context", true);
// create a context passing these properties
Context ctx = new InitialContext(jndiProps);
// lookup the bean Hello
Hello bean = (Hello) ctx.lookup(getLookupString());
}
在您的pom.xml
中,您应该拥有以下内容:
<dependency>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-ejb-client-bom</artifactId>
<version>8.0.0.Final</version>
<type>pom</type>
</dependency>
如果您想使用 http-remoting 协议,则应在getLookupString
方法中移除ejb:
并显示
http-remoting 客户端假定远程查找中的JNDI名称与java:jboss / exported命名空间相关,绝对JNDI名称的查找将失败。
可在Wildfly 8 Remote JNDI Reference Update Draft上找到更多信息。