Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Quartz作业和Spring调度任务之间的区别?_Java_Spring_Quartz Scheduler_Spring Scheduled - Fatal编程技术网

Java Quartz作业和Spring调度任务之间的区别?

Java Quartz作业和Spring调度任务之间的区别?,java,spring,quartz-scheduler,spring-scheduled,Java,Spring,Quartz Scheduler,Spring Scheduled,我不熟悉Spring boot(1.3.6版)和Quartz,我想知道使用以下工具执行任务有什么区别: 及 代码: public class HelloJob implements Job { public HelloJob() { } public void execute(JobExecutionContext context) throws JobExecutionException { System.err.println(

我不熟悉Spring boot(1.3.6版)和Quartz,我想知道使用以下工具执行任务有什么区别:

代码:

  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调度器还有其他更好的功能?

Spring调度器是一个抽象层,用于隐藏不同JDK(如Java SE 1.4、Java SE 5和Java EE环境)中执行器的实现,它们有自己的具体实现

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

Spring Scheduler以
触发器的形式提供与Quartz Scheduler的集成,以使用Quartz Scheduler的全部功能


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

所以通常我们可以说使用
Spring调度器
是更好的选择?虽然与和中的
Quartz调度器
?相比可能会有一些开销,但Quartz在
集群
中似乎更强大。而且基于目前的情况,现在使用
Quartz
似乎确实容易多了。
  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);