I am trying to create temporary files and folder which will delete after a specified time or when the session timeout. Is there there way to do this in servlet or spring ?
1 Answers
here is what I would do:
for creating the files :
- you can get the system's temp dir (e. g. /tmp) from the jvm using the java.io.tmpdir system property: String pathToTmpDir = System.getProperty("java.io.tmpdir");
of course you can always specify your own tmp directory, put that path in your application config and pull the path from there.
for cleaning up :
add a component which has a method annotated with @scheduled. you can pass a cron expression to that annotation to control when the method gets invoked. alternatively you can define a fixed interval for that, though using a cron expression gives you the greatest degree of control over the execution. for this to work you need a to annotate a config bean with @enableScheduling.
as for handling session timeouts you can implement the ApplicationListener interface to handle the SessionDestroyedEvent. Have a look at this SO post: Logout/Session timeout catching with spring security don't forget to enable the HttpSessionEventListener in your web.xml for that.
good luck =)

- 1
- 1

- 519
- 1
- 3
- 14