在Java中,實現定時任務有多種方式,本文介紹4種,Timer和TimerTask、Spring、QuartZ、Linux Cron。
以上4種實現定時任務的方式,Timer是最簡單的,不需要任何框架,僅僅JDK就可以,缺點是僅僅是個時間間隔的定時器,調度簡單;Spring和QuartZ都支持cron,功能都很強大,Spring的優點是稍微簡單一點,QuartZ的優點是沒有Spring也可使用;Linux Cron是個操作系統級別的定時任務,適用于所有操作系統支持的語言,缺點是精度只能到達分鐘級別。
# Timer和TimerTask
關于Timer定時器的實現原理,如果我們看過JDK源碼,就會發現,是使用的Object.wait(timeout),來進行的線程阻塞,timeout是根據下次執行實際和當前實際之差來計算。實際上,這可以歸結為一個多線程協作(協作都是在互斥下的協作)問題。
在java.util.concurrent中,有個ScheduledThreadPoolExecutor,也可以完全實現定時任務的功能。
而其他的框架,無非是功能的增強,特性更多,更好用,都是在基礎的java之上的包裝。
代碼示例如下:
~~~
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:**
beans根節點增加內容:xmlns:task="http://www.springframework.org/schema/task"
beans根節點中,xsi:schemaLocation屬性下增加:
http://www.springframework.org/schema/task http://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"
fi
~~~
- 前言
- Java之旅--如何從草根成為技術專家
- 《深入理解Java虛擬機》學習筆記
- 《Spring3.X企業應用開發實戰》學習筆記--IoC和AOP
- 《Tomcat權威指南》第二版學習筆記
- Java之旅--多線程進階
- Java之旅--Web.xml解析
- 《Spring3.X企業應用開發實戰》學習筆記--DAO和事務
- 《Spring3.X企業應用開發實戰》學習筆記--SpringMVC
- Java之旅--定時任務(Timer、Quartz、Spring、LinuxCron)
- Spring實用功能--Profile、WebService、緩存、消息、ORM
- JDK框架簡析--java.lang包中的基礎類庫、基礎數據類型
- JDK框架簡析--java.util包中的工具類庫
- JDK框架簡析--java.io包中的輸入輸出類庫
- Java之旅--通訊
- Java之旅--XML/JSON
- Java之旅--Linux&amp;java進階(看清操作系統層面的事)
- Java之旅--硬件和Java并發(神之本源)
- Java之旅--設計模式
- jetty