Tuesday, October 23, 2012

Scheduler framework in Enterprise application


Many times Enterprise product has scheduling requirements where same type of tasks/Job   need to be executed again and again. For this we rely on some third party framework. There are different scheduling framework available for use and few of them are below

 In this blog we are going to first compare with the Spring Task Scheduler then discuss Quartz Scheduler which is a Enterprise Job Scheduler Framework from project from Opensymphony .

When to use Spring Task Scheduler?  -when our requirements are 'Simple' for eg.

    -Run tasks once after specified time
    -Schedule tasks to run repeatedly (fixed-rate as well as fixed delay)
    -Scheduling tasks based on simple cron expressions, etc.

On the other hand, when the requirements  are complex say the framework supports enterprise-class features like Persistent triggers, Clustering, JTA Transactions, etc. – Quartz scheduler is what you need

 Why to go for Quartz?  

Quartz is very light-weight, and requires very little setup/configuration - it can actually be used 'out-of-the-box' if your needs are relatively basic.Quartz is fault-tolerant and can persist our  scheduled jobs between system restarts.

Quartz is free for use silenced under Apache Licensing.

 Types of  Quartz Jobs:

  • Simple Quartz Job
  • Simple triggers
  • Cron triggers

Job stores:
  • RAMJobStore: This has volatile storage of the quartz job details.
  • JDBCJobStore: It  maintains the quartz job details over a database via the JDBC. Here the information on the quartz jobs, triggers, calendars etc are available any time in case the system has a downtime and then can be rescheduled once the system is up. We can reschedule the Job. 
Cron expressions consist of the following seven fields:
  • Seconds
  • Minutes
  • Hours
  • Day-of-month
  • Month
  • Day-of-week
  • Year (optional field)
The backslash (/) character denotes value increments. For example, "5/15" in the seconds field means every 15 seconds starting at the fifth second.

The question (?) character and the letter-L (L) character are permitted only in the day-of-month and day-of-week fields.  

  • The letter-W (W) character in the day-of-month field schedules execution on the weekday nearest to the value specified. Placing "1W" in the day-of month field schedules execution for the weekday nearest the first of the month.
  • The pound (#) character specifies a particular instance of a weekday for a given month. Placing "MON#2" in the day-of-week field schedules a task on the second Monday of the month.
  • The asterisk (*) character is a wildcard character and indicates that every possible value can be taken for that specific field.
  Scheduling a Cron Job:













 // Initiate a Schedule Factory
      SchedulerFactory sf = new StdSchedulerFactory("quartz.properties"); 
      // Retrieve a scheduler from schedule factory
        Scheduler scheduler = schedulerFactory.getScheduler(); 
        // current time
        long ctime = System.currentTimeMillis();
       
        // Initiate JobDetail with job name, job group, and executable job class
        JobDetail jobDetail =
            new JobDetail("jobDetail_1", "jobDetailGroup_1", SimpleQuartzJob.class);
        // Initiate CronTrigger with its name and group name
        CronTrigger cronTrigger = new CronTrigger("cronTrigger_1", "triggerGroup_1");
           try {
            // setup CronExpression
            CronExpression cexp = new CronExpression("0/6 * * * * ?");

         // Assign the CronExpression to CronTrigger
            cronTrigger.setCronExpression(cexp);
        } catch (Exception e) {
            e.printStackTrace();
        }
     cronTrigger.getJobDataMap().put("var1", "Value1");
     //Adding job listner to the scheduler
     scheduler.addJobListener(new MyJobListener());      
     // schedule a job with JobDetail and Trigger

//The name of a job or trigger must be unique within its group.      scheduler.scheduleJob(jobDetail, cronTrigger);
    // start the scheduler
    scheduler.start();
 Rescheduling / Deleting Jobs :
 // Initiate a Schedule Factory
  SchedulerFactory sf = new StdSchedulerFactory("quartz.properties");  
 // Retrieve a scheduler from schedule factory
 Scheduler scheduler = sf .getScheduler();
 String[] triggerGroups;
 String[] triggers;
 triggerGroups = scheduler.getTriggerGroupNames();
 for (int i = 0; i < triggerGroups.length; i++) {
    triggers = scheduler.getTriggerNames(triggerGroups[i]);
    for (int j = 0; j < triggers.length; j++) {
    Trigger tg = scheduler.getTrigger(triggers[j], triggerGroups[i]);
    
    if (tg instanceof SimpleTrigger && tg.getName().equals("cronTrigger_1")) {
     ((SimpleTrigger)tg).setRepeatCount(100);
     // reschedule the job
     scheduler.rescheduleJob(triggers[j], triggerGroups[i], tg);
     // unschedule the job
     //scheduler.unscheduleJob(triggersInGroup[j], triggerGroups[i]);
    }
    }
 }        

 // start the scheduler
 scheduler.start();
 Fore more refer to http://www.quartz-scheduler.org/

No comments:

Post a Comment