[jboss-user] [EJB 3.0] - ClassCastException in Stateful Session-Bean

te-bachi do-not-reply at jboss.com
Tue Jan 16 10:55:18 EST 2007


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)
  | 

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4002326#4002326

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4002326



More information about the jboss-user mailing list