3 Replies Latest reply on Oct 15, 2009 3:28 AM by kapitanpetko

    Quartz scheduler - how stop jobs firing concurrently

      Hi,

      I'm using quartz scheduler in seam. Scheduler is starting / running and pausing fine - but when I resume it then the amount of threads set org.quartz.threadPool.threadCount is firing concurrently.

      According to quartz documentation this should be done:




      How do I keep a Job from firing concurrently?


      Make the job class implement StatefulJob rather than Job. Read the JavaDOC for StatefulJob for more information.

      I think that it's solution for my problem - is it supported by seam QuartzTriggerHandle?



      In this method I'm starting my scheduler:



           @Override
           @Begin(join=true)
           public void initTimer() {
                if(timer != null) {
                     log.error("Timer is already created.");
                     return;
                }
                timer = clockTick.clockTick(TIMER_INTERVAL);
                if(timer == null) {
                     log.error("Timer is null. Returning.");
                }
           }




      I've also method for pausing the scheduler:




           @Override
           public void pauseTimer() {
                log.debug("Pausing timer.");
                if(timer == null) {
                     log.error("Timer is null. Returning.");
                     return;
                }
                try {
                     timer.pause();
                } catch (SchedulerException e) {
                     log.error("Pausing scheduler exception: {0}", e.getMessage());
                }          
                log.debug("Timer paused.");
           }



      And for resuming scheduler:



           @Override
           public void resumeTimer() {
                log.debug("Resuming timer.");
                if(timer == null) {
                     log.error("Timer is null. Returning.");
                     return;
                }
                try {
                     timer.resume();
                } catch (SchedulerException e) {
                     log.error("Resuming scheduler exception: {0}", e.getMessage());
                }          
                log.debug("Timer resumed.");
           }




        • 1. Re: Quartz scheduler - how stop jobs firing concurrently
          kapitanpetko

          QuartzTriggerHandle pauses/resumes the job's trigger, shouldn't matter what the Job is. Check out the source, it is quite simple, just
          a thin wrapper around Quartz methods.


          HTH


          • 2. Re: Quartz scheduler - how stop jobs firing concurrently

            Hi,

            I assume that this peace of code which needs to be changes (marked: change to StatefulJob):



               public static class QuartzJob implements Job /* change to StatefulJob */
               {
                  private Asynchronous async;
                  
                  public QuartzJob() { }
            
                  public void execute(JobExecutionContext context)
                      throws JobExecutionException
                  {
                     JobDataMap dataMap = context.getJobDetail().getJobDataMap();
                     async = (Asynchronous)dataMap.get("async");
                     QuartzTriggerHandle handle = new QuartzTriggerHandle(context.getTrigger().getName());
                     try
                     {
                        async.execute(handle);
                     }
                     catch (Exception e) 
                     {
                        async.handleException(e, handle);
                     }
                  }
               }




            • 3. Re: Quartz scheduler - how stop jobs firing concurrently
              kapitanpetko

              Something like that. If you are OK with all your jobs being stateful, you could override Seam's QuartzDispatcher and use StaefulJob
              for ever async method. Not sure what impact on performance etc. this will have. Or, you could schedule your jobs using the Quartz API directly,
              without using @Asynchronous.