一尘不染

Quartz Job和带有Spring的计划任务之间的区别?

java

我是Spring-boot(版本1.3.6)和Quartz的新手,我想知道使用Spring-
scheduler
进行任务之间有什么区别:

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

石英方式

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是否提供了更灵活的方式来定义Jobs,Triggers和Scheduler,或者Spring Scheduler还有其他更好的方法?


阅读 352

收藏
2020-12-03

共1个答案

一尘不染

Spring Scheduler是一个抽象层,用于隐藏执行程序在不同JDK(如Java SE 1.4,Java SE 5和Java
EE环境)中的实现,这些JDK具有各自的特定实现。

Quartz Scheduler是一个成熟的调度框架,它允许基于CRON或简单定期任务执行。

Spring Scheduler确实提供了与Quartz Scheduler的集成,Trigger以使用Quartz Scheduler的全部功能。

使用Spring Scheduler而不直接使用Quartz Scheduler特定类的优势是抽象层提供了灵活性和松散耦合。

2020-12-03