我正在尝试以下列方式设置新的InitialContext
(我相信这非常标准):
private static InitialContext getInitialContext() throws NamingException {
InitialContext context = null;
try {
Properties properties = new Properties();
properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
properties.put(Context.PROVIDER_URL, "remote://localhost:4447");
properties.put(Context.SECURITY_PRINCIPAL, "username");
properties.put(Context.SECURITY_CREDENTIALS, "password");
context = new InitialContext(properties);
System.out.println("\n\tGot initial Context: " + context);
}
catch (Exception e) {
e.printStackTrace();
}
return context;
}
public static void sendMessage(RoboticsParameters object_msg) throws Exception {
InitialContext context = getInitialContext();
// other code
}
使用属性创建new InitialContext
的行中的代码“失败”,我得到java.lang.NullPointerException
。我怀疑我错过了一个论点。这是堆栈跟踪:
WARN: EJB client integration will not be available due to a problem setting up the EJB client handler java.lang.NullPointerException
at org.jboss.naming.remote.client.InitialContextFactory.<clinit>(InitialContextFactory.java:118)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:72)
at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:61)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:672)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
at javax.naming.InitialContext.init(InitialContext.java:244)
at javax.naming.InitialContext.<init>(InitialContext.java:216)
有什么建议吗?
我正在运行JBoss EAP 6.4并使用EJB 3.我在类路径中有jboss-client.jar
。
答案 0 :(得分:0)
我检查了源代码:
jboss-remote-naming/src/main/java/org/jboss/naming/remote/client/InitialContextFactory.java
并找到日志消息的来源:
public class InitialContextFactory implements javax.naming.spi.InitialContextFactory {
// code
private static final String REMOTE_NAMING_EJB_CLIENT_HANDLER_CLASS_NAME = "org.jboss.naming.remote.client.ejb.RemoteNamingStoreEJBClientHandler";
// code
try {
klass = classLoader.loadClass(REMOTE_NAMING_EJB_CLIENT_HANDLER_CLASS_NAME);
method = klass.getMethod("setupEJBClientContext", new Class<?>[] {Properties.class, List.class});
} catch (Throwable t) {
logger.warn("EJB client integration will not be available due to a problem setting up the EJB client handler", t);
}
// other code
}
类org.jboss.naming.remote.client.ejb.RemoteNamingStoreEJBClientHandler
在我添加到类路径的jar中,但由于某种原因加载类时出现问题。
然后我偶然发现了README-EJB-JMS.txt
文件夹中的这个小[jboss_home]/bin/client
文件,其中说明了以下内容:
&#34; Maven用户不应该使用此jar,而应使用以下BOM依赖项
<dependencies>
<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-ejb-client-bom</artifactId>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-jms-client-bom</artifactId>
<type>pom</type>
</dependency>
</dependencies>
这是因为使用带有阴影jar的maven很有可能导致类版本冲突,这就是原因所在 我们不会将此jar发布到maven存储库。&#34;
所以,我添加了maven依赖,而不是在我的类路径和VOILA中使用jar!它有效!