problem with a stateless session bean
flamant Oct 9, 2014 10:44 AMHello,
I don't know if it is the right forum for this discussion. I didn't find the answer elsewhere. Anyway, here is my problem
I have a java client (Main program) that call a remote SLSB by a lookup
[CODE]
int choice = 1;
String viewClassName = LibrarySessionBeanRemote.class.getName();
LibrarySessionBeanRemote libraryBean = (LibrarySessionBeanRemote) ctx.lookup("EJBTutorial/EJBTutorialEJB//LibrarySessionBean!"+ viewClassName);
while (choice != 2) {
String bookName;
showGUI();
String strChoice = brConsoleReader.readLine();
choice = Integer.parseInt(strChoice);
if (choice == 1) {
System.out.print("Enter book name: ");
bookName = brConsoleReader.readLine();
libraryBean.addBook(bookName);
} else if (choice == 2) {
break;
}
}
List<String> booksList = libraryBean.getBooks();
System.out.println("Book(s) entered so far: " + booksList.size());
for (int i = 0; i < booksList.size(); ++i) {
System.out.println((i + 1) + ". " + booksList.get(i));
}
[/CODE]
Before doing my lookup, I run a dozen threads that call a servlet. Each servlet contains an instance of my SLSB
[CODE]
@WebServlet(name="ServletCallSessionBean", urlPatterns={"/test"})
public class ServletCallSessionBean extends HttpServlet {
@EJB(beanName = "LibrarySessionBean")
private LibrarySessionBeanLocal monBean;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
//PrintWriter out = response.getWriter();
monBean.getBooks();
System.out.println("DEBUT ATTENTE");
try {
Thread.sleep(300000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("FIN ATTENTE");
PrintWriter out = new PrintWriter (response.getOutputStream());
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet MaServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("</body>");
out.println("</html>");
} finally {
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);
}
@Override
public String getServletInfo() {
return "Ma servlet de test";
}
}
[/CODE]
This SLSB has an attribut List<String> bookShelf.
[CODE]
@Stateless(name = "LibrarySessionBean")
@Local(LibrarySessionBeanLocal.class)
@Remote(LibrarySessionBeanRemote.class)
@LocalBean
public class LibrarySessionBean {
private List<String> bookShelf;
/**
* Default constructor.
*/
public LibrarySessionBean() {
// TODO Auto-generated constructor stub
System.out.println("object this.toString=" + this.toString());
System.out.println("constructeur LibrarySessionBean appele");
bookShelf = new ArrayList<String>();
}
public void addBook(String bookname) {
System.out.println("object this.toString=" + this.toString());
System.out.println("addbook appele");
bookShelf.add(bookname);
}
public List<String> getBooks() {
System.out.println("object this.toString=" + this.toString());
System.out.println("getBooks appele");
return bookShelf;
}
}
[/CODE]
When I add a book from the client side in the Main program(see the red part of the code) and when I inspect (debugging) my List the book isn't in it
I know that it is a SLSB (without state), but immediately after adding a book, the book should be in the List. When I don't run the dozen threads before, the book is in the List
Do you have an idea ? Thanks in advance for your answers.