2011年9月23日 星期五

[Java] Quartz Quick Start (Scheduler)

Download and Installation of Quartz

To download Quartz, visit http://www.quartz-scheduler.org/downloads and download current stable release (2.0.2).

Once downloaded and unpacked the zip file, it's done.

Starting a Sample Application
  1. New a Java project in Eclipse.
  2. Add external JARs:
    • quartz-all-2.0.2.jar
    • slf4j-api-1.6.2.jar & slf4j-simple-1.6.2.jar. (slf4j-api needs to bind with one and only one of slf4j-nop, slf4j-simple, slf4j-log4j12 or slf4j-jdk14. Surely, if we bind with slf4j-log4j12, we need log4j JAR.)
  3. Main.java
    package demo;
    
    public class Main {
        public static void main(String[] args) {
            QuartzTest.run();
        }
    }
    
  4. QuartzTest.java
    package demo;
    
    import org.quartz.Scheduler;
    import org.quartz.SchedulerException;
    import org.quartz.impl.StdSchedulerFactory;
    
    public class QuartzTest {
        public static void run() {
            try {
                Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
                scheduler.start();
                scheduler.shutdown();
            } catch (SchedulerException e) {
                e.printStackTrace();
            }
        }
    }
    
  5. Run
    32 [main] INFO org.quartz.impl.StdSchedulerFactory - Using default implementation for ThreadExecutor
    35 [main] INFO org.quartz.simpl.SimpleThreadPool - Job execution threads will use class loader of thread: main
    50 [main] INFO org.quartz.core.SchedulerSignalerImpl - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
    51 [main] INFO org.quartz.core.QuartzScheduler - Quartz Scheduler v.2.0.2 created.
    52 [main] INFO org.quartz.simpl.RAMJobStore - RAMJobStore initialized.
    53 [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.
    
    53 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
    53 [main] INFO org.quartz.impl.StdSchedulerFactory - Quartz scheduler version: 2.0.2
    54 [main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED started.
    54 [main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutting down.
    54 [main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED paused.
    54 [main] INFO org.quartz.core.QuartzScheduler - Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutdown complete.
    
Enjoy!

Reference

沒有留言:

張貼留言