2011年9月27日 星期二

[Java] Quartz Scheduler - CronTrigger

Tutorial: http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/tutorial-lesson-06

We cannot use CronTrigger without knowing Cron Expressions. Cron-Expressions are used to configure instances of CronTrigger. Cron-Expressions are strings that are actually made up of seven sub-expressions, that describe individual details of the schedule.

For example, "0 0/1 * * * ?" is an expression to create a trigger that simply fires every 1 minute.

Sample code:
package demo;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

import java.text.ParseException;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;

public class CronTriggerTest {
    public static void run() {
        try {
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
            scheduler.start();
        
            JobDetail job = newJob(HelloJob.class)
                    .withIdentity("job1", "group1")
                    .build();

            Trigger trigger = newTrigger()
                    .withIdentity("trigger3", "group1")
                    .withSchedule(cronSchedule("0 0/1 * * * ?"))
                    .build();

            scheduler.scheduleJob(job, trigger);

            try {
                Thread.sleep(600000);
            } catch (InterruptedException e) {}
            
            scheduler.shutdown();
        } catch (SchedulerException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}

Sample console output:
28 [main] INFO org.quartz.impl.StdSchedulerFactory - Using default implementation for ThreadExecutor
31 [main] INFO org.quartz.simpl.SimpleThreadPool - Job execution threads will use class loader of thread: main
47 [main] INFO org.quartz.core.SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
48 [main] INFO org.quartz.core.QuartzScheduler - Quartz Scheduler v.2.0.2 created.
49 [main] INFO org.quartz.simpl.RAMJobStore - RAMJobStore initialized.
50 [main] INFO org.quartz.core.QuartzScheduler - Scheduler meta-data: Quartz Scheduler (v2.0.2) 'DefaultQuartzScheduler' with instanceId 'NON_CLUSTERED'
  Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
  NOT STARTED.
  Currently in standby mode.
  Number of jobs executed: 0
  Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads.
  Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered.

50 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
50 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler version: 2.0.2
50 [main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.
5731 [Timer-0] INFO org.quartz.utils.UpdateChecker - New Quartz update(s) found: 2.1.0 [http://www.terracotta.org/kit/reflector?kitID=default&pageID=QuartzChangeLog]
Hello! HelloJob is executing.
Hello! HelloJob is executing.
Hello! HelloJob is executing.
Hello! HelloJob is executing.
Hello! HelloJob is executing.
Hello! HelloJob is executing.
Hello! HelloJob is executing.
Hello! HelloJob is executing.
Hello! HelloJob is executing.
Hello! HelloJob is executing.
600092 [main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutting down.
600092 [main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED paused.
600092 [main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutdown complete.

Enjoy!

沒有留言:

張貼留言