, ,

As with stateless Session Bean there was no big deal in deploying the stateful Session Bean on JBoss AS 5.0.0.CR1, also.

2) Stateful Session Bean


package com.example.bean;

import java.util.HashMap;
import javax.ejb.Remove;

public interface ShoppingCart {
     void buy(String product, int quantity);

     HashMap getCartContents();

     @Remove void checkout();


package com.example.bean;

import java.io.Serializable;
import java.util.HashMap;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.ejb.Remote;

public class ShoppingCartBean
     implements ShoppingCart, Serializable {
     private HashMap cart = new HashMap();

     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 getCartContents() {
          return cart;

     public void checkout() {
          System.out.println(“To be implemented”);


package com.example.client;

import java.util.HashMap;
import javax.naming.InitialContext;
import com.example.bean.ShoppingCart;

public class Client {
     public static void main(String[] args) throws Exception {
          InitialContext ctx = new InitialContext();
          System.out.println(“got initial context : “ + ctx );

          ShoppingCart cart = (ShoppingCart) ctx.lookup(“ShoppingCartBean/remote”);

          System.out.println(“Buying 1 memory stick”);
          cart.buy(“Memory stick”, 1);
          System.out.println(“Buying another memory stick”);
          cart.buy(“Memory stick”, 1);

          System.out.println(“Buying a laptop”);
          cart.buy(“Laptop”, 1);

          System.out.println(“Print cart:”);
          HashMap fullCart = cart.getCartContents();
          for (String product : fullCart.keySet()) {
               System.out.println(fullCart.get(product) + ”     “ + product);


          System.out.println(“Should throw an object not
               found exception by invoking on cart after @Remove method”);
          } catch(Exception e){
               System.out.println(“Successfully caught no such object exception.”);

Directory Structure of the ShoppingCart.jar
com/example/bean → ShoppingCart.java, ShoppingCartBean.java

Directory Structure of Client
com/example/client → Client.java

Now deploy it in the JBoss
After successful deployment the console of JBoss will give some output like this:

17:52:13,145 INFO  [STDOUT] ======>
 Creating interceptor metadata bridge
17:52:13,934 INFO  [JBossASKernel] Created KernelDeployment for: stateful.jar
17:52:13,954 INFO  [JBossASKernel] installing bean:
17:52:13,956 INFO  [JBossASKernel]   with dependencies:
17:52:13,956 INFO  [JBossASKernel]   and demands:
17:52:13,957 INFO  [JBossASKernel]      jboss.ejb:service=EJBTimerService
17:52:13,957 INFO  [JBossASKernel]   and supplies:
17:52:13,957 INFO  [JBossASKernel]      jndi:ShoppingCartBean/
17:52:13,957 INFO  [JBossASKernel]      jndi:ShoppingCartBean/
17:52:13,958 INFO  [JBossASKernel]      jndi:ShoppingCartBean
17:52:13,958 INFO  [JBossASKernel]      Class:com.example.bean.ShoppingCart
17:52:13,958 INFO  [JBossASKernel] Added bean(jboss.j2ee:ear=stateful.jar,
service=EJB3) to KernelDeployment of: stateful.jar
17:52:14,830 INFO  [EJBContainer] STARTED EJB:
com.example.bean.ShoppingCartBean ejbName: ShoppingCartBean

After running the client, the output on the console is:

got initial context : javax.naming.InitialContext@a01335
Buying 1 memory stick
Buying another memory stick
Buying a laptop
Print cart:
2     Memory stick
1     Laptop
Should throw an object not found exception by
invoking on cart after @Remove method
Successfully caught no such object exception.
About these ads