JavaEE, JSF-2.3, Websocket, WebApplication, WildFly.
For each user, a session is created, within which it operates, authorization, authentication, and so on. After 15 minutes of inactivity, the session is automatically destroyed, thanks to the settings of web.xml -
<session-config>
<session-timeout>15</session-timeout>
</session-config>
In JSF-2.3 available WebSocket, so I decided to do so ExitBean.java -
@Inject
@Push(channel = "exit")
PushContext push;
@PreDestroy
public void sessionTimeOut() {
push.send("exitEvent");
}
On the page, respectively, exit.xhtml -
<h:form >
<f:websocket channel="exit" scope="session">
<f:ajax event="exitEvent" onevent="PF('dlg1').show()"/>
</f:websocket>
</h:form>
At the end of the session, judging by the logs, the sessionTimeOut()
method works, it's still @PreDestroy
, but there is no response on the page.
For the test, I placed a button on the exit.xhtml page, by clicking on which the sessionTimeOut()
method is called. When this button is clicked, event - "exitEvent", executes as expected, invoking the PrimeFaces script PF('dlg1').show()
, which displays a dialog box.
I suspect that websockets are killed even earlier than the @Predestroy
method is called.
There is another option with the websocket, it looks like this:
<h:form >
<f:websocket channel="exit" scope="session" onclose="PF('dlg1').show()"/>
</h:form>
But it works only when the page loads and again no reaction to the end of the session.
Two questions:
- How to handle the end of a session using websockets?
- In extreme cases, offer an alternative.