Java: 当在线程内部运行时,JNDI查找失败

huangapple go评论153阅读模式
英文:

Java: JNDI Lookup fails when ran inside a Thread

问题

以下是您提供的内容的翻译部分:

我面临一个关于使用 Wildfly 18 进行 JNDI 查找的相当奇怪的行为我有一个简单的 EJB

package com.getronics.ejb;

import javax.ejb.Remote;
import javax.ejb.Local;
import javax.ejb.Stateless;

@Stateless
@Local(ICalculadoraLocal.class)
public class CalculadoraBean implements ICalculadoraLocal {
    public int suma(int a, int b){
        return a+b;
    }
}

一个简单的接口

package com.getronics.ejb;

public interface ICalculadoraLocal{
    int suma(int a, int b);
}

以及一个简单的 JSP 页面来检查它

<html>
    <body>
        <%@ page import="java.util.Hashtable,javax.naming.*,com.getronics.ejb.*" %>
        request: <%= request.getRequestURI()%><br>
        <%
          final Hashtable jndiProperties = new Hashtable();
          jndiProperties.put(Context.URL_PKG_PREFIXES,"org.jboss.ejb.client.naming");
          final Context context = new InitialContext(jndiProperties);
          ICalculadoraLocal calculadora = (ICalculadoraLocal)context.lookup("java:app/ejb-0.0.0.0.0.1/CalculadoraBean!com.getronics.ejb.ICalculadoraLocal");
          int suma = calculadora.suma(2,2);
        %>
        context: <%= context%></br>
        2+2= <%=suma%>
    </body>
</html>

这个可以正常工作

request: /ejb/index.jsp
context: javax.naming.InitialContext@3e532295
2+2= 4

然而当我尝试使用一个线程像这样

<html>
    <body>
        <%@ page import="java.util.Hashtable,javax.naming.*,com.getronics.ejb.*" %>
        request: <%= request.getRequestURI()%><br>
        <%
            new Thread() {
                public void run() {
                    try {
                        final Hashtable jndiProperties = new Hashtable();
                        jndiProperties.put(Context.URL_PKG_PREFIXES,"org.jboss.ejb.client.naming");
                        final Context context = new InitialContext(jndiProperties);
                        ICalculadoraLocal calculadora = (ICalculadoraLocal)context.lookup("java:app/ejb-0.0.0.0.0.1/CalculadoraBean!com.getronics.ejb.ICalculadoraLocal");
                        int suma = calculadora.suma(2,2);
                        System.out.println("suma: " + suma);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                };
            }.start();
        %>
    </body>
</html>

它会抛出一个 NameNotFoundException

javax.naming.NameNotFoundException: java:app/ejb-0.0.0.0.0.1/CalculadoraBean!com.getronics.ejb.ICalculadoraLocal

关于为什么会发生这种情况有什么想法吗
英文:

I'm facing a rather strange behaviour regarding JNDI lookup using Wildfly 18. I have one simple EJB:

package com.getronics.ejb;
import javax.ejb.Remote;
import javax.ejb.Local;
import javax.ejb.Stateless;
@Stateless
@Local(ICalculadoraLocal.class)
public class CalculadoraBean implements ICalculadoraLocal {
public int suma(int a, int b){
return a+b;
}
}

With a simple interface:

package com.getronics.ejb;
public interface ICalculadoraLocal{
int suma(int a, int b);
}

And a simple JSP page to check it out:

&lt;html&gt;
&lt;body&gt;
&lt;%@ page import=&quot;java.util.Hashtable,javax.naming.*,com.getronics.ejb.*&quot; %&gt;
request: &lt;%= request.getRequestURI()%&gt;&lt;br&gt;
&lt;%
final Hashtable jndiProperties = new Hashtable();
jndiProperties.put(Context.URL_PKG_PREFIXES,&quot;org.jboss.ejb.client.naming&quot;);
final Context context = new InitialContext(jndiProperties);
ICalculadoraLocal calculadora = (ICalculadoraLocal)context.lookup(&quot;java:app/ejb-0.0.0.0.0.1/CalculadoraBean!com.getronics.ejb.ICalculadoraLocal&quot;);
int suma = calculadora.suma(2,2);
%&gt;
context: &lt;%= context%&gt;&lt;/br&gt;
2+2= &lt;%=suma%&gt;
&lt;/body&gt;
&lt;/html&gt;

This works fine:

request: /ejb/index.jsp
context: javax.naming.InitialContext@3e532295
2+2= 4

However, when I try to use a Thread, like this:

&lt;html&gt;
&lt;body&gt;
&lt;%@ page import=&quot;java.util.Hashtable,javax.naming.*,com.getronics.ejb.*&quot; %&gt;
request: &lt;%= request.getRequestURI()%&gt;&lt;br&gt;
&lt;%
new Thread() {
public void run() {
try {
final Hashtable jndiProperties = new Hashtable();
jndiProperties.put(Context.URL_PKG_PREFIXES,&quot;org.jboss.ejb.client.naming&quot;);
final Context context = new InitialContext(jndiProperties);
ICalculadoraLocal calculadora = (ICalculadoraLocal)context.lookup(&quot;java:app/ejb-0.0.0.0.0.1/CalculadoraBean!com.getronics.ejb.ICalculadoraLocal&quot;);
int suma = calculadora.suma(2,2);
System.out.println(&quot;suma: &quot; + suma);
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
%&gt;
&lt;/body&gt;
&lt;/html&gt;

It throws a NameNotFoundException:

javax.naming.NameNotFoundException: java:app/ejb-0.0.0.0.0.1/CalculadoraBean!com.getronics.ejb.ICalculadoraLocal

Any ideas on why can this happen?

答案1

得分: 2

似乎使用“global”而不是“app”可以使其工作:

ICalculadoraLocal calculadora = (ICalculadoraLocal)context.lookup("java:global/ear-0.0.0.0.0.1/ejb-0.0.0.0.0.1/CalculadoraBean!com.getronics.ejb.ICalculadoraLocal");

原因似乎是在这些线程中,JNDI查找“app”、“module”和“comp”不起作用,符合规范。

您可以查阅以下链接获取更多信息:

jndi lookup fails in user-thread

[jboss-as7-dev] 非 EE 线程无法访问 java:comp/UserTransaction 的问题

英文:

It seems that using "global" instead of "app" makes it work:

ICalculadoraLocal calculadora = (ICalculadoraLocal)context.lookup(&quot;java:global/ear-0.0.0.0.0.1/ejb-0.0.0.0.0.1/CalculadoraBean!com.getronics.ejb.ICalculadoraLocal&quot;);

The reason seems to be that JNDI lookup of "app", "module" and "comp" won't work in those threads as for specification.

You can check these links for more info:

jndi lookup fails in user-thread

[jboss-as7-dev] Issue with access to java:comp/UserTransaction from non EE threads

huangapple
  • 本文由 发表于 2020年3月17日 00:43:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/60709911.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定