Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integration of tomcat and Quartz scheduler on startup

I am using tomcat version 6.0. My requirement is that when tomcat starts up, I would like to start a QuartzScheduler which will schedule some jobs at a regular interval. I am trying to figure out the best possible way to do it. Here are the options that I could think of -

  1. I can do this via a servlet with "load-on-startup" descriptor in web.xml file to start the scheduler and schedule the jobs inside the servlet.
  2. Can be done using a ContextListener (this sounds a better approach to me than 1). This might be a clean approach to start the scheduler inside the contextInitialized method and shutdown the scheduler inside contextDestroyed method.
  3. Using a MBean-descriptor. I develop a MBean which will get started when the server starts up with mbeans-descriptor.xml file.

To me, it looks like second approach is better. Third one might not be a good idea as it is clearly not a MBean to be monitored by jconsole or so. My purpose is to start the scheduler and stop it when tomcat stops. Is there any better and cleaner way to do this ?

like image 589
Shamik Avatar asked Jun 04 '10 15:06

Shamik


People also ask

Why we should not use Quartz Scheduler?

It has no built-in UI for configuration or monitoring, no useful and reasonably searchable logging or historical review, no support for multiple execution nodes, no administration interface, no alerts or notifications, not to mention buggy recovery mechanisms for failed jobs and missed jobs.

How do you trigger Quartz Scheduler?

All the Jobs registered in the Quartz Scheduler are uniquely identified by the JobKey which is composed of a name and group . You can fire the job which has a given JobKey immediately by calling triggerJob(JobKey jobKey) of your Scheduler instance. // Create a new Job JobKey jobKey = JobKey.

How do I start Quartz Scheduler in web application?

As a first step create a web application and keep all the Quartz Scheduler dependent jars in classpath. Put the quartz. properties and jobs XML file under classes folder. The configuration of the above job is defined in the job configuration XML file.

How does Quartz Scheduler work internally?

Quartz scheduler allows an enterprise to schedule a job at a specified date and time. It allows us to perform the operations to schedule or unschedule the jobs. It provides operations to start or stop or pause the scheduler. It also provides reminder services.


1 Answers

I would recommend the second approach as well, using a Servlet that exists only to start up some service, while a common usage, seems hacky to me.

It appears that quartz already provides a ServletContextListener for this exact purpose:

http://quartz-scheduler.org/documentation/quartz-2.x/cookbook/ServletInitScheduler

and

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

for details.

like image 68
Greg Case Avatar answered Sep 19 '22 21:09

Greg Case