淘寶買的quartz

簡介

---01---

核心接口:

調度器通過觸發器執行任務。

---02---

觸發器:

大前提是給定的時間

shi

大前提是每隔一段時間

年可以省略的。

---03---

簡單的案例:

代碼:

第一步:我們首先看下這個HelloJob:

簡單的觸發示例:

/* 
 * All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved. 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
 * use this file except in compliance with the License. You may obtain a copy 
 * of the License at 
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0 
 *   
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 * License for the specific language governing permissions and limitations 
 * under the License.
 * 
 */

package cn.itcast.quartz.example;

import static org.quartz.DateBuilder.evenMinuteDate;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * This Example will demonstrate how to start and shutdown the Quartz scheduler and how to schedule
 * a job to run in Quartz.
 * 
 * @author Bill Kratzer
 */
public class SimpleExample {
    public void run() throws Exception {
        Logger log = LoggerFactory.getLogger(SimpleExample.class);
        log.info("------- Initializing ----------------------");
        // 定義調度器
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = sf.getScheduler();
        log.info("------- Initialization Complete -----------");
        // 獲取當前時間的下一分鐘
        Date runTime = evenMinuteDate(new Date());
        log.info("------- Scheduling Job  -------------------");
        // 定義job
        // 在quartz中,有組的概念,組+job名稱 唯一的
        JobDetail job = newJob(HelloJob.class).withIdentity("job1", "group1").build();
        // 定義觸發器,在下一分鐘啓動
        Trigger trigger = newTrigger().withIdentity("trigger1", "group1").startAt(runTime).build();
        // 將job註冊到調度器
        sched.scheduleJob(job, trigger);
        log.info(job.getKey() + " will run at: " + runTime);
        // 啓動調度器
        sched.start();
        log.info("------- Started Scheduler -----------------");
        // 等待65秒
        log.info("------- Waiting 65 seconds... -------------");
        try {
            // wait 65 seconds to show job
            Thread.sleep(65L * 1000L);
            // executing...
        } catch (Exception e) {
            //
        }
        // 關閉調度器
        log.info("------- Shutting Down ---------------------");
        sched.shutdown(true);
        log.info("------- Shutdown Complete -----------------");
    }
    public static void main(String[] args) throws Exception {
        SimpleExample example = new SimpleExample();
        example.run();
    }
}

---04---

複雜的調度:

/* 
 * All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved. 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
 * use this file except in compliance with the License. You may obtain a copy 
 * of the License at 
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0 
 *   
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 * License for the specific language governing permissions and limitations 
 * under the License.
 * 
 */

package cn.itcast.quartz.example;

import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.DateBuilder.evenMinuteDate;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * This Example will demonstrate how to start and shutdown the Quartz scheduler and how to schedule
 * a job to run in Quartz.
 * 
 * @author Bill Kratzer
 */
public class SimpleCronExample {
    public static int a = 0;
    public static int b = 0;

    public void run() throws Exception {
        Logger log = LoggerFactory.getLogger(SimpleCronExample.class);

//        log.info("------- Initializing ----------------------");

        // 定義調度器
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = sf.getScheduler();

//        log.info("------- Initialization Complete -----------");

        // 獲取當前時間的下一分鐘
        Date runTime = evenMinuteDate(new Date());

//        log.info("------- Scheduling Job  -------------------");

        // 定義job
        JobDetail job = newJob(HelloJob.class).withIdentity("job", "group1").build();

        // 定義觸發器,每2秒執行一次
        Trigger trigger = newTrigger().withIdentity("trigger", "group1")
                .withSchedule(cronSchedule("0/1 * * * * ?")).build();

        // 將job註冊到調度器
        sched.scheduleJob(job, trigger);
//        log.info(job.getKey() + " will run at: " + runTime);

        // 啓動調度器
        sched.start();

//        try {
//            Thread.sleep(6L * 1000L);
//        } catch (Exception e) {
//            //
//        }
//        Trigger trigger1 = newTrigger().withIdentity("trigger1", "group1")
//                .withSchedule(cronSchedule("0/1 * * * * ?")).build();
//        JobDetail job1 = newJob(HelloJob1.class).withIdentity("job1", "group1").build();
//        sched.scheduleJob(job1,trigger1);

//        log.info("------- Started Scheduler -----------------");

        // 等待1分鐘
//        log.info("------- Waiting 60 seconds... -------------");
        try {
            Thread.sleep(600L * 10000L);
        } catch (Exception e) {
            //
        }

        // 關閉調度器
        log.info("------- Shutting Down ---------------------");
        sched.shutdown(true);
        log.info("------- Shutdown Complete -----------------");
    }

    public static void main(String[] args) throws Exception {

        SimpleCronExample example = new SimpleCronExample();
        example.run();

    }

}

---05---

和spring整合。

---06--

一個觸發只能由一個job,一個job可以由多個觸發。

---07---

關閉淘寶訂單。

mvc的標籤的用法:https://www.cnblogs.com/afeng2010/p/10133797.html

---08---

配置問題:https://blog.csdn.net/cdnight/article/details/79807689?utm_medium=distribute.pc_relevant_right.none-task-blog-BlogCommendFromMachineLearnPai2-3.nonecase&depth_1-utm_source=distribute.pc_relevant_right.none-task-blog-BlogCommendFromMachineLearnPai2-3.nonecase

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章