Rather than invent your own scheduler or use CRON jobs to manage regular processing, use a mature open source scheduler such as Quartz to execute jobs.  Application servers such as Tomcat detect Quartz configuration files in the WAR and automatically start jobs in a controlled environment.  

In the following example, a simple job is scheduled to trigger every 30 minutes:

<?xml version="1.0" encoding="UTF-8"?>
<quartz xmlns="http://www.opensymphony.com/quartz/JobSchedulingData"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.opensymphony.com/quartz/JobSchedulingData
    http://www.opensymphony.com/quartz/xml/job_scheduling_data_1_5.xsd"
  overwrite-existing-jobs="true" version="1.5">
  <job>
    <job-detail>
      <name>Radio Guide</name>
      <group>DEFAULT</group>
      <description>Radio Guide RSS feed</description>
      <job-class>com.playpen.feedprocessor.RssFeedReader</job-class>
      <job-data-map>
        <entry>
           <key>rss.url.input</key>
           <value>http://www.subscription.net/radioguide.xml</value>
        </entry>
        <entry>
           <key>rss.dir.output</key>
           <value>/feeds/radioguide/</value>
        </entry>
        <entry>
           <key>rss.contentType</key>
           <value>radioguide</value>
        </entry>
        <entry>
           <key>rss.maxAge</key>
   <value>30</value>
        </entry>
        <entry>
           <key>rss.mapDateFields</key>
           <value>showdate|dd/MM/yyyy;</value>
        </entry>
      </job-data-map>
    </job-detail>

    <trigger>
      <simple>
        <name>Radio Guide Trigger</name>
        <group>DEFAULT</group>
        <misfire-instruction>MISFIRE_INSTRUCTION_RESCHEDULE_NOW_WITH_REMAINING_REPEAT_COUNT</misfire-instruction>
        <job-name>Radio Guide</job-name>
        <job-group>DEFAULT</job-group>
        <repeat-count>-1</repeat-count>
        <repeat-interval>1800000</repeat-interval>
      </simple>
    </trigger>
  </job>

The disadvantage of using scripted CRON style jobs instead of Quartz is that they tend not to have exception handling in place so there is no visibility on when and why they failed.

blog comments powered by Disqus