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 は、独自の特定の実装を持つ Java SE 1.4、Java SE 5、および Java EE 環境などのさまざまな JDK で Executor の実装を隠すために作成された抽象化レイヤーです。

Quartz Scheduler は、CRON ベースまたはシンプルな定期的なタスクの実行を可能にする本格的なスケジューリング フレームワークです。

Spring Scheduler は、Quartz スケジューラーTriggerの全機能を使用するために、Quartz スケジューラーとの統合を の形で提供します。

Quartz Scheduler 固有のクラスを直接使用せずに Spring Scheduler を使用する利点は、抽象化レイヤーが柔軟性と疎結合を提供することです。

于 2016-07-25T10:54:58.137 に答える