Java之旅--定時任務(Timer、Quartz、Spring、LinuxCron)

在Java中,實現定時任務有多種方式,本文介紹4種,Timer和TimerTask、Spring、QuartZ、Linux Cron。java

以上4種實現定時任務的方式,Timer是最簡單的,不須要任何框架,僅僅JDK就能夠,缺點是僅僅是個時間間隔的定時器,調度簡單;Spring和QuartZ都支持cron,功能都很強大,Spring的優勢是稍微簡單一點,QuartZ的優勢是沒有Spring也可以使用;Linux Cron是個操做系統級別的定時任務,適用於全部操做系統支持的語言,缺點是精度只能到達分鐘級別。python

Timer和TimerTask

關於Timer定時器的實現原理,若是咱們看過JDK源碼,就會發現,是使用的Object.wait(timeout),來進行的線程阻塞,timeout是根據下次執行實際和當前實際之差來計算。實際上,這能夠歸結爲一個多線程協做(協做都是在互斥下的協做)問題。spring

在java.util.concurrent中,有個ScheduledThreadPoolExecutor,也能夠徹底實現定時任務的功能。多線程

而其餘的框架,無非是功能的加強,特性更多,更好用,都是在基礎的java之上的包裝。併發

代碼示例以下:app

import java.util.Date; import java.util.Timer; import java.util.TimerTask; public class TimerTest extends TimerTask { private Timer timer; public static void main(String[] args) { TimerTest timerTest= new TimerTest(); timerTest.timer = new Timer(); //馬上開始執行timerTest任務,只執行一次 timerTest.timer.schedule(timerTest,new Date()); //馬上開始執行timerTest任務,執行完本次任務後,隔2秒再執行一次 //timerTest.timer.schedule(timerTest,new Date(),2000); //一秒鐘後開始執行timerTest任務,只執行一次 //timerTest.timer.schedule(timerTest,1000); //一秒鐘後開始執行timerTest任務,執行完本次任務後,隔2秒再執行一次 //timerTest.timer.schedule(timerTest,1000,2000); //馬上開始執行timerTest任務,每隔2秒執行一次 //timerTest.timer.scheduleAtFixedRate(timerTest,new Date(),2000); //一秒鐘後開始執行timerTest任務,每隔2秒執行一次 //timerTest.timer.scheduleAtFixedRate(timerTest,1000,2000); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } //結束任務執行,程序終止 timerTest.timer.cancel(); //結束任務執行,程序並不終止,由於線程是JVM級別的 //timerTest.cancel(); } @Override public void run() { System.out.println("Task is running!"); } } 

使用spring @Scheduled註解執行定時任務

這種方式很是簡單,卻能使用cron完成和QuartZ同樣的功能,值得推薦一下。框架

ApplicationContext.xml:ide

beans根節點增長內容:xmlns:task="http://www.springframework.org/schema/task"測試

beans根節點中,xsi:schemaLocation屬性下增長:spa

http://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.1.xsd

task:annotation-driven/

實現類:

@Component  //import org.springframework.stereotype.Component; public class MyTestServiceImpl implements IMyTestService { @Scheduled(cron="0/5 * * * * ? ") //每5秒執行一次 @Override public void myTest(){ System.out.println("進入測試"); } } 

注意幾點:

spring的@Scheduled註解  須要寫在實現上;

定時器的任務方法不能有返回值;

實現類上要有組件的註解@Component,@Service,@Repository

QuartZ

QuartZ With Spring

applicationContext-schedule.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <bean id="quartzScheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <!-- 啓動的Trigger列表 --> <ref local="startThriftTrigger" /> </list> </property> <property name="quartzProperties"> <props> <prop key="org.quartz.threadPool.threadCount">5</prop> </props> </property> <!-- 啓動時延期3秒開始任務 --> <property name="startupDelay" value="3" /> <property name="autoStartup" value="${scheduler.autoStartup}" /> </bean> <!-- 啓動Thrift,當即啓動 --> <bean id="startThriftTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean"> <property name="startDelay" value="0" /> <property name="repeatInterval" value="1000" /> <property name="repeatCount" value="0" /> <property name="jobDetail" ref="startThriftTask" /> </bean> <bean id="startThriftTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject" ref="startThrift" /> <property name="targetMethod" value="execute" /> <!-- 同一任務在前一次執行未完成而Trigger時間又到時是否併發開始新的執行, 默認爲true. --> <property name="concurrent" value="true" /> </bean> </beans> 

實現類

package xx.schedule; @Component public class StartThrift { /** * 調度入口 */ public void execute() { // to do something } } 

QuartZ No Spring

使用的QuartZ jar是1.8.5,以下:

<dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>1.8.5</version> </dependency> 

調用類實現代碼以下:

import org.quartz.CronExpression; import org.quartz.CronTrigger; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.impl.StdSchedulerFactory; public class InvokeStatSchedule { public void start() throws SchedulerException { SchedulerFactory schedulerFactory = new StdSchedulerFactory(); Scheduler scheduler = schedulerFactory.getScheduler(); //InvokeStatJob是實現了org.quartz.Job的類 JobDetail jobDetail = new JobDetail("jobDetail", "jobDetailGroup", InvokeStatJob.class); CronTrigger cronTrigger = new CronTrigger("cronTrigger", "triggerGroup"); try { CronExpression cexp = new CronExpression("0 0 * * * ?"); cronTrigger.setCronExpression(cexp); } catch (Exception e) { e.printStackTrace(); } scheduler.scheduleJob(jobDetail, cronTrigger); scheduler.start(); } } 

定時任務類代碼以下:

import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; public class InvokeStatJob implements Job { @Override public void execute(JobExecutionContext arg0) throws JobExecutionException { //...要定時操做的內容 } } 

Linux Cron

這其實也是一種很是廣泛的實現定時任務的方式,實際是操做系統的定時任務。Linux Cron只能到達分鐘級,到不了秒級別。

通常咱們是設置定時執行一個sh腳本,在腳本里面寫一些控制代碼,例如,有以下的腳本:

3,33 * * * * /usr/local/log_parser/run_log_parser.sh &

run_log_parser.sh的內容大體以下:

#!/bin/sh log_parser_dir=/usr/local/log_parser tmp_file=/usr/local/run_parser_tmp.txt parser_log=/usr/local/access_parser.log tmpDir=/data/applogs/access_logs_bp date >> "$parser_log" if [! -f "$tmp_file"]; then echo '訪問日誌解析正在進行,還沒有完成' >> "$parser_log" echo '' >> "$parser_log" else echo '開始解析訪問日誌' >> "$parser_log" touch "$tmp_file" cd "$log_parser_dir" python access_log_parser.py >> "$parser_log" rm "$tmp_file" echo '解析訪問日誌完成' >> "$parser_log" echo '' >> "$parser_log" cd "$tmpDir" gzip gzip WEB0* mv *.gz gz/ echo '壓縮備份日誌及移動到壓縮目錄成功' >> "$parser_log" fifrom: https://www.kancloud.cn/digest/java-travel/159427
相關文章
相關標籤/搜索