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:
Your technical problem is that you didn't specify a function reference in onevent
or onclose
attribute. It goes like:
onevent="function() { PF('dlg1').show() }"
onclose="function() { PF('dlg1').show() }"
or
onevent="functionName"
onclose="functionName"
where functionName
is defined as a real function:
function functionName() {
PF('dlg1').show();
}
The correct approach is explained in Events section of javax.faces.Push
javadoc:
<f:websocket channel="exit" scope="session"
onclose="function(code) { if (code == 1000) { PF('dlg1').show() }}" />
or
<f:websocket channel="exit" scope="session" onclose="exitListener" />
function exitListener(code) {
if (code == 1000) {
PF('dlg1').show();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With