0
votes

In my web application i used ejb which is present in lib folder of my project as a jar and the ejb classes and interfaces are not annotationed based now i dont know how to call that ejb class from my application by through jndi name, i am deploying my application as a war file and using jboss 7 EAP server, and I can not change the ejb classes to annotation based also as it is a existing application.Please help how to achieve this.

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>

in my java file i am accessing like below:-

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

but always i am getting javax.naming.NameNotFoundException : local/com/BusinessService

ejb-jar.xml: which is is meta-inf folder of businesservice.jar

<?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

i tried with lots of alternative like ejb/local/com/BusinessService but i am facing same issue please help ....

Note:- I am using jboss EAP Server

1
Does your BusinessService.jar contain an ejb-jar.xml file? Please add the xml fragment that describes your BusinessService to your questionSteve C
HI @SteveC i added the ejb-jar.xml which is there in my businessservice.jar in meta-inf folder.Goutam

1 Answers

1
votes

I eventually had to RTFM this for myself.

The current EJB 3.2 specification has the following to say:

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.

The short answer is to move the ejb-jar.xml file into the WEB-INF directory of your application, right next to the web.xml.

Secondly, you need to add your ejb-ref as follows:

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

Subsequently, the following lookup code works just fine:

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

I have verified this in a local environment.