My question is related to this one (and probably others):
@PreDestroy never called on @ViewScoped
As stated there, there's no trivial solution to either have view-scoped beans destroyed upon navigation and the same seems to hold true for when the session expires.
What would a non-trivial approach to release (calling the @PreDestroy
method) JSF view-scoped beans look like, or more specifically as soon as the session expires?
I'm using Java EE 6 and Mojarra 2.1.x on GlassFish 3.1.2.
Create a @SessionScoped
bean to hold the resources (in some collection/array?) and inject it in the @ViewScoped
bean and then rely on the @PreDestroy
of the session scoped bean.
True, this way the resources live a little longer than you want, but that's the most easy and reliable solution you can get. If you want to keep the @PreDestroy
in the view scoped bean, then you'd need to somehow make sure that the enduser always performs navigation by a HTTP POST request on exactly this view scoped bean. You can't reliably guarantee that (the enduser's PC might crash and so on).
来源:https://stackoverflow.com/questions/13082379/destroying-view-scoped-beans-when-session-ends