tomcatstartupjobsschedule

Quartz job at startup - Context Listener Addition


My goal is to get a Quartz job scheduled at the time the Tomcat server starts up. I created a test class (in Java) that sets up the scheduling the way that I need, as well as a class that it would run when it triggers (again, Java).

I'm following the "option 2" of this thread, as well as its answer from the Quartz site:

Integration of tomcat and Quartz scheduler on startup

It says I need to: "....start the scheduler inside the contextInitialized method and shutdown the scheduler inside contextDestroyed method"

Where are those two methods? I'm assuming I would put some kind of reference to the "scheduling class" I made above, which connects to the "job class".

Or am I off base, and should be doing this through some other means? I found this link, but I think I would run into the sample problem.

Quartz run job on startup

Tomcat 7 Quartz 2.2.1


Solution

  • Yes....I should have done this in XML. Putting this in web.xml will start Quartz when the server starts. This works on Quartz 2.5.0 with Tomcat 10.1.

    http://www.mkyong.com/java/example-to-run-multiple-jobs-in-quartz/

     <!-- Start up Quartz -->
     <context-param>
         <param-name>quartz:shutdown-on-unload</param-name>
         <param-value>true</param-value>
     </context-param>
     <context-param>
         <param-name>quartz:wait-on-shutdown</param-name>
         <param-value>false</param-value>
     </context-param>
     <context-param>
         <param-name>quartz:start-scheduler-on-load</param-name>
         <param-value>true</param-value>
     </context-param>