quartz簡單demo(一個簡單易用的任務調度開源架構)

來源:互聯網
上載者:User

package com.yjck.quartz;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;
public class QuartzTest {
    public static void main(String[] args) throws InterruptedException {
        QuartzTest tqz = new QuartzTest();
        try {
            tqz.startShedule();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    protected void startShedule() throws SchedulerException {
        /* 調度器 */
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        /* 具體執行類 */
        JobDetail jobDetail = new JobDetail("firstJOB",
                scheduler.DEFAULT_GROUP, MyJobClass.class);
        // 觸發器,定義每3秒執行一次
        // Trigger trigger = TriggerUtils.makeSecondlyTrigger(3);
        /* 簡單觸發器,相當於java timetask,CronTrigger 是更為發展的觸發器,有cron運算式 */
        SimpleTrigger trigger = new SimpleTrigger("triger1",
                scheduler.DEFAULT_GROUP, 10, 2000);
        scheduler.scheduleJob(jobDetail, trigger);
        scheduler.start();
//        scheduler.shutdown();
    }

}

    /*事件類別,處理具體的業務*/

package com.yjck.quartz;

import java.util.Date;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

public class MyJobClass implements Job{
    /*事件類別,處理具體的業務*/
    public void execute(JobExecutionContext job) throws JobExecutionException {
        System.out.println("hello quartz"+ new Date());
        
    }

}

項目結構如下

                                                                      

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.