How to keep JSF Conversations from expiring

I have a requirement to prevent the end of the JSF 2.2 CDI session. I tried to implement the heartbeat mechanism when I click the "hidden" button using Ajax, which in turn calls the servlet. But the conversation is still running out. I set the timeout to 10 seconds for testing, and my code is shown below.

// The begin conversation method in my managed bean
public void beginConversation() {
      if (conversation.isTransient())
      {
          conversation.setTimeout(10000);
          conversation.begin();
      }
}

// JQuery document ready function
$(document).ready(function() {
setInterval(function(){$.get("/HeartbeatServlet");}, 5000);

});

// Heartbeat servlet
@WebServlet("/HeartbeatServlet")
public class HeartbeatServlet extends HttpServlet{

private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
{
    System.out.println("Heartbeat Received");
    request.getSession();
}
}

He prints the text "Heartbeat Recieved" every 5 seconds. But the conversation is still running out.

+4
source share
4 answers

Talk timeout - how long the conversation will survive. This is not a permanent tracker in conversation. He should not remain alive upon request.

, begin (id) . bean .

+1

. "cid" . , Heartbeat Servlet . , .

, , . PrimeFaces p: poll managebean.

.

@ManagedBean
@ViewScoped
public class ConversationListener implements Serializable {

...

/**
 * Inject Conversation instance if it is available.
 */
@Inject
private Conversation conversation;
@Inject
FacesBroker facesBroker;

...

/**
 * Method to refresh current conversation
 */
public void renewConversation() {

    if (conversation == null) {
        System.out.println("no conversation");
    } else {
        HttpServletRequest request = (HttpServletRequest) facesBroker.getContext().getExternalContext().getRequest();
        log.info("***     ConversationListener called for conversation ID: {}", new Object[] { conversation.getId() });
        log.info("***     ConversationListener called for session ID: {}", new Object[] { request.getSession().getId() });
    }
}
}
+1

specificifc, begin(), , requestparameter. , .

​​ cdi "cid". , .

try adding a parameter to your query and it should display a dialog and possibly update the timeout.

hope that helps

0
source

I found that maintaining the duration of the conversation can be achieved by calling the method in a bean session at the same time, configured to use a reasonable interval (the 5 minute interval worked for me), no magic is required.

0
source

All Articles