(一)简介
SpringTask是Spring3.0以后自主开发的定时任务工具,可以将它比作一个轻量级的Quartz,而且使用起来很简单,除spring相关的包外不需要额外的包,而且支持注解和配置文件两种形式,
(二)注解和配置文件适用场景
1.注解
主要适用于任务调度较少的场景,使用简单,方便。
2.配置文件
主要适用于任务调度较多的场景,方便合理规划任务调度时间。当任务调度较多时,我们需要对所有的任务调度进行统一规划,防止某段时间执行任务过度,导致任务执行较慢(线程资源有限,得一个任务线程执行完,下一个任务线程才能开启)
(三)注解和配置文件使用实例
1.配置文件方式
(1)编写任务调度类和方法
/**
* @author **
* @date 2018/6/10 19:42
* “@Component("businessTask")”注解:这里面的value("businessTask"),我们在配置文件中需要使用该值,用以
* 指向任务调度类(BusinessTask)
*/
@Component("businessTask")
public class BusinessTask {
/**
* 同步商品已售数量
*/
public void synchronizedNumber(){
//任务调度方法
}
}
(2)编写配置文件ApplicationContext-task
主要需要引入SpringTask的命名空间
<?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">
<!-- Spring3.0以后自带的任务调度 配置文件 -->
<!-- 扫描task包下的任务调度类,实现向Spring容器注册 -->
<context:component-scan base-package="com.imooc.task"/>
<!-- 配置任务调度 -->
<task:scheduled-tasks>
<!--ref参数指定的即任务类,method指定的即需要运行的方法,cron及cronExpression表达式-->
<!--每小时同步一次商品已售数量-->
<task:scheduled ref="businessTask" method="synchronizedNumber" cron="0 0 0/1 * * ? "/>
</task:scheduled-tasks>
</beans>
备注:网上有“Cron在线生成工具”
2.注解方式
@Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Scheduled
{
public abstract String cron();
public abstract long fixedDelay();
public abstract long fixedRate();
}
可以看出该注解有三个方法或者叫参数,分别表示的意思是:
cron:指定cron表达式。
fixedDelay:即表示从上一个任务完成开始到下一个任务开始的间隔,单位是毫秒。
fixedRate:即从上一个任务开始到下一个任务开始的间隔,单位是毫秒。
(1)编写任务调度类和方法
/**
* @author **
* @date 2018/6/10 19:42
* “@Component("businessTask")”注解:这里面的value("businessTask"),我们在配置文件中需要使用该值,用以
* 指向任务调度类(BusinessTask)
*/
@Component("businessTask")
public class BusinessTask {
/**
* 同步商品已售数量
*/
@Scheduled(cron = "cron="0 0 0/1 * * ?")
public void synchronizedNumber(){
//任务调度方法
}
}
(2)编写配置文件
主要需要引入SpringTask的命名空间
<?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">
<!-- Spring3.0以后自带的任务调度 配置文件 -->
<!-- 扫描task包下的任务调度类,实现向Spring容器注册 -->
<context:component-scan base-package="com.imooc.task"/>
<!-- 配置任务调度 -->
<!-- 理论上只需要加上<task:annotation-driven />这句配置就可以了,这些参数都不是必须的。 -->
<!—开启这个配置,spring才能识别@Scheduled注解 —>
<task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
<task:scheduler id="qbScheduler" pool-size="10"/>
</beans>