5 Replies Latest reply on Feb 24, 2010 7:57 AM by prem14261

    Problem with Quartz @IntervalCron on startup

    prem14261

      Hi,


      I'm trying to create a scheduled task that runs every day at the given time. And i had a requirement to run this task after some time the server is started. Say 5 minutes after the server start up.


      So, i'm setting @Expiration date to 5 minutes after the server startup. It is not triggering the action when the time expires initially, but works properly for subsequent timeouts as set in the cron string.


      Here is my implemetation:-


      @Name("billingScheduler")
      @AutoCreate
      public class BillingScheduler implements Serializable {
           
           private static final long serialVersionUID = 323760983458853226L;
      
           @Logger
           private Log log;
           
           @In(create=true)
           private BillingProcessor billingProcessor;
           
           @SuppressWarnings("unused")
           private QuartzTriggerHandle callLogTriggerHandle;
           
          private static String CL_CRON_INTERVAL = "0 30 23 * * ?";
       
          public void scheduleBillingTasks() {
               try {
                    Calendar cal = Calendar.getInstance();
                    cal.add(Calendar.MINUTE, 5);
                    cal.set(Calendar.SECOND, 0);
                    
                    Calendar cal2 = Calendar.getInstance ();
                    cal2.set(3000, Calendar.MAY, 10);
                      
                    log.info("############### Scheduling Call Log Cron Job.........");
                    log.info("Setting Call Log Initial Timeout to ::::::::: "+cal.getTime());
                    
                    callLogTriggerHandle = 
                         billingProcessor.createCallLogCronJob(cal.getTime(), CL_CRON_INTERVAL, cal2.getTime());
                    
               } catch (Exception e) {
                    log.error("Error!!!!", e);
      
                }              
           }
      }





      @Name("billingProcessor")
      @AutoCreate
      @Scope(ScopeType.APPLICATION)
      public class BillingProcessor {
      
           @Logger
           private Log log;
      
           @Asynchronous
           @Transactional
           public QuartzTriggerHandle createCallLogCronJob(
                     @Expiration Date when, 
                     @IntervalCron String interval,
                     @FinalExpiration Date endDate) {
                String date = new Date().toString();          
                log.info("@@@@@@@@@@@@@@ Call Log Quartz Test: " + date);
                QuartzTriggerHandle handle = new QuartzTriggerHandle("CallLogTrigger");
                return handle;
           }
      }



      Also, i had added these lines in my components.xml file :-


      <event type="org.jboss.seam.postInitialization"> 
          <action execute="#{billingScheduler.scheduleBillingTasks}"/>
       </event>
       <async:quartz-dispatcher/>
      



      Please help me in this regard and let me know if i'm doing something wrong.


      Thanks,
      Prem 

        • 1. Re: Problem with Quartz @IntervalCron on startup
          kapitanpetko

          You should create the QuartzTriggerHandle, just return null. Besides that your code is OK. The behaviour you are seeing is
          actually correct. @Expiration Date when is set as the startTime of the Quartz trigger by Seam. However, startTime != first file time.
          From the Quartz JavaDoc about setStartTime:


          The time at which the trigger's scheduling should start. May or may not be the first actual fire time of the trigger, 
          depending upon the type of trigger and the settings of the other properties of the trigger. 
          However the first actual first time will not be before this date.
          



          So if you want to make sure your job is run 5 minutes after you start the server, you need to schedule a one-off job, and use the cron job
          for later runs. You will probably need to do some checking so that the two jobs don't run simultaneously if the times coincide.


          HTH


          • 2. Re: Problem with Quartz @IntervalCron on startup
            prem14261

            Thanks Nikolay, is there any API available for scheduling these off jobs?
            Can you please post the sample code or a link where i can find some examples.


            - Prem

            • 3. Re: Problem with Quartz @IntervalCron on startup
              kapitanpetko

              You can schedule them using Seam, as you are doing now. Simply add one more asynchronous method and call both methods from BillingScheduler.


              public class BillingProcessor {
              
                @Asynchronous
                public QuartzTriggerHandle processAfter5Mins(@Expiration Date date) {
                 ...
                }
              
                @Asynchronous
                @Transactional
                public QuartzTriggerHandle createCallLogCronJob(
                             @Expiration Date when, 
                             @IntervalCron String interval,
                             @FinalExpiration Date endDate) {
                ...
                }
               
              

              • 4. Re: Problem with Quartz @IntervalCron on startup
                kapitanpetko

                Check the seampay and quartz example applications in the distribution. Documentation is,as usual, in the manual

                • 5. Re: Problem with Quartz @IntervalCron on startup
                  prem14261

                  It worked great! Thanks for your help Nikolay. I have to make sure in my code that the two times doesn't coincide.


                  - Prem