可以用来做一些定时小任务。例子如下:
项目结构:
beans.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-4.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<context:component-scan base-package="com.oliver.schedule" />
<task:executor id="myExecutor" pool-size="5" />
<task:scheduler id="myScheduler" pool-size="10" />
<task:annotation-driven executor="myExecutor" scheduler="myScheduler" />
<context:annotation-config/>
</beans>
ShowTime.java
package com.oliver.schedule;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class ShowTime {
static int i = 10;
static int j = 10;
/**
* web应用启动1秒后,每隔5秒运行以下方法
*/
@Scheduled(initialDelay=1000,fixedDelay=5000)
public void show(){
i--;
if(i > 0) {
System.out.println(getTime());
System.out.println("show1 = "+i);
}
}
// cron expression 自定义规则
// 参数顺序
// 秒 分 时 日 月 星期
// "0 0 * * * *" = the top of every hour of every day.
// "*/10 * * * * *" = every ten seconds.
// "0 0 8-10 * * *" = 8, 9 and 10 o'clock of every day.
// "0 0 6,19 * * *" = 6:00 AM and 7:00 PM every day.
// "0 0/30 8-10 * * *" = 8:00, 8:30, 9:00, 9:30, 10:00 and 10:30 every day.
// "0 0 9-17 * * MON-FRI" = on the hour nine-to-five weekdays
// "0 0 0 25 12 ?" = every Christmas Day at midnight
/**
* 10秒执行一次
*/
@Scheduled(cron="0/10 * * * * *")
public void show2() {
j--;
if(j > 0) {
System.out.println(getTime());
System.out.println("show2 = "+j);
}
}
public String getTime() {
return new SimpleDateFormat("HH:mm:ss").format(new Date());
}
}
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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>tourguide</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
直接丢服务器,运行结果如下:
22:36:52
show1 = 9
22:36:57
show1 = 8
22:37:00
show2 = 9
22:37:02
show1 = 7
22:37:07
show1 = 6
22:37:10
show2 = 8
22:37:12
show1 = 5
22:37:17
show1 = 4
22:37:20
show2 = 7
22:37:22
show1 = 3
22:37:27
show1 = 2
22:37:30
show2 = 6
22:37:32
show1 = 1
22:37:40
show2 = 5
22:37:50
show2 = 4
22:38:00
show2 = 3
22:38:10
show2 = 2
22:38:20
show2 = 1