19

我是 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!");
    }
  }

和调度程序:

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 调度程序有其他更好的方法?

4

1 回答 1

16

Spring Scheduler 是一个抽象层,用于隐藏不同 JDK 中的 Executor 实现,如 Java SE 1.4、Java SE 5 和 Java EE 环境,它们有自己的特定实现。

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

Spring 调度器确实以 a 的形式提供与 Quartz 调度器的集成,Trigger以使用 Quartz 调度器的全部功能。

在不直接使用 Quartz Scheduler 特定类的情况下使用 Spring Scheduler 的优点是抽象层提供了灵活性和松耦合。

于 2016-07-25T10:54:58.137 回答