geronimo-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Aaron Mulder" <ammul...@alumni.princeton.edu>
Subject Re: NameNotFoundException with EJB created with Eclipse Geronimo plugin
Date Sun, 06 Aug 2006 20:11:17 GMT
All right, this is just scratching the surface, but...

In order for a web application to access an EJB, you need to add an
"EJB Reference" to the web application for each EJB that the web
application is going to use.  The EJB reference puts the EJB in the
web application's private JNDI space.  There's one last wrinkle, which
is that an EJB may have home & remote interfaces, localhome & local
interface, or all 4.  For a web application that's going to use EJBs,
you'll want to use the localhome & local interfaces, because the
performance is better (the local approach only works for clients in
the same JVM as the EJB).  I mention that because technically there's
different syntax to create a remote EJB reference and a local EJB
reference for the web application, and I'm only going to describe the
local syntax here.

Now, your Tiger EJB already has local home and local interfaces, so
everything's fine on the EJB side.  To set up the EJB reference, add a
block to your web.xml like this:

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4">
    ...
    <ejb-local-ref>
        <ejb-ref-name>ejb/Tiger</ejb-ref-name>
        <ejb-ref-type>Session</ejb-ref-type>
        <local-home>com.zoo.example.TigerLocalHome</local-home>
        <local>com.zoo.example.TigerLocal</local>
        <ejb-link>Tiger</ejb-link>
    </ejb-local-ref>
</web-app>

So long as your web application and the EJB are in the same EAR, this
is all you need.  If they were separated, you couldn't necessarily use
the ejb-link element and you might need to add some information to
geronimo-web.xml instead.

In the block above, the ejb-ref-name controls where in JNDI the EJB
will appear, the next three elements have to match the similar fields
in the ejb-jar.xml for this EJB, and the ejb-link must match the
ejb-name for that EJB in the ejb-jar.xml.  The exact JNDI location the
web app should use to access the EJB is "java:comp/env/" plus the
value of the ejb-ref-name -- so in this case, it would be
"java:comp/env/ejb/Tiger".

Now, I'm not sure what your TigerUtil class does, but this is what the
code would look like for a web application to use this EJB:

Context ctx = new InitialContext();
TigerLocalHome home = (TigerLocalHome)ctx.lookup("java:comp/env/ejb/Tiger");
TigerLocal tiger = home.create();

The TigerUtil class probably hides some of this by declaring a
constant for the JNDI location of the EJB, and so on.  But you're
calling the wrong method on it if it returns a TigerHome -- you want a
method that returns a TigerLocalHome, as I mentioned above.  If
TigerUtil is a generated class and has a different JNDI location than
java:comp/env/ejb/Tiger to look up the TigerLocalHome, then you can
change the ejb-ref-name in your web.xml so the JNDI location will
match whatever TigerUtil is using.

Thanks,
     Aaron

On 8/6/06, Four Sticks <fourstix@gmail.com> wrote:
> Hi,
>
> I have eclipse 3.2, the Callisto version of WebTools, JDK 1.5 and the
> Geronimo 1.1 plugin.  I'm able to create and run serverlets and JSP's
> just fine to Geronimo, but I'd like to learn about EJB's.
>
> I'm using this tutorial as a guide:
> http://www.eclipse.org/webtools/community/tutorials/ejbtutorial/buildingejbs.html
>
> Following this tutorial, I created an EJB project named Zoo, a Web
> project named ZooWeb and an EAR named ZooEAR. In the Zoo EJB project
> is an EJB named TigerBean.  The plugin created a ZooClient project. In
> the Web project is a JSP page named Test.jsp which gets the EJB and
> calls a method on it.
>
> It looked like everything created fine, but when I run the project on
> the Geronimo server. The JSP page is failing on the following line of
> java code:
>
> com.zoo.example.TigerHome home = com.zoo.example.TigerUtil.getHome();
>
> with the following error:
>
> javax.naming.NameNotFoundException: Tiger
>
> My question is what do I need to do so the JSP page can find the EJB?
>
> Here is the web.xml file creatd by the plugin:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
> http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
>  <display-name>
>  ZooWeb</display-name>
>  <welcome-file-list>
>   <welcome-file>index.html</welcome-file>
>  </welcome-file-list>
> </web-app>
>
> There is also a geronimo-web.xml file:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-1.1"
> xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.1"
> xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.1"
> xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.1">
>   <sys:environment>
>     <sys:moduleId>
>       <sys:groupId>default</sys:groupId>
>       <sys:artifactId>ZooWeb</sys:artifactId>
>       <sys:version>1.0</sys:version>
>       <sys:type>car</sys:type>
>     </sys:moduleId>
>   </sys:environment>
>   <context-root>/ZooWeb</context-root>
> </web-app>
>
> Do I need to add something to these two files? I'd appreciate any help
> at all.  I'm not to familiar with EJB's so I'm not sure what I need to
> do so that the JSP page can find them.
>
> In the Zoo Ejb project the ejb-jar.xml file has the following:
> <?xml version="1.0" encoding="UTF-8"?>
> <ejb-jar id="ejb-jar_1" xmlns="http://java.sun.com/xml/ns/j2ee"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
> http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd" version="2.1">
>    <description><![CDATA[Zoo generated by eclipse wtp xdoclet
> extension.]]></description>
>    <display-name>Zoo</display-name>
>
>    <enterprise-beans>
>
>       <!-- Session Beans -->
>       <session id="Session_Tiger">
>          <description><![CDATA[An EJB named Tiger]]></description>
>          <display-name>Tiger</display-name>
>
>          <ejb-name>Tiger</ejb-name>
>
>          <home>com.zoo.example.TigerHome</home>
>          <remote>com.zoo.example.Tiger</remote>
>          <local-home>com.zoo.example.TigerLocalHome</local-home>
>          <local>com.zoo.example.TigerLocal</local>
>          <ejb-class>com.zoo.example.TigerSession</ejb-class>
>          <session-type>Stateless</session-type>
>          <transaction-type>Container</transaction-type>
>
>       </session>
>
>      <!--
>        To add session beans that you have deployment descriptor info for, add
>        a file to your XDoclet merge directory called session-beans.xml
> that contains
>        the <session></session> markup for those beans.
>      -->
>
>       <!-- Entity Beans -->
>      <!--
>        To add entity beans that you have deployment descriptor info for, add
>        a file to your XDoclet merge directory called entity-beans.xml
> that contains
>        the <entity></entity> markup for those beans.
>      -->
>
>       <!-- Message Driven Beans -->
>      <!--
>        To add message driven beans that you have deployment descriptor
> info for, add
>        a file to your XDoclet merge directory called
> message-driven-beans.xml that contains
>        the <message-driven></message-driven> markup for those beans.
>      -->
>
>    </enterprise-beans>
>
>    <!-- Relationships -->
>
>    <!-- Assembly Descriptor -->
>      <!--
>        To specify your own assembly descriptor info here, add a file to your
>        XDoclet merge directory called assembly-descriptor.xml that contains
>        the <assembly-descriptor></assembly-descriptor> markup.
>      -->
>
>    <assembly-descriptor id="AssemblyDescriptor_1">
>      <!--
>        To specify additional security-role elements, add a file in the merge
>        directory called ejb-security-roles.xml that contains them.
>      -->
>
>    <!-- method permissions -->
>      <!--
>        To specify additional method-permission elements, add a file in the merge
>        directory called ejb-method-permissions.ent that contains them.
>      -->
>
>    <!-- transactions -->
>      <!--
>        To specify additional container-transaction elements, add a
> file in the merge
>        directory called ejb-container-transactions.ent that contains them.
>      -->
>
>    <!-- finder transactions -->
>
>    <!-- message destinations -->
>      <!--
>        To specify additional message-destination elements, add a file
> in the merge
>        directory called ejb-message-destinations.ent that contains them.
>      -->
>
>    <!-- exclude list -->
>      <!--
>        To specify an exclude-list element, add a file in the merge directory
>        called ejb-exclude-list.xml that contains it.
>      -->
>    </assembly-descriptor>
>
>    <ejb-client-jar>ZooClient.jar</ejb-client-jar>
>
> </ejb-jar>
>
> And there is also an openejb-jar.xml file in the EJB projcect with the
> following:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <openejb-jar xmlns="http://www.openejb.org/xml/ns/openejb-jar-2.1"
> xmlns:nam="http://geronimo.apache.org/xml/ns/naming-1.1"
> xmlns:pkgen="http://www.openejb.org/xml/ns/pkgen-2.0"
> xmlns:sec="http://geronimo.apache.org/xml/ns/security-1.1"
> xmlns:sys="http://geronimo.apache.org/xml/ns/deployment-1.1">
>   <sys:environment>
>     <sys:moduleId>
>       <sys:groupId>default</sys:groupId>
>       <sys:artifactId>Zoo</sys:artifactId>
>       <sys:version>1.0</sys:version>
>       <sys:type>car</sys:type>
>     </sys:moduleId>
>   </sys:environment>
>   <enterprise-beans/>
> </openejb-jar>
>
> Any help at all would be gratefully appreciated it.
>
> Thanks,
> Gaston
>

Mime
View raw message