Uploaded image for project: 'EJB 3.0'
  1. EJB 3.0
  2. EJBTHREE-456

ClassCastException while using JNDI lookup for stateful session bean

    XMLWordPrintable

Details

    • Bug
    • Resolution: Won't Do
    • Minor
    • None
    • EJB 3.0 RC5 - PFD
    • None
    • None
    • Compatibility/Configuration

    Description

      PLEASE NOTE: this code works when the WAR file (servlet) deployed on a REMOTE JBoss, the problem is when the WAR file is in the same deploy directory of the EAR file (ejb) but not in the EAR itself.
      If it was in the EAR itself, it works.

      package test.client;

      import java.io.IOException;

      import javax.naming.InitialContext;
      import javax.naming.NamingException;
      import javax.servlet.GenericServlet;
      import javax.servlet.ServletException;
      import javax.servlet.ServletRequest;
      import javax.servlet.ServletResponse;

      import test.common.UserContext;

      public class SimpleServlet extends GenericServlet {

      private static final long serialVersionUID = 1L;

      public void service(ServletRequest req, ServletResponse res)
      throws ServletException, IOException {

      System.out.println("service called (Service)");

      String retVal = "FAILED";
      try

      { InitialContext ctx = new InitialContext(); ctx.addToEnvironment("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); ctx.addToEnvironment("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces"); ctx.addToEnvironment("java.naming.provider.url","outsourcing1"); UserContext userContext = (UserContext) ctx.lookup("myear/usercontext/remote"); retVal = "Calling login: " + userContext.login(); }

      catch (NamingException e)

      { e.printStackTrace(); }

      res.getWriter().println(
      "<HTML>" +
      "<HEADER><TITLE>Test EJB3</TITLE></HEADER>" +
      "<BODY>" +
      "<P>" + retVal +
      "</P>" +
      "</BODY>" +
      "</HTML>");

      }
      }

      package test.server;

      import java.io.Serializable;
      import java.util.Date;

      import javax.ejb.Stateful;

      import test.common.UserContextRemote;

      @Stateful(name="usercontext")
      public class UserContextBean implements UserContextRemote, Serializable {

      /**

      • */
        private static final long serialVersionUID = 5825562882270540623L;
        private Date loginTime = null;

      public String login() {
      if (loginTime == null)

      { loginTime = new Date(); return "Welcome " + toString(); }

      else

      { return "You already login " + toString(); }

      }

      }

      package test.common;

      import javax.ejb.Remote;

      @Remote
      public interface UserContextRemote extends UserContext {

      }

      package test.common;

      public interface UserContext {

      public String login();

      }

      java.lang.ClassCastException: $Proxy49
      at test.client.SimpleServlet.service(SimpleServlet.java:30)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)

      Attachments

        Activity

          People

            Unassigned Unassigned
            ohadpinchevsky Ohad Pinchevsky (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: