This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

TimerTask needing to write web accessible image files

Jan 3rd, 2009, 08:05 PM

I have TimerTasks specified in my spring servlet XML configuration using org.springframework.scheduling.timer.TimerFactoryB ean and org.springframework.scheduling.timer.ScheduledTime rTask beans. Everything works perfect. But now I have a TimerTask that needs to create images that will be accessed by that web application the TimerTask is in. How can I find out what directory to write the image files in from within a TimerTask?

Is this the best way to implement a task that repeats every 10 minutes? I can navigate my way to the webapps directory of tomcat, but I don't know which servlet context the app that called my TimerTask is in.

I dont think so you can know which servlet context it is getting executed automatically, since your time task does not have access to either request or servlet context directly, what you could maybe try is, send some meta information about the context that the time task can use to create the images on the disk under the appropriate webapp.

the other option is for you to persist the image into a database, and then write a simple controller that takes a image name as a parameter and serve the image to the browser, this option is better in my opinion since lot of security related issues are handled by the RDBMS itself.

Comment

Thank you Sami for your reply. It seems like the context should be able to be queried without a servletRequest or servletContext since the code is being executed in this environment, but I guess not . Yes I can pass in the context as a parameter to the TimerTask, I also have to count on tomcat having been executed from 1 particular directory. Not a pleasant workaround in my particular situation, but I appreciate the suggestion.

Saving the images in the database is not appealing to me, the images will never be altered or queried (aside from their meta-data which is stored in the database). So I want to rely on filesystems' and web servers' caching mechanisms to make this efficient.