LoginSignup
5
5

More than 5 years have passed since last update.

Quartz Schedulerでジョブを即時発行

Last updated at Posted at 2016-11-21

環境

こちらの記事と同様です。
Quartz Schedulerでワンタイムジョブ発行

動作

  1. WEBサーバの形式でアプリケーションを実行します。
  2. /api/job/onetime?value=fooにPOSTリクエストすると、ジョブを実行開始するだけですぐさまレスポンス200が返ってきます。
  3. すぐにジョブが実行されて、サーバログに「Executiong HelloJob ! : [valueの値"foo"] | at [実行日時]」が出力されます。

コード

ジョブの定義

こちらの記事と同様です。
Quartz Schedulerでワンタイムジョブ発行 - ジョブの定義

スケジューラの定義・生成

こちらの記事と同様です。
Quartz Schedulerでワンタイムジョブ発行 - スケジューラの定義・生成

HelloJobを実行するためのサンプルアプリ(RESTコントローラ)

クラスの定義はこちらの記事と同様です。
Quartz Schedulerでワンタイムジョブ発行 - HelloJobを実行するためのサンプルアプリ(RESTコントローラ)

メソッドだけ違います。

JobResource.java


    /**
     * ジョブの即時実行
     *
     * @param value
     * @return
     * @throws URISyntaxException
     */
    @ApiOperation(value = "ジョブの即時実行", notes = "ワンタイムジョブを即時実行します。ジョブの登録処理だけですぐにレスポンスが返ってきます。")
    @RequestMapping(value = "/kick",
        method = RequestMethod.POST,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public ResponseEntity<String> kickJob(
        @RequestParam(value = "value", defaultValue = "lol") String value
    ) throws URISyntaxException, SchedulerException {
        log.debug("REST request to kick job just now : " + value);

        JobDetail job = newJob(HelloJob.class)
            .withIdentity("job2", "group1")
            .storeDurably()  // <==========※これが必要!記述してないと例外「org.quartz.SchedulerException: Jobs added with no trigger must be durable.」が発生します。
            .build();
        job.getJobDataMap().put("value", value);
        scheduler.addJob(job, true);
        scheduler.triggerJob(job.getKey());

        return ResponseEntity.ok().body("job kicked! : " + value);
    }
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5