Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notify the user about the end of the session in JSF 2.3

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:

  1. How to handle the end of a session using websockets?
  2. In extreme cases, offer an alternative.
like image 709
Constantine Avatar asked Aug 07 '18 18:08

Constantine


1 Answers

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();
    }
}

See also:

  • Automatically perform action in client side when the session expires
  • Execute JavaScript before and after the f:ajax listener is invoked
like image 135
BalusC Avatar answered Nov 11 '22 20:11

BalusC