title: quartz: preventing concurrent instances of a job in jobs.xml
body: <p>This should be really easy. I'm using Quartz running under Apache Tomcat 6.0.18, and I have a <a href="http://www.quartz-scheduler.org/docs/cookbook/JobInitPlugin.html" rel="noreferrer">jobs.xml file</a> which sets up my scheduled job that runs every minute.</p>
  
  <p>What I would like to do, is if the job is still running when the next trigger time rolls around, I don't want to start a new job, so I can let the old instance complete.</p>
  
  <p>Is there a way to specify this in jobs.xml (prevent concurrent instances)?</p>
  
  <p>If not, is there a way I can share access to an in-memory singleton within my application's <a href="http://www.quartz-scheduler.org/docs/api/org/quartz/Job.html" rel="noreferrer">Job</a> implementation (is this through the <a href="http://www.quartz-scheduler.org/docs/api/org/quartz/JobExecutionContext.html" rel="noreferrer">JobExecutionContext</a>?) so I can handle the concurrency myself? (and detect if a previous instance is running)</p>
  
  <hr>
  
  <p><strong>update:</strong> After floundering around in the docs, here's a couple of approaches I am considering, but either don't know how to get them to work, or there are problems.</p>
  
  <ol>
  <li><p>Use <a href="http://www.quartz-scheduler.org/docs/api/org/quartz/StatefulJob.html" rel="noreferrer">StatefulJob</a>. This prevents concurrent access... but I'm not sure what other side-effects would occur if I use it, also I want to avoid the following situation:</p>
  
  <p>Suppose trigger times would be every minute, i.e. trigger#0 = at time 0, trigger #1 = 60000msec, #2 = 120000, #3 = 180000, etc. and the trigger#0 at time 0 fires my job which takes 130000msec. With a plain Job, this would execute triggers #1 and #2 while job trigger #0 is still running. With a StatefulJob, this would execute triggers #1 and #2 in order, immediately after #0 finishes at 130000. I don't want that, I want #1 and #2 not to run and the next trigger that runs a job should take place at #3 (180000msec). So I still have to do something else with StatefulJob to get it to work the way I want, so I don't see much of an advantage to using it.</p></li>
  <li><p>Use a <a href="http://www.quartz-scheduler.org/docs/api/org/quartz/TriggerListener.html" rel="noreferrer">TriggerListener</a> to return true from vetoJobExecution().</p>
  
  <p>Although implementing the interface seems straightforward, I have to figure out how to setup one instance of a TriggerListener declaratively. <a href="https://stackoverflow.com/questions/2676514/quartz-documentation-for-xml-files">Can't find the docs for the xml file</a>.</p></li>
  <li><p>Use a <code>static</code> shared thread-safe object (e.g. a semaphore or whatever) owned by my class that implements Job.</p>
  
  <p>I don't like the idea of using singletons via the <code>static</code> keyword under Tomcat/Quartz, not sure if there are side effects. Also I really don't want them to be true singletons, just something that is associated with a particular job definition.</p></li>
  <li><p>Implement my own <a href="http://www.quartz-scheduler.org/docs/api/org/quartz/Trigger.html" rel="noreferrer">Trigger</a> which extends <a href="http://www.quartz-scheduler.org/docs/api/org/quartz/SimpleTrigger.html" rel="noreferrer">SimpleTrigger</a> and contains shared state that could run its own TriggerListener.</p>
  
  <p>Again, I don't know how to setup the XML file to use this trigger rather than the standard <code>&lt;trigger&gt;&lt;simple&gt;...&lt;/simple&gt;&lt;/trigger&gt;</code>.</p></li>
  </ol>