i am trying to make a simple examples of @Stateful, @Stateless and @Singleton EJB beans to help me better understand the differences. The problem is that there is no difference at all when I annotate the bean with any of @Stateful, @Stateless or @Singleton annotations.
Here is the bean:
import javax.ejb.Singleton;
import javax.ejb.Stateful;
import javax.ejb.Stateless;
@Stateful
public class Bean {
private int counter = 0;
public int getCounter(){
return counter++;
}
}
And here is the Servlet Client:
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javaeetutorial.converter.ejb.Bean;
@WebServlet(urlPatterns="/")
public class Client extends HttpServlet{
@EJB
Bean bean;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html lang=\"en\">");
out.println("<head>");
out.println("<title>test</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet ConverterServlet at " +
request.getContextPath() + "</h1>");
try {
out.println("<form method=\"get\">");
out.println("<input type=\"submit\" value=\"Submit\">");
out.println("</form>");
out.println("<p>" + bean.getCounter() + "</p>");
out.println("<p>" + bean + "</p>");
} finally {
out.println("</body>");
out.println("</html>");
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
The expected results for me are:
@Statefull - when every different client press the button to see counting from 0
@Singleton - when any client press the button to see only one counting
@Stateless - i don't know what exactly to expect
The problem that you have is that the webservlet is instantiated once (or at least not once per request, that is what you would need to see clearly the difference), so it is the bean instance. Therefore, there is no difference which type of bean are you using (stateful, stateless, singleton), since the Bean instance is depending always on the same servlet instance.
If you would use a requestScoped resource, with the singleton annotation you would count how many times have been the counter requested (all requests share the same instance), with the stateful you would see how many times a client have requested the counter, and with stateless would depend. However, because the counter is a state, it make no sense to use a statless bean (look at the name). You can understand stateless as a pooled ejb, so each time that the bean is required the server will fetch an instance of the stateless pool. An advantage is the performance of the bean, since they are already created into the pool (check the stateless pool configuration) they are really fast to use.
You can found some more information here.