Wildfly 8 上简单 EJB 项目中的 NoInitialContextException
NoInitialContextException in simple EJB Project on Wildfly 8
我正在尝试为一个简单的 EJB 项目编写一个客户端,我已经在我的 Wildfly 8 运行ning 本地部署在 Eclipse 中。
我的界面:
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 的帖子,但我不确定该放些什么。据我了解,我可以拥有此属性文件,也可以像我一样以编程方式声明属性。
除了您使用的是 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 client assumes JNDI names in remote lookups are relative to java:jboss/exported namespace, a lookup of an absolute JNDI name will fail.
可以在 Wildfly 8 上找到更多信息 Remote JNDI Reference Update Draft。
我正在尝试为一个简单的 EJB 项目编写一个客户端,我已经在我的 Wildfly 8 运行ning 本地部署在 Eclipse 中。
我的界面:
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 的帖子,但我不确定该放些什么。据我了解,我可以拥有此属性文件,也可以像我一样以编程方式声明属性。
除了您使用的是 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 client assumes JNDI names in remote lookups are relative to java:jboss/exported namespace, a lookup of an absolute JNDI name will fail.
可以在 Wildfly 8 上找到更多信息 Remote JNDI Reference Update Draft。