Quartz定時任務框架(一) 入門案例

z1340954953發表於2018-07-31

介紹

Quartz是一套支援時間排程的框架,官網http://www.quartz-scheduler.org/, jar 包下載,點這裡

入門案例

1. quartz.properties檔案

這個檔案不是必須建立的,如果需要建立使用,必須建立在calsspath下面,對於web工程,編譯後放到WEB-INF/classes下面

#定義scheduler的名稱
org.quartz.scheduler.instanceName = MyScheduler
#定義quartz的執行緒池中最大的執行緒數量,意味著最多能有幾個job同時執行
org.quartz.threadPool.threadCount = 3
#所有scheduler的資料都是放在記憶體中的,這個類是官網建議的類用來儲存資料,給配置上
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore

 2. 簡單案例

public static void main(String[] args) throws InterruptedException {
		try {
			//從排程程式工廠獲取一個排程程式的例項
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
            //輸出從quartz.properties檔案中設定的排程程式的名稱
            System.out.println("自定義的排程程式的名稱:"+scheduler.getSchedulerName());
            //........ 加入具體的定時任務
            
            scheduler.start();//啟動scheduler,將啟動具體的job
            scheduler.shutdown();//關閉scheduler,將job執行緒停止

        } catch (SchedulerException se) {
            se.printStackTrace();
        }
	}

2. 定義具體的任務

*  定義一個類實現job介面,在execute方法內,執行任務的具體內容

這個類必須是public,否則將會拋異常:

Class org.quartz.simpl.SimpleJobFactory can not access a member of class com.jiaoyiping.stuty_quartz.SimpleJob2 with modifiers ""

package cn.bing.quartz;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class HelloJob implements Job{
	@Override
	public void execute(JobExecutionContext paramJobExecutionContext)
			throws JobExecutionException {
		 System.err.println("["+Thread.currentThread().getName()+"]"+"Hello World!  MyJob is executing.");
	}
}

* 將job配置進去schelar並且設定任務的執行時間間隔

 // 定義一個job並且繫結到HelloJob上,這裡並不會馬上建立hellojob例項,在scheduler排程執行任務時候建立
            JobDetail job = JobBuilder.newJob (HelloJob.class).withIdentity("job1", "group1")
                .build();
            //配置任務是馬上執行的,並且是1秒執行一次
            Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger1", "group1")
                .startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(1)
                        .repeatForever())            
                .build();

            //將任務的配置應用到job上
            scheduler.scheduleJob(job, trigger);

完整的程式碼

package cn.bing.quartz;

import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
public class Myquartz {
	public static void main(String[] args) throws InterruptedException {
		try {
			//從排程程式工廠獲取一個排程程式的例項
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
            //輸出從quartz.properties檔案中設定的排程程式的名稱
            System.out.println("自定義的排程程式的名稱:"+scheduler.getSchedulerName());

            // 定義一個job並且繫結到HelloJob上,這裡並不會馬上建立hellojob例項,在scheduler排程執行任務時候建立
            JobDetail job = JobBuilder.newJob (HelloJob.class).withIdentity("job1", "group1")
                .build();
            //配置任務是馬上執行的,並且是1秒執行一次
            Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger1", "group1")
                .startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(1)
                        .repeatForever())            
                .build();

            //將任務的配置應用到job上
            scheduler.scheduleJob(job, trigger);
            
            scheduler.start();//啟動任務
            //為了排程的任務執行緒執行,需要休眠一段時間,才呼叫shutdown方法關閉排程執行緒
            //或者不呼叫shutdown方法
            Thread.sleep(10000);
            scheduler.shutdown();//停止任務

        } catch (SchedulerException se) {
            se.printStackTrace();
        }
	}
}

列印日誌:

[MyScheduler_Worker-1]Hello World!  MyJob is executing.
[MyScheduler_Worker-2]Hello World!  MyJob is executing.
[MyScheduler_Worker-3]Hello World!  MyJob is executing.
[MyScheduler_Worker-1]Hello World!  MyJob is executing.
[MyScheduler_Worker-2]Hello World!  MyJob is executing.
[MyScheduler_Worker-3]Hello World!  MyJob is executing.
[MyScheduler_Worker-1]Hello World!  MyJob is executing.
[MyScheduler_Worker-2]Hello World!  MyJob is executing.
[MyScheduler_Worker-3]Hello World!  MyJob is executing.
[MyScheduler_Worker-1]Hello World!  MyJob is executing.
[MyScheduler_Worker-2]Hello World!  MyJob is executing.

 

相關文章