Friday 14 October 2011

Dependency Injection or Context Lookup a stateful session bean

First create a stateful session bean

@Stateful(name="stateful")
@Local(Session.class)
public class StatefulSessionBean 
    implements Session {

    private int result;
    
    @Override
    public void add() {
        result++;
    }

    @Override
    public int get() {
        return result;
    }
}

Then create a servlet to Dependency Inject the stateful session bean

public class StatefulDependencyInjectionServlet 
    extends HttpServlet {
   
    @EJB(beanName="stateful")
    private Session stateful;
    
    @Override
    protected void doGet(HttpServletRequest req, 
        HttpServletResponse resp)
            throws ServletException, IOException {
        HttpSession session = req.getSession(true);
        Session bean = (Session)session.getAttribute("stateful");
        if (bean == null){
            bean = this.stateful;
        }
        bean.add();
        System.out.println("Stateful: " + bean.get());
        session.setAttribute("stateful", bean);
        
    }
} 

Test the servlet several times and check the result

[24/08/11 14:30:00:234 EST] 00000016 SystemOut O Stateful: 1
[24/08/11 14:30:03:234 EST] 00000016 SystemOut O Stateful: 2
[24/08/11 14:30:06:203 EST] 00000016 SystemOut O Stateful: 3

It seems working fine. But if we close the browser, open it again and test the servlet several times, we will see:

[24/08/11 14:33:15:812 EST] 00000016 SystemOut O Stateful: 4
[24/08/11 14:33:16:718 EST] 00000016 SystemOut O Stateful: 5
[24/08/11 14:33:17:328 EST] 00000016 SystemOut O Stateful: 6

This behaviour is incorrect as when the browser is closed and reopened, it represents a new client, who should get a new instance of stateful session bean.

Now create another servlet to access the stateful session bean through Context Lookup.

@EJB(name="ejb/stateful", 
     beanInterface=Session.class, 
     beanName="stateful")
public class StatefulContextLookupServlet 
extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, 
        HttpServletResponse resp)
            throws ServletException, IOException {
        HttpSession session = req.getSession(true);
        Session stateful = (Session)session.getAttribute("stateful");
        if (stateful == null){
            try{
                Context ctx = new InitialContext();
                stateful = (Session)ctx.lookup
                    ("java:comp/env/ejb/stateful");
            }catch (Exception e) {
                throw new EJBException(e);
            }    
        }
        stateful.add();
        System.out.println("Stateful: "+stateful.get());
        session.setAttribute("stateful", stateful);
        
    }
}

Test the servlet several times and check the result

[24/08/11 14:38:42:250 EST] 00000016 SystemOut O Stateful: 1
[24/08/11 14:38:45:453 EST] 00000016 SystemOut O Stateful: 2
[24/08/11 14:38:46:000 EST] 00000016 SystemOut O Stateful: 3

Close the browser, open it again and test the servlet several times

[24/08/11 14:39:38:734 EST] 00000016 SystemOut O Stateful: 1
[24/08/11 14:39:39:796 EST] 00000016 SystemOut O Stateful: 2
[24/08/11 14:39:40:546 EST] 00000016 SystemOut O Stateful: 3

Now the behaviour is correct.

No comments:

Post a Comment