Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Geronimo 3:EJB查找导致NamingException_Java_Ejb_Jndi_Openejb_Geronimo - Fatal编程技术网

Java Geronimo 3:EJB查找导致NamingException

Java Geronimo 3:EJB查找导致NamingException,java,ejb,jndi,openejb,geronimo,Java,Ejb,Jndi,Openejb,Geronimo,我刚刚在Eclipse中用web模块和EJB3模块创建了一个简单的企业应用程序项目 这是我的EJB: @Stateless public class MyBean implements MyBeanRemote { public String getGreeting(){return "Hello World!";} } @Remote public interface MyBeanRemote { String getGreeting(); } @WebServlet("/

我刚刚在Eclipse中用web模块和EJB3模块创建了一个简单的企业应用程序项目

这是我的EJB

@Stateless
public class MyBean implements MyBeanRemote {
    public String getGreeting(){return "Hello World!";}
}
@Remote
public interface MyBeanRemote {
    String getGreeting();
}
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        MyBeanRemote myBean = null;

        try {
            myBean=(MyBeanRemote) new InitialContext().lookup("XXX");                //1
        } catch (NamingException e) {e.printStackTrace();}

        response.getWriter().append(myBean.getGreeting());
    }
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ejb:openejb-jar xmlns:app="http://geronimo.apache.org/xml/ns/j2ee/application-2.0" xmlns:bp="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:client="http://geronimo.apache.org/xml/ns/j2ee/application-client-2.0" xmlns:conn="http://geronimo.apache.org/xml/ns/j2ee/connector-1.2" xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.2" xmlns:ejb="http://openejb.apache.org/xml/ns/openejb-jar-2.2" xmlns:jaspi="http://geronimo.apache.org/xml/ns/geronimo-jaspi" xmlns:log="http://geronimo.apache.org/xml/ns/loginconfig-2.0" xmlns:name="http://geronimo.apache.org/xml/ns/naming-1.2" xmlns:pers="http://java.sun.com/xml/ns/persistence" xmlns:pkgen="http://openejb.apache.org/xml/ns/pkgen-2.1" xmlns:sec="http://geronimo.apache.org/xml/ns/security-2.0" xmlns:web="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1">
    <dep:environment>
        <dep:moduleId>
            <dep:groupId>default</dep:groupId>
            <dep:artifactId>MyProjectEJB</dep:artifactId>
            <dep:version>1.0</dep:version>
            <dep:type>car</dep:type>
        </dep:moduleId>
    </dep:environment>
</ejb:openejb-jar>
这是我的远程业务界面

@Stateless
public class MyBean implements MyBeanRemote {
    public String getGreeting(){return "Hello World!";}
}
@Remote
public interface MyBeanRemote {
    String getGreeting();
}
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        MyBeanRemote myBean = null;

        try {
            myBean=(MyBeanRemote) new InitialContext().lookup("XXX");                //1
        } catch (NamingException e) {e.printStackTrace();}

        response.getWriter().append(myBean.getGreeting());
    }
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ejb:openejb-jar xmlns:app="http://geronimo.apache.org/xml/ns/j2ee/application-2.0" xmlns:bp="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:client="http://geronimo.apache.org/xml/ns/j2ee/application-client-2.0" xmlns:conn="http://geronimo.apache.org/xml/ns/j2ee/connector-1.2" xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.2" xmlns:ejb="http://openejb.apache.org/xml/ns/openejb-jar-2.2" xmlns:jaspi="http://geronimo.apache.org/xml/ns/geronimo-jaspi" xmlns:log="http://geronimo.apache.org/xml/ns/loginconfig-2.0" xmlns:name="http://geronimo.apache.org/xml/ns/naming-1.2" xmlns:pers="http://java.sun.com/xml/ns/persistence" xmlns:pkgen="http://openejb.apache.org/xml/ns/pkgen-2.1" xmlns:sec="http://geronimo.apache.org/xml/ns/security-2.0" xmlns:web="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1">
    <dep:environment>
        <dep:moduleId>
            <dep:groupId>default</dep:groupId>
            <dep:artifactId>MyProjectEJB</dep:artifactId>
            <dep:version>1.0</dep:version>
            <dep:type>car</dep:type>
        </dep:moduleId>
    </dep:environment>
</ejb:openejb-jar>
这是我的Servlet

@Stateless
public class MyBean implements MyBeanRemote {
    public String getGreeting(){return "Hello World!";}
}
@Remote
public interface MyBeanRemote {
    String getGreeting();
}
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        MyBeanRemote myBean = null;

        try {
            myBean=(MyBeanRemote) new InitialContext().lookup("XXX");                //1
        } catch (NamingException e) {e.printStackTrace();}

        response.getWriter().append(myBean.getGreeting());
    }
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ejb:openejb-jar xmlns:app="http://geronimo.apache.org/xml/ns/j2ee/application-2.0" xmlns:bp="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:client="http://geronimo.apache.org/xml/ns/j2ee/application-client-2.0" xmlns:conn="http://geronimo.apache.org/xml/ns/j2ee/connector-1.2" xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.2" xmlns:ejb="http://openejb.apache.org/xml/ns/openejb-jar-2.2" xmlns:jaspi="http://geronimo.apache.org/xml/ns/geronimo-jaspi" xmlns:log="http://geronimo.apache.org/xml/ns/loginconfig-2.0" xmlns:name="http://geronimo.apache.org/xml/ns/naming-1.2" xmlns:pers="http://java.sun.com/xml/ns/persistence" xmlns:pkgen="http://openejb.apache.org/xml/ns/pkgen-2.1" xmlns:sec="http://geronimo.apache.org/xml/ns/security-2.0" xmlns:web="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1">
    <dep:environment>
        <dep:moduleId>
            <dep:groupId>default</dep:groupId>
            <dep:artifactId>MyProjectEJB</dep:artifactId>
            <dep:version>1.0</dep:version>
            <dep:type>car</dep:type>
        </dep:moduleId>
    </dep:environment>
</ejb:openejb-jar>
Bean注入工作起来很有魅力,但我无法成功执行JDNI查找:应该在第
//1
行插入什么而不是
XXX
?我尝试过任何可能的组合(好吧,显然错过了正确的组合…)

据我所知,Geronimo使用OpenEJB将名称映射到资源,您可以阅读

默认JNDI名称的格式如下:
{deploymentId}{interfaceType.annotationName}

所以我以为这只是我的BeanRemote,但那不起作用。。。我错过了什么?我把最后的几个小时都花在了失败的尝试上,并在这里阅读了类似的答案

这是我的openejb.jar.xml

@Stateless
public class MyBean implements MyBeanRemote {
    public String getGreeting(){return "Hello World!";}
}
@Remote
public interface MyBeanRemote {
    String getGreeting();
}
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        MyBeanRemote myBean = null;

        try {
            myBean=(MyBeanRemote) new InitialContext().lookup("XXX");                //1
        } catch (NamingException e) {e.printStackTrace();}

        response.getWriter().append(myBean.getGreeting());
    }
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ejb:openejb-jar xmlns:app="http://geronimo.apache.org/xml/ns/j2ee/application-2.0" xmlns:bp="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:client="http://geronimo.apache.org/xml/ns/j2ee/application-client-2.0" xmlns:conn="http://geronimo.apache.org/xml/ns/j2ee/connector-1.2" xmlns:dep="http://geronimo.apache.org/xml/ns/deployment-1.2" xmlns:ejb="http://openejb.apache.org/xml/ns/openejb-jar-2.2" xmlns:jaspi="http://geronimo.apache.org/xml/ns/geronimo-jaspi" xmlns:log="http://geronimo.apache.org/xml/ns/loginconfig-2.0" xmlns:name="http://geronimo.apache.org/xml/ns/naming-1.2" xmlns:pers="http://java.sun.com/xml/ns/persistence" xmlns:pkgen="http://openejb.apache.org/xml/ns/pkgen-2.1" xmlns:sec="http://geronimo.apache.org/xml/ns/security-2.0" xmlns:web="http://geronimo.apache.org/xml/ns/j2ee/web-2.0.1">
    <dep:environment>
        <dep:moduleId>
            <dep:groupId>default</dep:groupId>
            <dep:artifactId>MyProjectEJB</dep:artifactId>
            <dep:version>1.0</dep:version>
            <dep:type>car</dep:type>
        </dep:moduleId>
    </dep:environment>
</ejb:openejb-jar>

违约
MyProjectEJB
1
汽车
由向导创建。

作为用户achabahe,在这些帮助下,我打印了整个JNDI树:

String getJNDITree() {
    Context context=null;
    try {
        context = new InitialContext();
    } catch (NamingException e1) {e1.printStackTrace();}

    List<String>roots=Arrays.asList("java:comp","java:module","java:app","java:global");

    StringBuilder tree=new StringBuilder();
    for(String root:roots)
        recursiveDeepVisit(context,root,"",tree);   

    return tree.toString()+"\n";
}

void recursiveDeepVisit(Context context,String path,String prefix,StringBuilder tree){
    tree.append("\n"+prefix+path);
    try {
        NamingEnumeration<NameClassPair> list = context.list(path);
        while (list.hasMore()) {
            String innerPath = path+"/"+list.next().getName();
            recursiveDeepVisit(context, innerPath, "    "+prefix,tree);
        }
    } catch (Exception e) {}
}
原来

MyBeanRemote myBean=(MyBeanRemote) new InitialContext().lookup("java:app/MyProjectEJB/MyBean!com.dev.bean.MyBeanRemote"); 

是我一直在寻找的解决方案。

您可以省略jndi上下文的内容;这将登记树的第一个级别,您可以从那里开始更深一步