Rectangle 27 0

After trying Session.sendKeepAliveMsg() without success, I came to the following solution which seems to be rather stable:

I tried to test it by killing the open session on the server. All prior versions I tested this way showed the exact same behaviour, regardless wheater the problem poped up after waiting some days or killing the server process, so I thought this test - and its outcome for the above solution - to be meaningful. Unfortunately it isn't.

private Session getSession() throws Exception {
    try {
        ChannelExec testChannel = (ChannelExec) session.openChannel("exec");
        testChannel.setCommand("true");
        testChannel.connect();
        if(logger.isDebugEnabled()) {
            logger.debug("Session erfolgreich getestet, verwende sie erneut");
        }
        testChannel.exit();
    } catch (Throwable t) {
        logger.info("Session kaputt. Baue neue.");
        session = jsch.getSession(user, host, port);
        session.setConfig(config);
        session.connect();
    }
    return session;
}

This version runs several weeks in a productive environment. Once a day I have the info message logged.

The costs of opening a channel and performing some do-nothing-command are somewhat annoying, but I found no other way to be definitly sure about the state of the session.

nice solution, just one hint: I'd add testChannel.exit(); after successful check, otherwise you end up with one more sftp-server daemon processes on the sever side for each check

testChannel.exit()

java - jsch : how to keep the session alive and up - Stack Overflow

java session ssh jsch keep-alive
Rectangle 27 0

Take a look of Apache HttpClient and see its tutorial. HttpClient supports keep alive headers and other features that should enable you to programmatically make an HTTP call.

java - Programatically keep HTTP Session Alive without browser - Stack...

java http session java-ee
Rectangle 27 0

I had the same problem. (Using OpenPortal Portlet Container)

As I know there is two different sessions, 1- portlet session, 2- Container session.

I created a simple portlet and made request to the portlet with ajax periodically from container. However, session times out. My code fraction is here:

<%
    int per=15000; //ms
    String sessionExtenderPath = "http://portal.abc.com.tr:8080/SessionExtend/SessionExtend";
    //This is a portlet which includes servlet that returns a simple string.
%>

<script>
    periyod=100;

    function invokeRequest(){
     $.get("< %=sessionExtenderPath% >", function(data) {
      alert(1);
     });
    }
    window.setInterval("invokeRequest()",periyod);
</script>

I have solved my problem by requesting container page (wihout defining a channel for a portlet).

java - Portlets - Keeping session alive from server side - Stack Overf...

java spring session portlet
Rectangle 27 0

Basically, if(session != null){ means nothing here. You should read an attribute from the session and check it for null:

String username = (String)session.getAttribute("username");
   if(username!=null)
   {
      //user is logged in
   }
   else
   {
     //user is not logged in
   }

java - After invalidating session , it's still alive , why? - Stack Ov...

java jsp http session servlets
Rectangle 27 0

so if a stomp client is connected/subscribe to a server, doesnt, it signifies that the user is online.

but if thousand users are connected with server by web socket , then how can I distinguish ? which message is for whom users?

okay, so i didnt worked on web sockets, but i think http based cookie can be accessed in web sockets as well . right ? in that case just map that cookie to their respective session in server.

java - How to check other user Session alive in Spring Framework - Sta...

java spring java-ee jms activemq
Rectangle 27 0

If you are using spring security, you can have your own implementation of SecurityContextRepository (By default this is HttpSessionSecurityContextRepository) to persist the session content in database. In this way when user presents his cookie the security framework filter (SecurityContextPersistentFilter) will look at your database for the session. Hence, even when server is down the context is still available in database and is loaded during next user request after server restart.

If session persistence is not an option, you may have to deploy a cluster of servers with session replication strategy.

java - How to keep client session alive in spring MVC after server dis...

java spring spring-mvc session-cookies session-state
Rectangle 27 0

You can provide this entry in web.xml. This will keep the web session alive for 1 hr.

<session-config>
    <session-timeout>60</session-timeout>
</session-config>

I take it that this will keep the session alive for a hour since the last request?

Yes..This is the session idle timeout. The session will live for the time specified even if you keep the page idle.

java - Keep http session alive whilst use is on page - Stack Overflow

java jetty httpsession
Rectangle 27 0

I am not sure, how good it is to allow sessions running for a very long time e.g. hours in your case. Better would be to trigger a long running job with the user request to export something and give back (quickly) the id of the job to query again. When the user queries again, you either offer the download if the job is finished or give the status of the job.

  • Users closes the browser or something crashed on user side.
  • There are a big number of users blocking many sessions.

JAVA J2EE application keep the session alive for long request - Stack ...

java session java-ee
Rectangle 27 0

I have used a keep alive request solution for this in the past. Basically you set a JavaScript timeout for some long duration less than your session timeout, usually half to three-quarters of your session timeout. When the JavaScript timeout fires make an ajax request to a controller on the server that participates in the session. This will make the server think you are still active and should keep the server session alive by resetting it.

Also, you will need to reset the JavaScript timeout when your ajax response is received to start the timer again.

PS: You don't really need to do anything in the backend controller, you are just poking the session to keep it alive.

java - how to keep session alive - Stack Overflow

java javascript ajax
Rectangle 27 0

could it be what you are looking for?

here is the link to the blog-article:

java - How to keep client session alive in spring MVC after server dis...

java spring spring-mvc session-cookies session-state
Rectangle 27 0

The common way to do this is to associate a unique random key to your users, store it in a persistent cookie (use Cookie.setMaxAge() with the number of seconds you want this cookie to stay valid), and send this cookie to the user.

Look in the javadoc for HttpServletResponse.addCookie (to send a cookie to the user), and for HttpServletRequest.getCookies() (to get back the cookie from the client).

I know how to set and get cookies on client side. My question is that how to tell App Engine (on server) that we want it to retrieve a session with a specified ID (get this value from cookie), and not generating a new session every time we close browser and reopen it.

GAE stored the session state in the datastore, in the _ah_SESSION kind. AFAIK, they're not cleaned up automatically (yet), so you could cheat with the session mechanism by setting a very long expiration delay, and set the JSESSIONID cookie yourself, with a non-zero max age.

Great, you solved my problem! I set a cookie with a different name (sid, not JSESSIONID), that's why it doesn't work.

@Alan: then you can accept the answer, right?

Google App Engine Java - Keep Session alive after users close browser ...

java google-app-engine session remember-me
Rectangle 27 0

Since we can't mark a comment as an answer (and some people are likely to miss it entirely), I'll point out the specific solution per JB above. Get the session id using this.getThreadLocalRequest().getSession().getId(). Store the sid in a cookie as JSESSIONID with Cookies.setCookie(...)

This overwrites the cookie created by GAE, keeping the same session id, but applying your expiration time. Be careful how you use this, though, it's prone to attacks (look up session hijacking and XSS).

That didnt work for me when I used a servlet filter. When I inspected the response the JSESSIONID cookie hadnt been added yet, implying it is added afterwards? I created mine with maxage-1 but it came back to browser with two JSESSIONIDs in the header and just a session expiry

..implying GAE added the cookie after me, blatting the expiry

Google App Engine Java - Keep Session alive after users close browser ...

java google-app-engine session remember-me
Rectangle 27 0

An easy way if you have a small number of clients is to use request-reply over messaging in conjunction with topics. Have all user sessions listen to topic (pings or similar). When a client wants to see who is online, they send a message (the contents of the body don't matter) and set the JMSReplyTo and JMSCorrellationID headers to identify a temporary queue that they are listening to for replies. The listening parties will pick up this message and all send back a message containing their ids. That way you have a living cache on the sender as to who is currently "online", the cache should expire every couple of seconds.

java - How to check other user Session alive in Spring Framework - Sta...

java spring java-ee jms activemq
Rectangle 27 0

You can try by invoking the following method before invoking Session#connect().

java - jsch : how to keep the session alive and up - Stack Overflow

java session ssh jsch keep-alive
Rectangle 27 0

you can use -1 instead of 60 in web.xml as:

<session-config>
     <session-timeout>-1</session-timeout>
</session-config>

if you don't want to increase session time out then you can find work-around as :

run your task in a dedicated thread which touches (extends) the session while working or notifies the user when the thread has finished

This makes all sessions never expire, which is a very bad idea, especially if you actually store data in session.

@cjstehno : then in that case we can create one watcher task that will ping after 30 minutes to keep session alive.

As in my answer?

@cjstehno : yes that might be the one of the case.

java - how to keep session alive - Stack Overflow

java javascript ajax
Rectangle 27 0

First you should determine if you are actually getting the same session or not. There are 2 easy ways I can think of to do this.

1.) Look at the contents of the JSESSIONID cookie. Most browsers make this trivial. If the contents change, you have received a different session.

2.) You could try plugging in an HttpSessionListener to log when your sessions are being destroyed.

If you are getting a new session, you have to narrow it down to a configuration issue (Tomcat, web.xml, context snippet etc.) or an app issue. If it's a configuration issue, the problem should be repeatable on other pages than the ones you mention.

Also consider using getSession(false), which won't create a new session if one isn't already present. If you get null from this, it's another indicator that your sessions are timing out.

If you determine you have the same session, but for some odd reason attributes are disappearing, you can implement a HttpSessionAttributeListener and either log or breakpoint when items are removed from the session.

Thanks this helped me a lot. It seems that when I get to the resultpage and I open a new browser window and I leave my result page unfocused, after a minute or 2 the httpsession gets destroyed.

java - How to keep alive a session attribute - Stack Overflow

java web-applications session-variables tomcat
Rectangle 27 0

Do you require JAAS? You can implement a solution using spring-security, and a backing redis cache for the sessions ids. so e.g. when user is logged in on A, a session-id is put into the redis cache, when the user navigates to B through a link that passes a session id as a query parameter, the spring security layer (on B) will make sure that the session-id is alive and valid in the redis cache. Redis has built-in support for expiring data

Although our client finally chose to resolve this using Oracle Coherence, this was an interesting answer that can apply in many scenarios, and the link is quite useful. Thank you!

java - keeping alive multiple sessions in the same glassfish instance ...

java session web glassfish-3
Rectangle 27 0

I implemented it using a javascript timer that kept polling a portlet url. The portlet url was generated beforehadn so it was accessible in javascript.

java - Portlets - Keeping session alive from server side - Stack Overf...

java spring session portlet
Rectangle 27 0

<session-config>
         <session-timeout>-1</session-timeout>
 </session-config>

Simply paste this piece if code in your deployment descriptor (DD). If you want to keep your session alive for a particular duration of time replace -1 with any positive numeric value. Time specified here is in minutes.

2. If you want to change session timeout value for a particular session instance without affecting the timeout length of any other session in the application :

session.setMaxInactiveInterval(30*60);

Note :

1.In DD, the time specified is in minutes. 2.If you do it programatically, the time specified is in seconds.

Thanks.. I like this idea. But my intention here is to simulate browser behavior. So I would only keep the session alive if I keep getting request from the remote server. But I don't know what header to set in the http request from the remote server to keep the session alive

java - Programatically keep HTTP Session Alive without browser - Stack...

java http session java-ee
Rectangle 27 0

The purpose of remember me is altogether different. It's not to have session across server restarts, but for across restarts in client (browser). Even with remember-me you can not get it across server restarts, unless you use PersistentRememberMeService (to database)

java - How to keep client session alive in spring MVC after server dis...

java spring spring-mvc session-cookies session-state