如何使用 jndi 在 wildfly 中查找本地 bean

How to lookup a local bean in wildfly with jndi

我的问题似乎与 WildFly JNDI lookup for local EJB deployed in a war 相同,只是我无法让他的解决方案起作用。

我听说在 jar jaws-server-ejb3 中部署了 EJB。在我的应用程序中,我想调用一个 bean 上的方法。我已经在我的豆子上尝试过使用和不使用 @LocalBean

当 wildfly (8.2) 启动时,它打印出:

java:global/jaws/jaws-server-ejb3/ForecastService!com.termalabs.common.service.ForecastServiceRemote
java:app/jaws-server-ejb3/ForecastService!com.termalabs.common.service.ForecastServiceRemote
java:module/ForecastService!com.termalabs.common.service.ForecastServiceRemote
java:jboss/exported/jaws/jaws-server-ejb3/ForecastService!com.termalabs.common.service.ForecastServiceRemote
java:global/jaws/jaws-server-ejb3/ForecastService
java:app/jaws-server-ejb3/ForecastService
java:module/ForecastService

当我将@LocalBean 添加到我的 bean 时,我看到:

java:global/jaws/jaws-server-ejb3/ForecastService!com.termalabs.server.service.ForecastService
java:app/jaws-server-ejb3/ForecastService!com.termalabs.server.service.ForecastService
java:module/ForecastService!com.termalabs.server.service.ForecastService
java:global/jaws/jaws-server-ejb3/ForecastService!com.termalabs.common.service.ForecastServiceRemote
java:app/jaws-server-ejb3/ForecastService!com.termalabs.common.service.ForecastServiceRemote
java:module/ForecastService!com.termalabs.common.service.ForecastServiceRemote
java:jboss/exported/jaws/jaws-server-ejb3/ForecastService!com.termalabs.common.service.ForecastServiceRemote

所以从我读到的所有内容来看,我应该能够用 "java:app/jaws-server-ejb3/ForecastService" 或 "java:app/jaws-server-ejb3/ForecastService!com.termalabs.common.service.ForecastServiceRemote"

查找这个 bean

这是我目前硬编码的查询:

public static Object getBeanInternal(String beanName, Class remoteClass) throws NamingException {
    System.out.println("Getting internal bean: " + beanName + " for class "  + remoteClass.getName());
    final Hashtable jndiProperties = new Hashtable();
    jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
    InitialContext ctx = new InitialContext(jndiProperties);
    Object ejb = ctx.lookup("java:app/jaws-server-ejb3/ForecastService");
//    Object ejb = ctx.lookup("java:global/jaws-server-ejb3/ForecastService!com.termalabs.common.service.ForecastService");
    return ejb;
}

我已经用空的 jndiProperties table 或上面引用的设置试过了。无论我尝试过什么,我都会得到一个像

这样的 NamingException
2015-04-07 15:50:01,836 WARN  [com.termalabs.server.system.AutosysJammerSequence] (JammerManager-2015/04/07 15:49:46.710 MDT-0) Can't lookup: javax.naming.NameNotFoundException: java:app/jaws-server-ejb3/ForecastService
    at org.jboss.as.naming.InitialContext$DefaultInitialContext.findContext(InitialContext.java:187)
    at org.jboss.as.naming.InitialContext$DefaultInitialContext.lookup(InitialContext.java:231)
    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:188)
    at org.jboss.as.naming.NamingContext.lookup(NamingContext.java:184)
    at javax.naming.InitialContext.lookup(InitialContext.java:417) [rt.jar:1.8.0_25]
    at javax.naming.InitialContext.lookup(InitialContext.java:417) [rt.jar:1.8.0_25]
    at com.termalabs.common.ejb3.EJB3Util.getBeanInternal(EJB3Util.java:55) [jaws-common-ejb3intf-1.0.jar:]
    at com.termalabs.server.system.AutosysJammerSequence.runJammerSequence(AutosysJammerSequence.java:427) [jaws-server-base-5.1dev.jar:]
    at com.termalabs.server.system.JammerSequence.run(JammerSequence.java:125) [jaws-server-base-5.1dev.jar:]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [rt.jar:1.8.0_25]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [rt.jar:1.8.0_25]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_25]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_25]
    at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_25]

我可以使用远程接口访问这个 bean,但这似乎不对,因为我 运行 这个代码来自同一只耳朵。我不需要那样做。

我错过了什么?

谢谢,

迈克尔

查看消息时我发现 JammerManager-2015/04/07 15:49:46.710 MDT-0 看起来您正在启动自己的线程。如果您不使用新的 EE 并发实用程序,则命名上下文可能不会复制到您的线程。您需要确保为新线程设置了命名上下文、安全上下文和 TCCL。