使用Spring的Quartz作业和调度任务之间的区别? [英] Difference between Quartz Job and Scheduling Tasks with Spring?

查看:626
本文介绍了使用Spring的Quartz作业和调度任务之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring-boot(版本1.3.6)和Quartz的新手,我想知道用 Spring-scheduler

I am new to Spring-boot(version 1.3.6) and Quartz and I am wondering what is the difference between making a task with Spring-scheduler:

    @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还有其他的东西是什么更好?

Does Quartz provide more flexible way to define Jobs, Triggers and Schedulers or Spring Scheduler has something else which is better?

推荐答案

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

Spring Scheduler is an abstraction layer written to hide the implementations of Executors in different JDKs like Java SE 1.4, Java SE 5 and Java EE environments, which have their own specific implementations.

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

Quartz Scheduler is a fully fledged scheduling framework which allows CRON based or Simple periodic task execution.

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

Spring Scheduler does provide integration with Quartz scheduler in the form of a Trigger to use the full functionality of the Quartz scheduler.

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

Advantage of using Spring Scheduler without directly using the Quartz Scheduler specific classes is that the abstraction layer provides flexibility and loose coupling.

这篇关于使用Spring的Quartz作业和调度任务之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆