public class SessionCleanup extends StandardServlet {

        private static final long serialVersionUID =
7896915936685966869L;
        private static final Logger logger =
Logger.getLogger(SessionCleanup.class.getName());
        private Cache cache = null;
        private static final String EXPIRES = "_expires";

        @Override
        public void init() throws ServletException {
                try {
                        final CacheFactory cacheFactory =
CacheManager.getInstance().getCacheFactory();
                        cache =
cacheFactory.createCache(Collections.emptyMap());
                } catch (final CacheException e) {
                        logger.log(Level.SEVERE, "failed to configure
cache", e);
                }
        }

        @Override
        protected void processRequest(final HttpServletRequest
request, final HttpServletResponse response) throws ServletException,
                        IOException {

                final long now = new Date().getTime();
                final DatastoreService datastore =
DatastoreServiceFactory.getDatastoreService();
                final Query query = new Query("_ah_SESSION");

                // TODO make this a task
                for (final Entity session :
datastore.prepare(query).asIterable(FetchOptions.Builder.withLimit(100)))
{
                        Long expires = (Long)
session.getProperty(EXPIRES);
                        if (expires < now) {
                                final Key key = session.getKey();
                                datastore.delete(key);
                                final StringBuilder sb = new
StringBuilder("Removed session with expiration ").append((new
Date(expires)).toString());
                                if (cache != null)
                                        if
(cache.containsKey(key.getName())) {
 
cache.remove(key.getName());
                                                sb.append(", also from
memcache");
                                        }
                                logger.log(Level.INFO, sb.toString());
                        }
                }

        }

}


On May 8, 8:20 pm, Joe Fawzy <[email protected]> wrote:
> Hi Peter
> thanks for your reply and for the link to the other thread
>
> it will be great if u posted the code
>
> thanks a lot
>
> Joe
>
> On May 8, 7:50 pm, Peter Ondruska <[email protected]> wrote:
>
>
>
>
>
> > expires field is long and can be used as Date.getTime(). 
> > Seehttp://groups.google.com/group/google-appengine-java/browse_thread/th...
> > and if you need hint on code for cleaning up expired sessions let me
> > know. I can post the source.
>
> > On May 8, 3:54 pm, Joe Fawzy <[email protected]> wrote:
>
> > > Hi dear
>
> > > actually the session data cannot be analyzed that easy as it is just a
> > > blob of the session with all the web framework things
> > > and the expires field is an int so i do not know how this translate to
> > > date to delete non expired ones
>
> > > any help
> > > thanks
> > > Joe
>
> > > On May 8, 3:30 pm, Piotr Tarsa <[email protected]> wrote:
>
> > > > I think you should do some cron job.
>
> > > > I think keeping expired sessions can be an useful feature. You can dump
> > > > session to your local disk and analyze them. For example you can log 
> > > > some
> > > > user actions to sessions and then analyze them offline.
>
> > > > 2010/5/8 Joe Fawzy <[email protected]>
>
> > > > > Hi
> > > > > i have thausands of entries in my ah_SESSION data store table, it
> > > > > dates several days back
> > > > > it does not expire or delete, i think it is non sense to keep all the
> > > > > sessions even that expired in the web container in the data store....
> > > > > if it expires , when it expires? and how to trigger that
> > > > > if it does not expires or auto delete? is it a feature?
> > > > > How can i delete them? do i have to do it myself? or appengine have a
> > > > > setting for it?
> > > > > i know i can do it in code .. but....
> > > > > What is your experience with it?
>
> > > > > Joe
>
> > > > > --
> > > > > You received this message because you are subscribed to the Google 
> > > > > Groups
> > > > > "Google App Engine for Java" group.
> > > > > To post to this group, send email to
> > > > > [email protected].
> > > > > To unsubscribe from this group, send email to
> > > > > [email protected]<google-appengine-java%2B
> > > > >  [email protected]>
> > > > > .
> > > > > For more options, visit this group at
> > > > >http://groups.google.com/group/google-appengine-java?hl=en.
>
> > > > --
> > > > You received this message because you are subscribed to the Google 
> > > > Groups "Google App Engine for Java" group.
> > > > To post to this group, send email to 
> > > > [email protected].
> > > > To unsubscribe from this group, send email to 
> > > > [email protected].
> > > > For more options, visit this group 
> > > > athttp://groups.google.com/group/google-appengine-java?hl=en.
>
> > > --
> > > You received this message because you are subscribed to the Google Groups 
> > > "Google App Engine for Java" group.
> > > To post to this group, send email to 
> > > [email protected].
> > > To unsubscribe from this group, send email to 
> > > [email protected].
> > > For more options, visit this group 
> > > athttp://groups.google.com/group/google-appengine-java?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups 
> > "Google App Engine for Java" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to 
> > [email protected].
> > For more options, visit this group 
> > athttp://groups.google.com/group/google-appengine-java?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Google App Engine for Java" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group 
> athttp://groups.google.com/group/google-appengine-java?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en.

Reply via email to