You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by riazb <ma...@riazb.com> on 2009/08/02 22:00:49 UTC

Failing to inject @EJB into a servlet

Hello All,
I'm trying to get my head round OpenEJB and unsure what I'm currently
missing in my test projects.

Basically, I've created a Java project with the necessary libraries (mainly
pointing at OpenEJB/lib) with a Remote interface and an implementation.
I'm actually using CalculatorRemote and CalculatorImpl from the supplied
examples.
Compiled and exported to an EJB3 Jar (MyEJB.jar)

I've got OpenEJB3 working with Tomcat6 - installation is successful and all
tests return OK.
Hence, I've deployed MyEJB.jar using openejb.xml:
<Deployments dir="/Developer/OpenEJB3/apps/" /> (the jar file is in there).

Booted Tomcat within Eclipse and verified openejb.log. Success: MyEJB.jar is
picked up and added.
Extract:
2009-08-02 20:40:43,501 - INFO  - Assembling app:
/Developer/OpenEJB3/apps/MyEJB.jar
2009-08-02 20:40:43,590 - INFO  - Jndi(name=CalculatorImplRemote) -->
Ejb(deployment-id=CalculatorImpl)
2009-08-02 20:40:43,596 - INFO  - Created Ejb(deployment-id=CalculatorImpl,
ejb-name=CalculatorImpl, container=My Stateless Container)
2009-08-02 20:40:43,596 - INFO  - Deployed
Application(path=/Developer/OpenEJB3/apps/MyEJB.jar)

Now this is where I got in trouble:
The docs seem to imply that from there on, my webapps should be able to use
any EJB that OpenEJB3 found and initialized.
Hence I created a Dynamic Web Project on the same Tomcat server with a very
simple servlet called TestServlet and tried to inject the EJB as follows:

@EJB private CalculatorRemote calc;

The servlet is otherwise 'empty'.
I restart Tomcat after after my new Web Project as module. Upon initialising
my project it bombs out. The error log seems to indicate OpenEJB is having
trouble finding the right class (or something like that...).

context path = /MyWeb
java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: java.lang.NoClassDefFoundError: Could not fully load class:
com.riaz.TestServlet
 due to:Lfarm/web/CalculatorRemote;
 in classLoader: 
org.apache.openejb.core.TempClassLoader@5332f20b
	at org.apache.xbean.finder.ClassFinder.<init>(ClassFinder.java:177)
	at
org.apache.openejb.config.AnnotationDeployer$ProcessAnnotatedBeans.createInheritedClassFinder(AnnotationDeployer.java:2958)
	at
org.apache.openejb.config.AnnotationDeployer$ProcessAnnotatedBeans.deploy(AnnotationDeployer.java:909)
	at
org.apache.openejb.config.AnnotationDeployer.deploy(AnnotationDeployer.java:198)
	at
org.apache.openejb.tomcat.catalina.TomcatWebAppBuilder.createWebModule(TomcatWebAppBuilder.java:662)
	at
org.apache.openejb.tomcat.catalina.TomcatWebAppBuilder.loadApplication(TomcatWebAppBuilder.java:541)
	at
org.apache.openejb.tomcat.catalina.TomcatWebAppBuilder.start(TomcatWebAppBuilder.java:242)
	at
org.apache.openejb.tomcat.catalina.GlobalListenerSupport.lifecycleEvent(GlobalListenerSupport.java:58)
	at
org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
	at
org.apache.catalina.core.StandardContext.start(StandardContext.java:4252)
	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
	at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
	at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
	at org.apache.catalina.core.StandardService.start(StandardService.java:516)
	at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
	at org.apache.catalina.startup.Catalina.start(Catalina.java:578)
	... 6 more

I've read the Geronimo Server docs and know it uses a deployment plan
strategy to make the webapp 'aware' of the EJB.
Is there something similar I need to do when using OpenEJB with Tomcat?

Any help would be much much appreciated - thank you.

Riaz


-- 
View this message in context: http://www.nabble.com/Failing-to-inject-%40EJB-into-a-servlet-tp24781514p24781514.html
Sent from the OpenEJB User mailing list archive at Nabble.com.


Re: Failing to inject @EJB into a servlet

Posted by Jonathan Gallimore <jo...@gmail.com>.
Great stuff, I'm glad that works for you. I didn't realise you could you
could do that - thanks for pointing it out :)

Jon

On Tue, Aug 4, 2009 at 11:36 PM, riazb <ma...@riazb.com> wrote:

>
> Hi, thanks for your reply
>
> I tried putting the EJB JAR inside Tomcat's lib folder and it worked.
> No ejb-ref file needed.
>
> That's great news for me as I really want to keep the EJB side of things as
> detached as possible - including deployment.
>
> Riaz
>
>
> Jonathan Gallimore-2 wrote:
> >
> >
> > If your EJBs are deployed separately to your web app, I don't think the
> > deployer will be able to figure out where your EJBs are to inject them
> > into
> > the servlet.
> >
> > I guess you might be able to add some ejb-refs to your web.xml that
> > reference another ejb-jar. I'm thinking along the lines of
> >
> >       <ejb-ref>
> >
> > <ejb-ref-name>com.foo.shapes.SquareBean/orangeRemote</ejb-ref-name>
> >         <ejb-link>colorsApp.jar#OrangeBean</ejb-link>
> >       </ejb-ref>
> >
> > from http://openejb.apache.org/3.0/ejb-refs.html
> >
> >
>
> --
> View this message in context:
> http://www.nabble.com/Failing-to-inject-%40EJB-into-a-servlet-tp24781514p24817514.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>
>

Re: Failing to inject @EJB into a servlet

Posted by riazb <ma...@riazb.com>.
Hi, thanks for your reply

I tried putting the EJB JAR inside Tomcat's lib folder and it worked.
No ejb-ref file needed.

That's great news for me as I really want to keep the EJB side of things as
detached as possible - including deployment.

Riaz


Jonathan Gallimore-2 wrote:
> 
> 
> If your EJBs are deployed separately to your web app, I don't think the
> deployer will be able to figure out where your EJBs are to inject them
> into
> the servlet.
> 
> I guess you might be able to add some ejb-refs to your web.xml that
> reference another ejb-jar. I'm thinking along the lines of
> 
>       <ejb-ref>
>        
> <ejb-ref-name>com.foo.shapes.SquareBean/orangeRemote</ejb-ref-name>
>         <ejb-link>colorsApp.jar#OrangeBean</ejb-link>
>       </ejb-ref>
> 
> from http://openejb.apache.org/3.0/ejb-refs.html
> 
> 

-- 
View this message in context: http://www.nabble.com/Failing-to-inject-%40EJB-into-a-servlet-tp24781514p24817514.html
Sent from the OpenEJB User mailing list archive at Nabble.com.


Re: Failing to inject @EJB into a servlet

Posted by Jonathan Gallimore <jo...@gmail.com>.
Hi Riaz,

I believe we only inject EJBs into servlets if both the EJBs and the
Servlets are in the same application - i.e. your EJBs are inside your .war
file (like the ejb-examples webapp in our examples), your EJB jar is in
WEB-INF/lib (as you've discovered), or if you package your EJB jar and .war
file together in an EAR file.

If your EJBs are deployed separately to your web app, I don't think the
deployer will be able to figure out where your EJBs are to inject them into
the servlet.

I guess you might be able to add some ejb-refs to your web.xml that
reference another ejb-jar. I'm thinking along the lines of

      <ejb-ref>
        <ejb-ref-name>com.foo.shapes.SquareBean/orangeRemote</ejb-ref-name>
        <ejb-link>colorsApp.jar#OrangeBean</ejb-link>
      </ejb-ref>

from http://openejb.apache.org/3.0/ejb-refs.html

I'm only thinking out loud though - I've never tried it. Personally I'd
stick with either your current approach or building an EAR file.

Hope that's some help.

Jon

On Sun, Aug 2, 2009 at 9:13 PM, riazb <ma...@riazb.com> wrote:

>
> Well, I removed the MyEJB.jar from where it was and placed it within the
> WEB-INF/lib folder of my test web project.
> Re-configured OpenEJB via openejb.xml and removed the 'Deployments' tag.
>
> Looking at the logs, OpenEJB picks up the MyEJB.jar automatically from
> within my web project's lib folder and initializes everything.
>
> Now it works.
>
> So, is this a requirement as to where the EJB jar is located? Does it have
> to be within my web project? Or Tomcat's class path maybe?
>
> Thanks
> --
> View this message in context:
> http://www.nabble.com/Failing-to-inject-%40EJB-into-a-servlet-tp24781514p24781640.html
> Sent from the OpenEJB User mailing list archive at Nabble.com.
>
>

Re: Failing to inject @EJB into a servlet

Posted by riazb <ma...@riazb.com>.
Well, I removed the MyEJB.jar from where it was and placed it within the
WEB-INF/lib folder of my test web project.
Re-configured OpenEJB via openejb.xml and removed the 'Deployments' tag.

Looking at the logs, OpenEJB picks up the MyEJB.jar automatically from
within my web project's lib folder and initializes everything.

Now it works.

So, is this a requirement as to where the EJB jar is located? Does it have
to be within my web project? Or Tomcat's class path maybe?

Thanks
-- 
View this message in context: http://www.nabble.com/Failing-to-inject-%40EJB-into-a-servlet-tp24781514p24781640.html
Sent from the OpenEJB User mailing list archive at Nabble.com.