一、定時任務之Timer:
1.首先我們新建一個任務類(eg:MyTimerTssk)繼承TimerTask類,從TimerTask(public abstract class TimerTask implements Runnable)源碼中可以看出,該類是一個抽象類並且實現了Runnable接口,但run方法還是一個抽象方法。在我們新建的MyTimerTask類中重寫run()方法,該方法主要是包含線程要執行的內容,也就是定時執行的事件。代碼如下:
/**
*
* @ClassName: MyTimeTask
* @Description: 任務類
* @author ll
* @date 2018年9月17日 上午11:57:38
*
*/
public class MyTimerTask extends TimerTask{
@override
public void run(){
//這裏是要定時執行的事件,這裏只是簡單打印一句話
System.out.println("開始執行....");
}
}
2.測試定時任務
/**
*
* @ClassName: TestTimer
* @Description: 測試定時任務
* @author ll
* @date 2018年9月17日 上午11:58:31
*
*/
public class TestTimer {
public static void main(String[] args) {
//創建Timer實例
Timer timer = new Timer();
// public void schedule(TimerTask task, long delay, long period)schedule()方法有3個參數,第一個是TimerTask對象,
//第二個是個開始執行後或者可以設置服務啓動2秒後執行,第三個參數表示時間間隔
timer.schedule(new MyTimeTask(), 2000, 1000);
}
}
二、定時任務之SpringTask
1.創建一個java web項目,引入spring相關jar,spirng3.0後自帶task調度工具,這裏我們只要引入spring-context和spring-web的jar。
2.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<!-- spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- spring核心監聽器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
3.對於springTask的使用,有兩種方式,分別是基於xml和註解(annotation)的方式。
3.1基於xml的方式:一般我們把定時任務的業務邏輯寫在service層,先定義一個接口:
package com.ll.service;
/**
*
* @ClassName: TaskService
* @Description: 定時任務業務層
* @author ll
* @date 2018年9月17日 下午1:19:09
*
*/
public interface TaskService {
public void readTask();
public void writeTask();
}
3.2接口實現類
package com.ll.service.impl;
import org.springframework.stereotype.Service;
import com.ll.service.TaskService;
/**
*
* @ClassName: TaskServiceImpl
* @Description: 業務邏輯實現層
* @author ll
* @date 2018年9月17日 下午1:21:36
*
*/
@Service
public class TaskServiceImpl implements TaskService {
public void readTask() {
System.out.println("the reading task is begenning...");
}
public void writeTask() {
System.out.println("the writing task is begenning...");
}
}
3.3編寫applicationContext.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"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<bean id="taskService" class="com.ll.service.impl.TaskServiceImpl"></bean>
<!-- 配置定時規則 -->
<task:scheduled-tasks>
<!-- 可以配置多個定時任務 -->
<task:scheduled ref="taskService" method="readTask" initial-delay="2000" fixed-delay="3000"/>
<task:scheduled ref="taskService" method=writeTask" initial-delay="4000" fixed-delay="5000"/>
</task:scheduled-tasks>
</beans>
在applicationContext.xml文件中注意要引入springTask的約束:http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd”
method:對應業務層的方法名。
initial-delay:表示當服務器啓動多久後執行(單位:毫秒)
fixed-delay:表示間隔多久執行(單位:毫秒)
3.4 基於註解的方式:
package com.ll.service.impl;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.ll.service.TaskService;
/**
*
* @ClassName: TaskServiceAnnotationImpl
* @Description: 基於註解方式實現
* @author ll
* @date 2018年9月17日 下午1:43:12
*
*/
@Service
public class TaskServiceAnnotationImpl implements TaskService {
@Scheduled(initialDelay=2000,fixedDelay=3000)
public void readTask() {
System.out.println("the reading task is begenning...");
}
@Scheduled(initialDelay=4000,fixedDelay=5000)
public void writeTask() {
System.out.println("the writing task is begenning...");
}
}
修改相應的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"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">
<!-- <bean id="taskService" class="com.ll.service.impl.TaskServiceImpl"></bean> -->
<!-- 配置定時規則 -->
<!-- <task:scheduled-tasks> -->
<!-- 可以配置多個定時任務 -->
<!-- <task:scheduled ref="taskService" method="readTask" initial-delay="1000" fixed-delay="2000"/>
<task:scheduled ref="taskService" method="writeTask" initial-delay="2000" fixed-delay="3000"/>
</task:scheduled-tasks> -->
<!-- 開啓註解掃描 -->
<context:component-scan base-package="com.ll"></context:component-scan>
<!-- 開啓對@Scheduled註解的支持 -->
<task:annotation-driven/>
</beans>
使用Timer對於簡單的時間定時任務處理方便,但對於複雜的時間要求確難以實現,而springTask對複雜的時間定時任務有很好的支持,可用cron表達式來定義複雜的定時規則。