Problem appreciating the stateless bean behavior
oceanvijai Oct 16, 2013 2:51 AMHi,
I am a newbie to EJB and started off with EJB 3.0. It annotations are cool but when I was trying to study EJB stateless session bean behavior by trying some samples, I got stuck up. The details are as follows,
I created a stateless session bean as follows,
stateless remote interface.
package sample.testing.EJBs.sessionBean.stateless; import javax.ejb.Remote; @Remote public interface FirstStatelessSessionBeanRemote { public String getMyName(); public void appendName( String nameToAppend); }
stateless bean impl
package sample.testing.EJBs.sessionBean.stateless; import javax.ejb.Remote; import javax.ejb.Stateless; /** * Session Bean implementation class FirstStatelessSessionBean */ @Stateless (name="vijai'sFirstStatelessSessionBean") public class FirstStatelessSessionBean implements FirstStatelessSessionBeanRemote { /** * Default constructor. * */ public FirstStatelessSessionBean() { name = "vijai"; } private String name=""; @Override public String getMyName() { // TODO Auto-generated method stub return getName(); } @Override public void appendName(String nameToAppend) { name += nameToAppend; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
So I deployed this as a jar is the same Jboss 7.1 server. For testing I created a simple servlet application and tested as follows,
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { final Hashtable jndiProperties = new Hashtable(); jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming"); final Context context = new InitialContext(jndiProperties); response.getWriter().println(context.getEnvironment()); String app_name=""; String module_name="/sampleEJBProject"; String distinct_name="/"; String bean_name="/vijai'sFirstStatelessSessionBean"; String remoteInterface_name="sample.testing.EJBs.sessionBean.stateless.FirstStatelessSessionBeanRemote"; String ejbBeanJNDIName = "ejb:"+app_name+module_name+distinct_name+bean_name+"!"+remoteInterface_name; response.getWriter().println(ejbBeanJNDIName); Object obj = context.lookup(ejbBeanJNDIName); response.getWriter().println(obj); FirstStatelessSessionBeanRemote bean = (FirstStatelessSessionBeanRemote)obj; bean.appendName("Vijai"); response.getWriter().println(bean.getMyName()); bean.appendName("anand"); response.getWriter().println(bean.getMyName()); } catch (Exception e) { e.printStackTrace(); response.getWriter().println(e.getMessage()); }
The result was something like this,
1st refresh: vijaVijaianand
2nd refresh: vijaVijaianandVijaianand
3rd refresh: vijaVijaianandVijaianandVijaianand
and so.... but as per the definition of a stateless bean, the scope is expected to be destroyed after the invocation method is complete right ?
so I should be getting something like
1st refresh: vijaVijaianand
2nd refresh: vijaVijaianand
3rd refresh: vijaVijaianand
But I dont, can anyone advice me when am I doing wrong here please....