英文:
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:
<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>
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:
<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>
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("java:global/ear-0.0.0.0.0.1/ejb-0.0.0.0.0.1/CalculadoraBean!com.getronics.ejb.ICalculadoraLocal");
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论