如何通过 Jboss 中的 jndi 名称调用我 war 的 jar 中的 ejb class 7

How to call ejb class which is in a jar of my war by jndi name in Jboss 7

在我的 Web 应用程序中,我使用了 ejb,它作为 jar 存在于我的项目的 lib 文件夹中,ejb classes 和接口不是基于注释的,现在我不知道如何调用那个 ejb class 从我的应用程序通过 jndi 名称,我将我的应用程序部署为 war 文件并使用 jboss 7 EAP 服务器,并且 我无法将 ejb classes 更改为基于注释,因为它是现有的 application.Please 帮助如何实现此目的。

web.xml :-

<ejb-ref>
        <ejb-ref-name>local/com/BusinessService</ejb-ref-name>
        <ejb-ref-type>Session</ejb-ref-type>
        <home>com.common.ejb.service.BusinessServiceLocalHome</home>
        <remote>com.common.ejb.service.BusinessServiceLocal</remote>
        <ejb-link>BusinessService.jar#BusinessService</ejb-link>
    </ejb-ref>

在我正在访问的 java 文件中,如下所示:-

javax.naming.Context ic = new javax.naming.InitialContext();
initCtx.lookup("java:comp/env/local/com/BusinessService");

但我总是得到 javax.naming.NameNotFoundException : local/com/BusinessService

ejb-jar.xml: 这是 businesservice.jar

的 meta-inf 文件夹
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
<ejb-jar id="ejb-jar_ID">
    <display-name>BusinessService</display-name>
    <enterprise-beans>
        <session id="BusinessService">
            <ejb-name>BusinessService</ejb-name>
            <home>com.common.ejb.service.BusinessServiceHome</home>
            <remote>com.common.ejb.service.BusinessService</remote>
            <local-home>com.common.ejb.service.BusinessServiceLocalHome</local-home>
            <local>com.common.ejb.service.BusinessServiceLocal</local>
            <ejb-class>com.common.ejb.service.BusinessServiceBean</ejb-class>
            <session-type>Stateless</session-type>
            <transaction-type>Container</transaction-type>
    </session>
    </enterprise-beans>
<assembly-descriptor>
        <container-transaction>
            <method>
                <ejb-name>BusinessService</ejb-name>
                <method-name>*</method-name>
            </method>
            <trans-attribute>Required</trans-attribute>
        </container-transaction>
    </assembly-descriptor>
</ejb-jar>

--->and also when i am adding <local-home> tag in web.xml its showing error. so i removed the tag

我尝试了很多替代方案,例如 ejb/local/com/BusinessService,但我遇到了同样的问题,请帮助....

注意:- 我正在使用 jboss EAP 服务器

我最终不得不为自己进行 RTFM。

当前的 EJB 3.2 规范有以下内容:

15.4 Enterprise Beans Packaged in a .war file
An enterprise bean class with a component-defining annotation defines an enterprise bean component when packaged within the WEB-INF/classes directory or within a jar file within the WEB-INF/lib directory. An enterprise bean can also be defined via the WEB-INF/ejb-jar.xml deployment descriptor.

A .war file may contain enterprise bean classes in a combination of classes within the WEB-INF/classes directory and one or more jar files within the WEB-INF/lib directory.

A “ejb-jar” file in the WEB-INF/lib directory that contains enterprise beans is not considered an independent Java EE “module” in the way that a .war file, stand-alone ejb-jar file, or an .ear-level ejb-jar file is considered a module. Such an “ejb-jar file” does not define its own module name or its own namespace for ejb-names, environment dependencies, persistence units, etc. All such namespaces are scoped to the enclosing .war file. In that sense, the packaging of enterprise bean classes in an “ejb-jar” file in the WEB-INF/lib directory is merely a convenience. It is semantically equivalent to packaging the classes within WEB-INF/classes directory.

A .war file may contain an ejb-jar.xml file. If present, the ejb-jar.xml is packaged as WEB-INF/ejb-jar.xml. If an ejb-jar.xml is present, it applies to all enterprise beans defined by the .war file, independently of whether they are packaged with the WEB-INF/classes direc- tory or in a jar file within WEB-INF/lib directory. The packaging of an ejb-jar.xml file any- where else within the .war file is not portable and may result in a deployment error.

简短的回答是将 ejb-jar.xml 文件移动到应用程序的 WEB-INF 目录中,就在 web.xml.

旁边

其次,您需要添加您的 ejb-ref 如下:

<ejb-local-ref>
    <ejb-ref-name>local/com/BusinessService</ejb-ref-name>
    <ejb-ref-type>Session</ejb-ref-type>
    <local-home>com.common.ejb.service.BusinessServiceLocalHome</local-home>
    <local>com.common.ejb.service.BusinessServiceLocal</local>
</ejb-local-ref>

随后,以下查找代码工作正常:

Context initialContext = new InitialContext();
Object homeObj = initialContext.lookup("java:comp/env/local/com/BusinessService");
BusinessServiceLocalHome businessServiceLocalHome
     = (BusinessServiceLocalHome) PortableRemoteObject.narrow
                (homeObj, BusinessServiceLocalHome.class);
BusinessServiceLocal businessServiceLocal = businessServiceLocalHome.create();
businessServiceLocal.businessMethod();

我已经在本地环境中验证过了。