I have a requirement to keep the JSF 2.2 CDI conversation from expiring. I tried implementing a heartbeat mechanism where I click a 'hidden' button using Ajax which in turn calls a servlet. But the Conversation still expires. I set the timeout to 10s for testing purposes and my code is as 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();
}
}
It prints the "Heartbeat Recieved" text every 5 seconds. But the conversation still expires.
The conversation timeout is how long the conversation will stay alive. It is not a permanent tracker of a conversation. It is not meant to be a keep alive on requests.
There is no extend conversation concept, however you can use begin(id) to recreate a conversation. Any bean state in that conversation will be lost.