{"id":605,"date":"2004-06-15T11:37:00","date_gmt":"2004-06-15T15:37:00","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=605"},"modified":"2004-06-15T11:37:00","modified_gmt":"2004-06-15T15:37:00","slug":"scheduled-tasks-using-quartz","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2004\/06\/15\/scheduled-tasks-using-quartz\/","title":{"rendered":"Scheduled Tasks using Quartz"},"content":{"rendered":"<p>Where I work we run about 30-40 public web sites deployed across a variety of servers in development, staging and live environments.  One of my personal goals is to automate the deployment process, making it as close to foolproof as possible (no touching!) to deploy an application to any (or all) of the environments.  To that end, I&#8217;d really like to get rid of are the scheduled tasks we have implemented using <a href=\"http:\/\/www.microsoft.com\/windows2000\/techinfo\/howitworks\/management\/task_scheduler.asp\">Microsoft Windows Task Scheduler<\/a> (which requires touching).  Almost every application we write has at least one process that needs to be fired every couple minutes or every night at 5:30am.  With ASP, this means that we have to write a script in VBScript that is called by WGET which is called by WSH which is then called by a Windows Scheduled Task.  Lucky for me, I&#8217;m working with Java on my current project.  <\/p>\n<p>I found <a href=\"http:\/\/www.opensymphony.com\/quartz\/\">Quartz<\/a> when I was exploring the Scheduled Details option under &#8216;Administration &#8211;&gt; System&#8217; in our <a href=\"http:\/\/www.atlassian.com\/software\/jira\/\">Jira<\/a> installation.  Quartz is described as an: &#8220;<i> open source job scheduling system that can be integrated with, or used along side virtually any J2EE or J2SE application<\/i>&#8220;; in short it provides close to the same functionality as Windows Task Scheduler or <a href=\"http:\/\/www.uwsg.iu.edu\/usail\/automation\/cron.html\">cron<\/a> but it can be embedded within your application, which means a zero touch deployment.  An added benefit is that the scheduled tasks are now running in the same JVM as the application to which it was deployed, whereas a job scheduled and executed using a Windows .bat file and the Windows Task Scheduler runs in a separate JVM. Additionally, it can be configured using a text file rather than a GUI and the jobs and triggers can optionally be persisted to a database.<\/p>\n<p>Using Quartz is a piece of cake.  After adding the quartz.jar (<a href=\"http:\/\/www.opensymphony.com\/quartz\/download.html\">download<\/a>) to my classpath, the jobs that I had written previously to be run from the command line were modified to implement the <code>Job<\/code> interface, which requires only that the class have an <code>execute(JobExecutionContext context)<\/code> method that returns void.  To schedule and run the jobs is a bit more complicated.  Because I didn&#8217;t want to persist the jobs or job schedules to a database and I wanted the jobs to run in the context of a web application, I needed a way to schedule the jobs when the application was started. In a web application you can do this by creating a listener (technically a <code>ServletContextListener<\/code>) that takes action when the <code>contextInitialized(ServletContextEvent context)<\/code> event is fired.  Inside that method, you&#8217;d then start the scheduler, create your jobs and triggers and then schedule the jobs:<br \/>\n<code><br \/>\n\/\/ create &amp; start the scheduler<br \/>\nSchedulerFactory factory = new StdSchedulerFactory();<br \/>\nScheduler scheduler = factory.getScheduler();<br \/>\nscheduler.start();<br \/>\n\/\/ create the job &amp; trigger<br \/>\nJobDetail job = new JobDetail(\"myjob\",<br \/>\n&nbsp;&nbsp;Scheduler.DEFAULT_GROUP, JobClass.class);<br \/>\nCronTrigger trigger = new CronTrigger(\"inventory\",<br \/>\n&nbsp;&nbsp;Scheduler.DEFAULT_GROUP, \"0 15 4 * * ?\");<br \/>\n\/\/ schedule the job<br \/>\nscheduler.scheduleJob(job, trigger);<br \/>\n<\/code><br \/>\nMake sure that the listener is added to your web.xml deployment descriptor:<br \/>\n<code><br \/>\n&lt;listener&gt;<br \/>\n&nbsp;&nbsp;&lt;listener-class&gt;com.mycompany.tasks.SchedulerLauncher&lt;\/listener-class&gt;<br \/>\n&lt;\/listener&gt;<br \/>\n<\/code><br \/>\nand you&#8217;re all set.<\/p>\n<p>If it sounds interesting, definitely check out the <a href=\"http:\/\/www.opensymphony.com\/quartz\/docs.html\">documentation<\/a> and the <a href=\"http:\/\/www.opensymphony.com\/quartz\/tutorial.html\">tutorial<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Where I work we run about 30-40 public web sites deployed across a variety of servers in development, staging and live environments. One of my personal goals is to automate the deployment process, making it as close to foolproof as possible (no touching!) to deploy an application to any (or all) of the environments. To &hellip; <a href=\"https:\/\/cephas.net\/blog\/2004\/06\/15\/scheduled-tasks-using-quartz\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Scheduled Tasks using Quartz<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/605"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=605"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/605\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=605"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=605"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=605"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}