EJB Beginner - how to use ti from a web application

Discussions

EJB programming & troubleshooting: EJB Beginner - how to use ti from a web application

  1. Hello,
       I want to access an EJB from my web application - the EJB is deployed on the same app server (JBoss 4.0.2). The EJB deploys without any problems, it's the accesing part I have problems with. Here's part of my code:

    Hashtable env = new Hashtable();


                env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");

                env.put(Context.PROVIDER_URL, "jnp://localhost:1099");

                env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces" );

                

                Context context = new InitialContext(env);

                

                UpdateTimerHome updateTimerHome =

                    (UpdateTimerHome)PortableRemoteObject.narrow(context.lookup("federation/UpdateTimer"),

                            UpdateTimerHome.class);

                

                UpdateTimerRemote updateTimerRemote = updateTimerHome.create();

    And after that I get an exception:
    java.rmi.ServerException: EJBException:; nested exception is:
            javax.ejb.EJBException: Invalid invocation, check your deployment packaging, method=public abstract org.jboss.forge.federation.timer.ejb.UpdateTimerRemote org.jboss.forge.federation.timer.ejb.UpdateTimerHome.create() throws java.rmi.RemoteException,javax.ejb.CreateException

    I don't have anything special in my web application's web.xml, in ejb-jar.xml:
    <ejb-jar>
    <description>Session Bean Stateless</description>
    <display-name>UpdateTimer</display-name>
    <enterprise-beans>
    <session>
    <ejb-name>federation/UpdateTimer</ejb-name>
    <home>package.UpdateTimerHome</home>
    <remote>package.UpdateTimerRemote</remote>
    <ejb-class>package.UpdateTimerBean</ejb-class>
    <session-type>Stateless</session-type>
    <transaction-type>Container</transaction-type>
    </session>
    </enterprise-beans>
    </ejb-jar>

    Where do I make a mistake?
  2. Is the jndi name federation/UpdateTimer that u use the same as in your ejb deployment description file? you have to use a consistent name or else the lookup will fail.
  3. I suppose it is - I checked in the web console, and it's cleary written there: "JNDI: federation/UpdateTimer". Besides, it's not the lookup that fails, it is the invocation of the create() method.
  4. I have written a stand-alone program to to the same thing - that is, lookup and create the EJB - and it works without any problems. So how can you access EJBs from a web application? I guess there's something you have to add to web.xml, but what?
  5. I had the same problem. The only thing you have to do is:
    Copy the file "jbossall-client.jar" (which is located in the $JBOSS_HOME/client folder) in the "lib" folder of your web application. That's all.
  6. I had the same problem, and the prescribed solution (moving jbossall-client.jar into my web-inf/lib directory) worked. BUT... why? I have deployed many other webapps with EJBs, etc... and I've not had this problem before, nor have I used jbossall-client.jar either.

    Thanks for the info,
    jimi
  7. An Easier Way[ Go to top ]

    I found that when posting both my EJB jar file and my War file to the same server, JBoss does not like having the home and remote interface in the War file.

    If I take the home and remote interfaces out of my war file then the code works fine.

    Dawn
  8. The right solution[ Go to top ]

    Thank you Dawn it was the solution. I had the same problem. A funny detail is that the problem doesn't happen in Jboss 4.0.0. Thanks, Carlos
  9. Question[ Go to top ]

    I'm having the same problema, I don't understand something in the solution, if I remove the Beans and remotes classes, ┬┐How am I gonna invoke the methods in the session bean?
  10. Found the solution[ Go to top ]

    OK, I finally understood what the last posts said about taking out the Remote classes from my WAR. It's simple, during codification I had to use the EJB lib because I needed the remote classes to compile the project, right? But When I was creating the WAR, I just removed that lib from the WAR archive with help of my IDE (i used JBuilder 2007), and that was it.