根据项目需求,需要一个定时任务每天执行一次,Spring 3.0之后自带了task调度工具,比Quartz更加的简单方便,相当于轻量级的Quartz,除spring核心包外不需要引入额外的jar包,支持注解和配置文件两种形式,特此介绍。
1. 配置文件方式
编写job类
package cn.com.job.order;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.stereotype.Component;
@Component("orderFileJob")
public class OrderFileJob {
private static final long serialVersionUID = -7194794905986149036L;
public void executeJob() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
System.out.println("executeJob........." + sdf.format(new Date()));
}
}
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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
default-lazy-init="false">
<context:component-scan base-package="cn.com.job.order" />
<task:scheduled-tasks>
<task:scheduled ref="orderFileJob" method="executeJob" cron="0/10 * * * * ?"/> <!-- 测试,每10秒执行一次 -->
</task:scheduled-tasks>
</beans>
添加beans标签 xmlns:task=“http://www.springframework.org/schema/task” 和http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd" ;
设置spring注解扫描<context:component-scan base-package=“cn.com.job.order” />;
task标签的ref设置指定的job类,method设置要运行的方法,cron设置定时周期
2. 注解方式
编写job类,需要在运行方法上添加注解@Scheduled,并配置表达式
package cn.com.job.order;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component("orderFileJob")
public class OrderFileJob {
private static final long serialVersionUID = -7194794905986149036L;
@Scheduled(cron="0/10 * * * * ?")
public void executeJob() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
System.out.println("executeJob........." + sdf.format(new Date()));
}
}
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:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"
default-lazy-init="false">
<context:component-scan base-package="cn.com.job.order" />
<task:annotation-driven/>
</beans>
相比于第一种方式,采用注解方式只需在配置文件中添加<task:annotation-driven/>即可。