Jsf-2 – @ predestroy is not called after the session expires
This is in was8 JSF 2 application running on 0 This is the code of a page's "support" bean
@Named("mySessionBean")
@SessionScoped
@Stateful
@LocalBean
@StatefulTimeout(unit = TimeUnit.MINUTES,value = 10)
public class MySessionBean implements Serializable {
@postconstruct
public void init()
{
System.out.println("start MySessionBean: " + this.hashCode());
}
@PreDestroy
public void cleanup()
{
System.out.println("destroy MySessionBean: " + this.hashCode());
}
....
}
web. The session timeout value set in XML is less than the bean timeout value When I run the application, I see printouts from @ postconstruct, but never from @ predestroy I tried the following two schemes: 1 logout – invalidateSession; 2. Just wait until the session expires
I'm not an application designer The designer insists on treating all supported beans as stateful session beans I think the more mainstream approach is to make them CDI beans But anyway, when I only changed the comment to CDI, I also started getting printouts from @ predestroy
@Named("mySessionBean")
@SessionScoped
public class MySessionBean implements Serializable {
.....
My question is, what is the reason why I didn't get the @ predestroy method call in the first case? If I can't see @ predestroy being called, is there another way to track the life cycle of the "supported" bean (in this case, a stateful session bean) thank you!
Solution
Look at this part of Java EE 6 tutorial, which shows the life cycle of stateful session beans The predestroy method is called only when the bean is explicitly removed from the client code and the method using the @ remove annotation is called
