2 Replies Latest reply on May 8, 2007 7:13 PM by neisan

    Problem running Stateless bean with JBOSS 4.0.4 GA and EJB 3

    neisan

      Hi there,

      I?m trying to run na EJB application developed with JBOSS IDE 2.0 and JBOSS 4.0.4 GA. I have an remote stateless session bean named TestStatelessBean that inherit from TestStateless. Here is the code for them:

      package com.test.ejbs;

      import javax.ejb.Remote;

      @Remote
      public interface TestStateless {
      public String testBean();
      }

      ----------------------------------------------------------------------------
      package com.test.ejbs;

      import javax.ejb.*;
      import com.test.ejbs.TestStateless;

      public @Stateless class TestStatelessBean implements TestStateless {

      public String testBean() {
      return "the server returned this string";
      }

      }
      ------------------------------------------------------------------------------

      I have a servlet that calls the EJB:

      package com.test.web;

      import javax.servlet.*;
      import javax.servlet.http.*;
      import javax.naming.*;
      import java.io.*;

      import com.test.ejbs.*;

      public class TestStatelessEJBServlet extends HttpServlet {

      /**
      *
      */
      private static final long serialVersionUID = 1L;
      private TestStatelessBean statelessBean;

      public void init() {
      try {
      Context context = new InitialContext();
      System.out.println("Before lookup");
      // statelessBean = (TestStatelessBean) context.lookup("TestStatelessBean/remote");
      statelessBean = (TestStatelessBean) context.lookup(TestStatelessBean.class.getName());
      System.out.println("After lookup");

      } catch (NamingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      }

      public void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
      PrintWriter writer = resp.getWriter();
      writer.write("The stateless bean returned this string: " +
      statelessBean.testBean());
      }

      public void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
      this.doGet(req, resp);
      }
      }

      --------------------------------------------------------------------

      If I try to get the bean with the code ?statelessBean = (TestStatelessBean) context.lookup("TestStatelessBean/remote");? I get a class cast exception.

      If try to get the bean with the code ?statelessBean = (TestStatelessBean) context.lookup(TestStatelessBean.class.getName());? I get a Bean not bound exception.

      I deployed my bean with the JBOSS IDE in the files TestInstallation.ejb3 and TetsInstallation.war. My project name is ejb3teste. These two files are in $JBOSS_HOME\server\default\deploy. The content of jndi.properties is:

      java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
      java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
      java.naming.provider.url=localhost:1099

      The web.xml file contains:

      <?xml version="1.0" encoding="UTF-8"?>
      <web-app 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">

      <servlet-name>TestStatelessEJBServlet</servlet-name>
      <servlet-class>com.test.web.TestStatelessEJBServlet</servlet-class>

      <servlet-mapping>
      <servlet-name>TestStatelessEJBServlet</servlet-name>
      <url-pattern>/testStatelessEJB</url-pattern>
      </servlet-mapping>
      </web-app>

      The content of the file packaging-buid.xml is:

      ?xml version="1.0" encoding="UTF-8"?
      project name="Packaging Generator" "default="_packaging_generation_"
      target name="_packaging_generation_" depends="N65540,N65565"
      target name="N65540" description="TestInstallation.ejb3"
      jar destfile="TestInstallation.ejb3"
      zipfileset dir="src"
      include name="jndi.properties"
      zipfileset
      zipfileset dir="bin" includes="**/ejbs/*"
      jar
      target
      target name="N65565" description="TestInstallation.war
      jar destfile="TestInstallation.war"
      zipfileset dir="src" prefix="WEB-INF"
      include name="web.xml"
      zipfileset
      zipfileset dir="bin" prefix="WEB-INF/classes" includes="**/web/*"
      jar
      target
      project

      In the jmx-console I have:

      jboss.j2ee

      * jar=TestInstallation.ejb3,name=TestStatelessBean,service=EJB3
      * module=TestInstallation.ejb3,service=EJB3
      * service=ClientDeployer
      * service=EARDeployer

      jboss.web

      * J2EEApplication=none,J2EEServer=none,WebModule=//localhost/TestInstallation,j2eeType=Servlet,name=TestStatelessEJBServlet

      ----------------------------------------------------------

      The JNDI Namespace

      Global JNDI Namespace

      +- XAConnectionFactory (class: org.jboss.mq.SpyXAConnectionFactory)
      +- TestStatelessBean (class: org.jnp.interfaces.NamingContext)
      | +- remote (proxy: $Proxy58 implements interface com.test.ejbs.TestStateless,interface org.jboss.ejb3.JBossProxy,interface javax.ejb.EJBObject)

      ----------------------------------------------------------------------

      I?m calling the servlet with the command line ?http://mymachine:8080/TestInstallation/testStatelessEJB?. I have to use this URL because the URL ?http://localhost:8080/TestInstallation/testStatelessEJB? doesn?t work. Do you think this can be the cause of the error?

      Thank you very much.

      Nei

        • 1. Re: Problem running Stateless bean with JBOSS 4.0.4 GA and E
          delkant

          this:

          context.lookup("TestStatelessBean/remote");

          is the rigth way... but here you will recive a remote interface, that is what you are requesting with the "/remote" part...

          so.. you have to cast it to ....

          (TestStateless)context.lookup("TestStatelessBean/remote");

          your servlet have tobe something like this:

          public class TestStatelessEJBServlet extends HttpServlet {
          
          /**
          *
          */
          private static final long serialVersionUID = 1L;
          private TestStateless testRemoteInterface;
          
          public void init() {
          try {
          Context context = new InitialContext();
          System.out.println("Before lookup");
          testRemoteInterface = (TestStateless) context.lookup("TestStatelessBean/remote");
          System.out.println("After lookup");
          
          } catch (NamingException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
          }
          }
          
          public void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
          PrintWriter writer = resp.getWriter();
          writer.write("The stateless bean returned this string: " +
          TestStateless.testBean());
          }
          
          public void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
          this.doGet(req, resp);
          }
          }


          • 2. Re: Problem running Stateless bean with JBOSS 4.0.4 GA and E
            neisan

            Hi delkant,

            I was trying to run this application for several days without sucess. Now, after your tips, it worked fine.


            Thank you very very much.

            Nei