Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing session timeout during long processing time in JSF

I've been working on an JSF application. At one place, I've to call an action in managed bean. The action actually process hundreds of records and the session timesout before the processing is finished.

Though all the records are processed successfully, the session expires and the user is sent to login page.

I'd tried adding

session.setMaxInactiveInterval(0);

before the processing of the records with no effect.

How to prevent the session time out during such process.

like image 313
darkapple Avatar asked Dec 24 '10 08:12

darkapple


1 Answers

Introduce ajaxical polls to keep the session alive as long as enduser has the page open in webbrowser. Here's a kickoff example with a little help of jQuery.

<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
    $(document).ready(function() {
        setInterval(function() {
            $.get('poll');
        }, ${(pageContext.session.maxInactiveInterval - 10) * 1000});
    });
</script>

Here ${pageContext.session.maxInactiveInterval} returns the remnant of seconds the session has yet to live (and is been deducted with 10 seconds -just to be on time with poll- and converted to milliseconds so that it suits what setInterval() expects).

The $.get('poll') should call a servlet which is mapped on an url-pattern of /poll and contains basically the following line in the doGet() method.

request.getSession(); // Keep session alive.

That's it.

like image 81
BalusC Avatar answered Sep 23 '22 05:09

BalusC