7 Replies Latest reply on Apr 15, 2009 6:10 AM by uday reddy

    ClassCastException in Stateful Session-Bean

    Andreas Bachmann Newbie

      I'm a beginner and would like to run some JBoss EJB 3.0 examples:
      Calculator (Stateless SessionBean) - OK!
      ShoppingCart (Stateful SessionBean) - ClassCastException

      Environment:
      o Java 1.5.0_09
      o IntelliJ IDEA 6.0.4
      o JBoss 4.0.5 AS (JEMS Installer with ejb3)
      o Windows XP SP2

      Interfaces:

      @Local
      @Remote
      public interface Calculator {
       int add(int x, int y);
       int subtract(int x, int y);
      }
      
      @Local
      @Remote
      public interface ShoppingCart {
       void buy(String product, int quantity);
       HashMap<String, Integer> getCartContents();
      }
      


      SessionBeans:
      @Stateless
      public class CalculatorBean implements Calculator {
       public int add(int x, int y) {
       return x + y;
       }
      
       public int subtract(int x, int y) {
       return x - y;
       }
      }
      
      @Stateful
      public class ShoppingCartBean implements ShoppingCart {
       private HashMap<String, Integer> cart;
      
       public ShoppingCartBean() {
       cart = new HashMap<String, Integer>();
       }
      
       public void buy(String product, int quantity) {
       if (cart.containsKey(product)) {
       int currq = cart.get(product);
       currq += quantity;
       cart.put(product, currq);
       } else {
       cart.put(product, quantity);
       }
       }
      
       public HashMap<String, Integer> getCartContents() {
       return cart;
       }
      }
      


      Client:
      public class Client {
       public static void main(String[] args) throws Exception {
       System.setSecurityManager(new RMISecurityManager());
      
       InitialContext ctx = new InitialContext();
       ctx.addToEnvironment(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
       ctx.addToEnvironment(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
       ctx.addToEnvironment(Context.PROVIDER_URL, "jnp://localhost:1099");
      
       Calculator calculator = (Calculator) ctx.lookup("MyApplication/CalculatorBean/remote");
      
       System.out.println("1 + 1 = " + calculator.add(1, 1));
       System.out.println("1 - 1 = " + calculator.subtract(1, 1));
       }
      }
      
      public class Client {
       public static void main(String[] args) throws Exception {
       System.setSecurityManager(new RMISecurityManager());
      
       InitialContext ctx = new InitialContext();
       ctx.addToEnvironment(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
       ctx.addToEnvironment(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
       ctx.addToEnvironment(Context.PROVIDER_URL, "jnp://localhost:1099");
      
       ShoppingCart cart = (ShoppingCart) ctx.lookup("MyApplication/ShoppingCartBean/remote");
       }
      }
      


      Policy:
      grant {
       permission java.security.AllPermission;
      };
      


      JBoss LOG:
      [...]
      16:34:50,404 INFO [JmxKernelAbstraction] installing MBean: jboss.j2ee:ear=MyApplication.ear,jar=MyEjb.jar,name=ShoppingCartBean,service=EJB3 with dependencies:
      16:34:50,419 INFO [EJBContainer] STARTED EJB: org.jboss.tutorial.stateful.bean.ShoppingCartBean ejbName: ShoppingCartBean
      16:34:50,466 INFO [SimpleStatefulCache] Initializing SimpleStatefulCache with maxSize: 100000 timeout: 300 for jboss.j2ee:ear=MyApplication.ear,jar=MyEjb.jar,name=ShoppingCartBean,service=EJB3
      16:34:50,466 INFO [JmxKernelAbstraction] installing MBean: jboss.j2ee:ear=MyApplication.ear,jar=MyEjb.jar,name=CalculatorBean,service=EJB3 with dependencies:
      16:34:50,466 INFO [EJBContainer] STARTED EJB: org.jboss.tutorial.stateless.bean.CalculatorBean ejbName: CalculatorBean
      [...]
      


      Command:
      java \
      -Djava.security.policy=java.policy \
      -classpath jbossall-client.jar;jboss-aop-jdk50-client.jar" \
      org.jboss.tutorial.stateless.client.Client
      
      1 + 1 = 2
      1 - 1 = 0
      
      java \
      -Djava.security.policy=java.policy \
      -classpath jbossall-client.jar;jboss-aop-jdk50-client.jar" \
      org.jboss.tutorial.stateful.client.Client
      
      Exception in thread "main" java.lang.ClassCastException: javax.naming.Reference
       at org.jboss.tutorial.stateful.client.Client.main(Client.java:20)
      


        • 2. Re: ClassCastException in Stateful Session-Bean
          Andreas Bachmann Newbie

           

          public interface ShoppingCart {
           void buy(String product, int quantity);
           HashMap<String, Integer> getCartContents();
          }
          
          @Local
          public interface ShoppingCartLocal extends ShoppingCart {
          }
          
          @Remote
          public interface ShoppingCartRemote extends ShoppingCart {
          }
          
          @Stateful
          public class ShoppingCartBean implements ShoppingCartLocal, ShoppingCartRemote {
           [...]
          }
          


          Same Exception:

          Exception in thread "main" java.lang.ClassCastException: javax.naming.Reference
           at org.jboss.tutorial.stateful.client.Client.main(Client.java:18)
          



          My structure:
          MyApplication.ear
          +--META-INF
          | +--application.xml
          +--MyEjb.jar
          | +--META-INF
          | | +--persistence.xml
          | | +--jbosscmp-jdbc.xml
          | +--org.jboss.tutorial.stateful.bean (Stateful SessionBean)
          | +--org.jboss.tutorial.stateful.client (Client)
          | +--org.jboss.tutorial.stateless.bean (Stateless SessionBean)
          | +--org.jboss.tutorial.stateless.client (Client)
          +--MyWebApp.war
           +--WEB-INF
           | +--web.xml
           +--index.jsp
          



          • 3. Re: ClassCastException in Stateful Session-Bean
            Andreas Bachmann Newbie

            Hmm... interresting!

            public class Client {
             public static void main(String[] args) throws Exception {
            
             [...]
             Reference ref = (Reference) ctx.lookup("MyApplication/ShoppingCartBean/remote");
             System.out.println("ClassName: " + ref.getClassName());
             System.out.println("FactoryClassLocation: " + ref.getFactoryClassLocation());
             System.out.println("FactoryClassName: " + ref.getFactoryClassName());
            
             Enumeration<RefAddr> refaddrs = ref.getAll();
             RefAddr refAddr = null;
             while (refaddrs.hasMoreElements()) {
             refAddr = refaddrs.nextElement();
             System.out.println("Content: " + refAddr.getContent());
             System.out.println("ContentClassName: " + refAddr.getContent().getClass().getName());
             System.out.println("Type: " + refAddr.getType());
             }
             [...]
            
             }
            }
            


            Shell:

            ClassName: java.lang.Object
            FactoryClassLocation: null
            FactoryClassName: org.jboss.ejb3.JndiProxyFactory
            Content: MyApplication/ShoppingCartBean/remoteStatefulProxyFactory
            ContentClassName: java.lang.String
            Type: FACTORY
            


            • 4. Re: ClassCastException in Stateful Session-Bean
              Andreas Bachmann Newbie

              Ahh... ok! Remote Interface don't work, but Local Interface does!

              import org.jboss.tutorial.stateful.bean.ShoppingCart;
              
              import javax.servlet.http.HttpServlet;
              import javax.servlet.http.HttpServletRequest;
              import javax.servlet.http.HttpServletResponse;
              import javax.servlet.ServletException;
              import javax.naming.InitialContext;
              import javax.naming.NamingException;
              import java.io.IOException;
              import java.io.PrintWriter;
              import java.util.HashMap;
              
              public class TestServlet extends HttpServlet {
              
               protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
               process(request, response);
               }
              
               protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
               process(request, response);
               }
              
               private void process(HttpServletRequest request, HttpServletResponse response) throws IOException {
               PrintWriter out = response.getWriter();
              
               try {
               InitialContext ctx = new InitialContext();
               ShoppingCart shoppingCart = (ShoppingCart) ctx.lookup("MyApplication/ShoppingCartBean/local");
               out.println("Buying 1 memory stick");
               shoppingCart.buy("Memory stick", 1);
               out.println("Buying another memory stick");
               shoppingCart.buy("Memory stick", 1);
              
               out.println("Buying a laptop");
               shoppingCart.buy("Laptop", 1);
              
               out.println("Print cart:");
               HashMap<String, Integer> fullCart = shoppingCart.getCartContents();
               for (String product : fullCart.keySet()) {
               out.println(fullCart.get(product) + " " + product);
               }
              
               out.println("Checkout");
               shoppingCart.checkout();
              
               out.println("Should throw an object not found exception by invoking on cart after @Remove method");
               try {
               shoppingCart.getCartContents();
               }
               catch (Exception e) {
               out.println("Successfully caught no such object exception.");
               }
               } catch (NamingException e) {
               out.println("Exception: " + e.getMessage());
               }
               }
              }
              


              • 6. Re: ClassCastException in Stateful Session-Bean
                uday reddy Newbie

                hi,

                i faced the same problem like class cast exception earlier.

                This is simple classpath error.

                Make sure that your client program classpath contains the jbossall-client.jar .

                And also open the META-INF of the jbossall-client.jar file and place the jar files which are there in META-INF file in your classpath.

                Try this. i'm sure this will work.

                Class-Path: commons-logging.jar concurrent.jar ejb3-persistence.jar hi
                 bernate-annotations.jar jboss-aop-client.jar jboss-appclient.jar jbos
                 s-aspect-jdk50-client.jar jboss-client.jar jboss-common-core.jar jbos
                 s-deployers-client-spi.jar jboss-deployers-client.jar jboss-deployers
                 -core-spi.jar jboss-deployers-core.jar jboss-deployment.jar jboss-ejb
                 3-common-client.jar jboss-ejb3-core-client.jar jboss-ejb3-ext-api.jar
                 jboss-ejb3-proxy-client.jar jboss-ejb3-proxy-clustered-client.jar jb
                 oss-ejb3-security-client.jar jboss-ha-client.jar jboss-ha-legacy-clie
                 nt.jar jboss-iiop-client.jar jboss-integration.jar jboss-j2se.jar jbo
                 ss-javaee.jar jboss-jsr77-client.jar jboss-logging-jdk.jar jboss-logg
                 ing-log4j.jar jboss-logging-spi.jar jboss-main-client.jar jboss-mdr.j
                 ar jboss-messaging-client.jar jboss-remoting.jar jboss-security-spi.j
                 ar jboss-serialization.jar jboss-srp-client.jar jboss-system-client.j
                 ar jboss-system-jmx-client.jar jbosscx-client.jar jbosssx-as-client.j
                 ar jbosssx-client.jar jmx-client.jar jmx-invoker-adaptor-client.jar j
                 np-client.jar slf4j-api.jar slf4j-jboss-logging.jar xmlsec.jar

                when you open the META-INF file, the file contains the code as shown above. Place all the jar files in your classpath(client program classpath). You will get all these jar files in the JBOSS AS folder(lib folder).

                • 7. Re: ClassCastException in Stateful Session-Bean
                  uday reddy Newbie

                  hi,

                  i faced the same problem like class cast exception earlier.

                  This is simple classpath error.

                  Make sure that your client program classpath contains the jbossall-client.jar .

                  And also open the META-INF of the jbossall-client.jar file and place the jar files which are there in META-INF file in your classpath.

                  Try this. i'm sure this will work.

                  Class-Path: commons-logging.jar concurrent.jar ejb3-persistence.jar hi
                   bernate-annotations.jar jboss-aop-client.jar jboss-appclient.jar jbos
                   s-aspect-jdk50-client.jar jboss-client.jar jboss-common-core.jar jbos
                   s-deployers-client-spi.jar jboss-deployers-client.jar jboss-deployers
                   -core-spi.jar jboss-deployers-core.jar jboss-deployment.jar jboss-ejb
                   3-common-client.jar jboss-ejb3-core-client.jar jboss-ejb3-ext-api.jar
                   jboss-ejb3-proxy-client.jar jboss-ejb3-proxy-clustered-client.jar jb
                   oss-ejb3-security-client.jar jboss-ha-client.jar jboss-ha-legacy-clie
                   nt.jar jboss-iiop-client.jar jboss-integration.jar jboss-j2se.jar jbo
                   ss-javaee.jar jboss-jsr77-client.jar jboss-logging-jdk.jar jboss-logg
                   ing-log4j.jar jboss-logging-spi.jar jboss-main-client.jar jboss-mdr.j
                   ar jboss-messaging-client.jar jboss-remoting.jar jboss-security-spi.j
                   ar jboss-serialization.jar jboss-srp-client.jar jboss-system-client.j
                   ar jboss-system-jmx-client.jar jbosscx-client.jar jbosssx-as-client.j
                   ar jbosssx-client.jar jmx-client.jar jmx-invoker-adaptor-client.jar j
                   np-client.jar slf4j-api.jar slf4j-jboss-logging.jar xmlsec.jar

                  when you open the META-INF file, the file contains the code as shown above. Place all the jar files in your classpath(client program classpath). You will get all these jar files in the JBOSS AS folder(lib folder).