5

I would like to delete some temporal files when user session finishes. The information associated with the files is stored in an object annotated with @SessionAttributes.

The only way I've found to deal with this is creating an HttpSessionListener.

Is there a higher level, simplified, Springy way to listen to the session end event where I could easily get my annotated object?

Eyal
  • 3,412
  • 1
  • 44
  • 60
Juan Calero
  • 4,124
  • 5
  • 31
  • 45
  • If the user logs out, you can act on that event. If the user just wanders off, `HttpSessionListener` is the only option. – DwB Mar 06 '12 at 20:43

3 Answers3

6

You most likely will need to create a HttpSessionListener.

Another stackoverflow answer:

Detect session timeout in Spring 3/Spring Security 2.0.5

Also and example on how to load spring beans into it:

http://www.mkyong.com/spring/spring-how-to-do-dependency-injection-in-your-session-listener/

Community
  • 1
  • 1
chrislovecnm
  • 2,549
  • 3
  • 20
  • 36
6

Two options to use HttpSessionListener with spring beans:

The first is to use WebApplicationContextUtils.getRequiredApplicationContext(servletContext) to obtain the servlet context. From there you have two sub-options:

  • use getBean(..)
  • If you want to use @Autowired / @Inject use getAutowireCapablyBeanFactory().autowireBean(this). You will have to do this only once (check if the fields are null), because the listener is singleton.

The second option is to use AspectJ and @Configurable on the listener.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
1

Not directly related, but might be an interesting project to look at.

https://github.com/shawnmclean/Idle.js

Session deletion typically happens on the server side, when the session expires (usually 30mn). The project above allows to detect user behaviors in the front end.

Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406
Olivier Refalo
  • 50,287
  • 22
  • 91
  • 122