0

我正在尝试以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在类路径中。

4

1 回答 1

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其中包含以下内容:

“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 存储库的原因。”

所以,我添加了 maven 依赖项,而不是在我的类路径中添加 jar,瞧!有用!

于 2016-02-18T18:00:05.720 回答