Java Servlets: "best practice" for performing background tasks?

I am working on a web application that needs to clear expired cache items from time to time. I would like to do this using the Quartz framework and have a separate work to take part in this.

Now I have used Quartz in other projects and usually schedule all jobs from ServletContextListener using contextInitialised (ServletContextEvent sce) and contextDestroyed (ServletContextEvent sce). However, I did it this way because the textbook I once read did it that way.

I'm not sure if I can just schedule tasks from the init () and destroy () methods instead? I think it can be argued that now I am mixing background tasks and servlets and therefore responsibilities, but I would like to hear if there is a β€œbest practice” on this?

+5
source share
3 answers

First of all, a common disclaimer: you should not enter streams in the Servlet container. Thus, all methods are by definition incorrect (I suspect that the correct path will call curlfrom crontab).

ServletContextListener ; Servlet init() OK, ServletContextListener . : -, ( load-on-startup); , ( , , ).

, , , - , , , , . , , .

+3

webapp. .

-, , .

A ServletContextListener - , - .

+3

contextInitialised contextDestroyed ServletContextListener .

, Spring Framework Spring beans (org.springframework.web.context.ContextLoaderListener).

(, Spring Framework).

+1

All Articles