Jakarta ee WAR中部署的本地EJB的WildFly JNDI查找

Jakarta ee WAR中部署的本地EJB的WildFly JNDI查找,jakarta-ee,ejb,jndi,wildfly,wildfly-8,Jakarta Ee,Ejb,Jndi,Wildfly,Wildfly 8,我使用的是WildFly 8.1.0最终版本 我的应用程序是一个部署在WAR中的JavaEEWeb应用程序(没有EJB module.ear) 我想使用JNDI以编程方式调用本地EJB EJB只是用@Stateless注释(没有本地或远程接口) 我尝试以下功能: private <E extends DomainObject> CrudService<E> lookUp(Class<E> cl) { try { final Hashta

我使用的是WildFly 8.1.0最终版本

我的应用程序是一个部署在WAR中的JavaEEWeb应用程序(没有EJB module.ear)

我想使用JNDI以编程方式调用本地EJB

EJB只是用@Stateless注释(没有本地或远程接口)

我尝试以下功能:

private <E extends DomainObject> CrudService<E> lookUp(Class<E> cl) {
    try {
        final Hashtable jndiProperties = new Hashtable();
        jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
        final Context context = new InitialContext(jndiProperties);
        // The app name is the application name of the deployed EJBs. This is typically the ear name
        // without the .ear suffix. However, the application name could be overridden in the application.xml of the
        // EJB deployment on the server.
        // Since we haven't deployed the application as a .ear, the app name for us will be an empty string
        final String appName = "";
        // This is the module name of the deployed EJBs on the server. This is typically the jar name of the
        // EJB deployment, without the .jar suffix, but can be overridden via the ejb-jar.xml
        // In this example, we have deployed the EJBs in a jboss-as-ejb-remote-app.jar, so the module name is
        // jboss-as-ejb-remote-app
        final String moduleName = "jboss-as-ejb-remote-app";
        // AS7 allows each deployment to have an (optional) distinct name. We haven't specified a distinct name for
        // our EJB deployment, so this is an empty string
        final String distinctName = "";
        // The EJB name which by default is the simple class name of the bean implementation class
        final String serviceName = cl.getSimpleName() + "Service";
        // let's do the lookup
        return (CrudService<E>) context.lookup("ejb:" + appName + "/" + moduleName + "/" + distinctName + "/" + serviceName );
        //return (CrudService<E>) context.lookup(serviceName);
    } catch (NamingException e) {
        log.error("NamingException {}",e) ;
        throw new RuntimeException(e) ;
    }
}
而且它起作用了


但我不想使用JSF,因为它与视图层无关。

这只是mobile的一个简短回答:
将@LocalBean用于bean的无接口视图。将它添加到无状态会话bean中,您应该可以在服务器的jndi视图中找到它。

谢谢Yamada,jndi名称会在启动时出现在服务器日志中

此解决方案的工作原理是:

private <E extends DomainObject> CrudService<E> lookUp(@NonNull Class<E> entityClass) throws NamingException {
    try {
        final Hashtable jndiProperties = new Hashtable();
        jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
        final Context context = new InitialContext(jndiProperties);
        return (CrudService<E>) context.lookup("java:module/"+entityClass.getSimpleName()+"Service");
    } catch (NamingException e) {
        log.error("lookUp({}) throws NamingException {}",entityClass.getSimpleName(),e.getMessage()) ;
        throw e ;
    }
}
private CrudService lookUp(@NonNull Class entityClass)引发NamingException{
试一试{
最终哈希表jndiProperties=新哈希表();
put(Context.URL_PKG_前缀,“org.jboss.ejb.client.naming”);
最终上下文=新的初始上下文(JNDIProperty);
return(CrudService)context.lookup(“java:module/”+entityClass.getSimpleName()+“Service”);
}捕获(NamingE例外){
错误(“查找({})抛出NamingException{}”、entityClass.getSimpleName()、e.getMessage());
投掷e;
}
}

部署war后,如果不使用@LocalBean注释EJB,WildFly服务器不会记录正在加载的JNDI名称?在glassfish中,我这样做是为了找出正确的查找字符串。。。
private <E extends DomainObject> CrudService<E> lookUp(@NonNull Class<E> entityClass) throws NamingException {
    try {
        final Hashtable jndiProperties = new Hashtable();
        jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
        final Context context = new InitialContext(jndiProperties);
        return (CrudService<E>) context.lookup("java:module/"+entityClass.getSimpleName()+"Service");
    } catch (NamingException e) {
        log.error("lookUp({}) throws NamingException {}",entityClass.getSimpleName(),e.getMessage()) ;
        throw e ;
    }
}