2016-07-25 91 views
14

我是新來的春天啓動(版本1.3.6)和Quartz和我想知道是什麼與Spring-scheduler製作任務之間的區別:Quartz作業和Spring安排任務之間的區別?

@Scheduled(fixedRate = 40000) 
    public void reportCurrentTime() { 
     System.out.println("Hello World"); 
    } 

而且Quartz way

0. Create sheduler. 
1. Job which implements Job interface. 
2. Create JobDetail which is instance of the job using the builder org.quartz.JobBuilder.newJob(MyJob.class) 
3. Create a Triger 
4. Finally set the job and the trigger to the scheduler 

在代碼:

public class HelloJob implements Job { 

    public HelloJob() { 
    } 

    public void execute(JobExecutionContext context) 
     throws JobExecutionException 
    { 
     System.err.println("Hello!"); 
    } 
    } 

和sheduler:

SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory(); 

    Scheduler sched = schedFact.getScheduler(); 

    sched.start(); 

    // define the job and tie it to our HelloJob class 
    JobDetail job = newJob(HelloJob.class) 
     .withIdentity("myJob", "group1") 
     .build(); 

    // Trigger the job to run now, and then every 40 seconds 
    Trigger trigger = newTrigger() 
     .withIdentity("myTrigger", "group1") 
     .startNow() 
     .withSchedule(simpleSchedule() 
      .withIntervalInSeconds(40) 
      .repeatForever()) 
     .build(); 

    // Tell quartz to schedule the job using our trigger 
    sched.scheduleJob(job, trigger); 

Quartz是否提供更靈活的方式來定義作業,觸發器和調度程序或者Spring Scheduler有其他更好的方法?

回答

11

Spring Scheduler是一個抽象層,用於隱藏執行程序在不同JDK中的實現,如Java SE 1.4,Java SE 5和Java EE環境,它們都有自己的特定實現。

Quartz Scheduler是一個完全成熟的調度框架,它允許基於CRON或簡單的定期任務執行。

Spring Scheduler確實以Trigger的形式提供與Quartz調度程序的集成,以使用Quartz調度程序的全部功能。

使用Spring Scheduler而不直接使用Quartz Scheduler特定類的好處是,抽象層提供了靈活性和鬆散耦合。

相關問題