WildFly JNDI查找部署在WAR中的本地EJB [英] WildFly JNDI lookup for local EJB deployed in a WAR

查看:526
本文介绍了WildFly JNDI查找部署在WAR中的本地EJB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WildFly 8.1.0最终版本。



我的应用程序是部署在WAR中的JavaEE Web应用程序(没有EJB模块.ear)。



我想以编程方式使用JNDI以他的名字调用本地EJB。



EJB只是使用@无状态(没有本地或远程接口)



我尝试以下功能:

  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);
//应用程序名称是部署的EJB的应用程序名称。这通常是没有.ear后缀的耳朵名称
//。但是,应用程序名称可能会在服务器上的
// EJB部署的application.xml中被覆盖。
//由于我们没有将应用程序部署为.ear,我们的应用程序名称将是一个空字符串
final String appName =;
//这是服务器上部署的EJB的模块名称。这通常是
// EJB部署的jar名称,没有.jar后缀,但可以通过ejb-jar.xml
覆盖//在本例中,我们已经部署了EJB一个jboss-as-ejb-remote-app.jar,所以模块名称是
// jboss-as-ejb-remote-app
final String moduleName =jboss-as-ejb-remote-应用;
// AS7允许每个部署具有(可选)不同的名称。我们没有为
//我们的EJB部署指定一个不同的名称,所以这是一个空字符串
final String distinctName =;
//默认情况下,EJB名称是bean实现类的简单类名称
final String serviceName = cl.getSimpleName()+Service;
//让我们进行查找
return(CrudService< E>)context.lookup(ejb:+ appName +/+ moduleName +/+ distinctName +/+ serviceName) ;
// return(CrudService< E>)context.lookup(serviceName);
} catch(NamingException e){
log.error(NamingException {},e);
抛出新的RuntimeException(e);
}
}

但它不起作用(obviusly它是为远程EJB)



但是我在WildFly的WAR中找不到本地EJB的任何示例,我不知道该怎么做,我不经常使用JNDI。



我发现了一个使用@Named(value =EjbClassName)注释EJB的工作,并使用JSF调用它们。

  FacesContext context = FacesContext.getCurrentInstance(); 
return context.getApplication()。evaluateExpressionGet(context,#{+ cl.getSimpleName()+Service},CrudService.class);

它的工作,



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

解决方案

谢谢山田JNDI名字出现在服务器日志在启动时



此解决方案适用:

 私人&E扩展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;
}
}

没有使用@LocalBean注释EJB


I'm using WildFly 8.1.0 Final release.

My application is a JavaEE web app deployed in a WAR (there is no EJB module .ear).

I want to programmatically invoke local EJB with his name using JNDI.

The EJB are just annotated with @Stateless (there is no Local or Remote interfaces)

I try below function:

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) ;
    }
}

but it doesn't work (obviusly it's for remote EJB)

But I don't find any example for local EJB in a WAR with WildFly and I have no idea how to do, I don't use JNDI often...

I found a work arround annotating EJB with @Named(value="EjbClassName") and invoke them using JSF.

FacesContext context = FacesContext.getCurrentInstance() ;
return context.getApplication().evaluateExpressionGet(context, "#{"+cl.getSimpleName()+"Service}", CrudService.class) ;

And it's working,

but I'd prefer not using JSF as it's not related to the view layer.

解决方案

Thank you Yamada the JNDI names appear in the server logs at startup

this solution works:

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 ;
    }
}

without annotate EJB with @LocalBean

这篇关于WildFly JNDI查找部署在WAR中的本地EJB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆