cron表達式的格式如下
{秒} {分} {時} {日期(具體哪天)} {月} {星期}
秒:必填項,允許的值範圍是0-59,支持的特殊符號包括 , - * /,
, 表示特定的某一秒纔會觸發任務,
- 表示一段時間內會觸發任務,
* 表示每一秒都會觸發,
/ 表示從哪一個時刻開始,每隔多長時間觸發一次任務。
分:必填項,允許的值範圍是0-59,支持的特殊符號和秒一樣,含義類推
時:必填項,允許的值範圍是0-23,支持的特殊符號和秒一樣,含義類推
日期:必填項,允許的值範圍是1-31,支持的特殊符號相比秒多了?,
? 表示與{星期}互斥,即意味着若明確指定{星期}觸發,則表示{日期}無意義,以免引起衝突和混亂。
月:必填項,允許的值範圍是1-12(JAN-DEC),支持的特殊符號與秒一樣,含義類推
星期:必填項,允許值範圍是1~7 (SUN-SAT),
1 代表星期天(一星期的第一天),以此類推,7代表星期六,
支持的符號相比秒多了?,表達的含義是與{日期}互斥,即意味着若明確指定{日期}觸發,則表示{星期}無意義。
<!-- 每半分鐘觸發任務 -->
<task:scheduled ref="app" method="execute1" cron="30 * * * * ?"/>
<!-- 每小時的10分30秒觸發任務 -->
<task:scheduled ref="app" method="execute2" cron="30 10 * * * ?"/>
<!-- 每天1點10分30秒觸發任務 -->
<task:scheduled ref="app" method="execute3" cron="30 10 1 * * ?"/>
<!-- 每月20號的1點10分30秒觸發任務 -->
<task:scheduled ref="app" method="execute4" cron="30 10 1 20 * ?"/>
<!-- 每年10月20號的1點10分30秒觸發任務 -->
<task:scheduled ref="app" method="execute5" cron="30 10 1 20 10 ?"/>
<!-- 每15秒、30秒、45秒時觸發任務 -->
<task:scheduled ref="app" method="execute6" cron="15,30,45 * * * * ?"/>
<!-- 15秒到45秒每隔1秒觸發任務 -->
<task:scheduled ref="app" method="execute7" cron="15-45 * * * * ?"/>
<!-- 每分鐘的每15秒時任務任務,每隔5秒觸發一次 -->
<task:scheduled ref="app" method="execute8" cron="15/5 * * * * ?"/>
<!-- 每分鐘的15到30秒之間開始觸發,每隔5秒觸發一次 -->
<task:scheduled ref="app" method="execute9" cron="15-30/5 * * * * ?"/>
<!-- 每小時的0分0秒開始觸發,每隔3分鐘觸發一次 -->
<task:scheduled ref="app" method="execute10" cron="0 0/3 * * * ?"/>
<!-- 星期一到星期五的10點15分0秒觸發任務 -->
<task:scheduled ref="app" method="execute11" cron="0 15 10 ? * MON-FRI"/>
xml配置文件實現
(1).pom.xml
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
</dependencies>
(2).spring配置文件
<?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-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">
<!-- 配置註解掃描 -->
<context:component-scan base-package="com.rhwayfun.task"/>
第一種:
<task:scheduler id="taskScheduler" pool-size="100" />
<task:scheduled-tasks scheduler="taskScheduler">
<!-- 每半分鐘觸發任務 -->
<task:scheduled ref="app" method="execute1" cron="30 * * * * ?"/>
</task:scheduled-tasks>
第二種:
<bean name="testTask" class="com.zyj.main.App" lazy-init="false"></bean>
<task:scheduled-tasks>
<task:scheduled ref="testTask" method="execute1" cron="0/5 * * * * ?"/>
</task:scheduled-tasks>
</beans>
測試代碼
@Component public class App {
public void execute1(){
System.out.printf("Task: %s, Current time: %s\n", 1, LocalDateTime.now());
}
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/appcontext-task.xml");
}
}
參考:http://blog.csdn.net/u011116672/article/details/52517247