Scheduled task in a web application?

This question already has an answer here:

  • What is recommended way for spawning threads from a servlet in Tomcat [duplicate] 6 answers

  • Quartz is your best bet, and most highly configurable. It has CRON based interface or a more dynamic way to generate jobs that are relative from a specific event, if your use case calls for it Quartz can do it. It has the ability to persist jobs to the database so they can survive restarts.

    http://www.quartz-scheduler.org/

    Make configurations in web.xml like this to auto-start it:

      <servlet> 
        <servlet-name>QuartzInitializer</servlet-name>
        <display-name>Quartz Initializer Servlet</display-name>
        <servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    
        <init-param>
          <param-name>shutdown-on-unload</param-name>
          <param-value>true</param-value>
        </init-param>
    
        <init-param>
          <param-name>start-scheduler-on-load</param-name>
          <param-value>true</param-value>
        </init-param>
    
      </servlet> 
    

    You should consider:

  • Quartz
  • The "regular" TimerTask
  • If you're using spring on your webapp, there is a dedicated part for it
  • Don't bother reinventing the wheel, Quartz and other products already handle Threads/timeouts/concurrency issues for you!

    链接地址: http://www.djcxy.com/p/38916.html

    上一篇: Java适用于“Web 2.0”应用程序吗?

    下一篇: Web应用程序中的计划任务?