一、 前言
1. 在使用spring task scheduler 之前,需要先学习搭建springmvc框架,了解cron表达式
① springmvc框架搭建可参考
http://blog.csdn.net/jxq0816/article/details/76084911
② cron表达式可参考
http://blog.csdn.net/jxq0816/article/details/51620400
2. github源代码
https://github.com/jxq0816/task_scheduler
3. 运行结果图
二、搭建过程
1. web.xml 配置应用上下文信息context-param,这里提供了三种方式,根据实际需求,选择一种方式即可
①spring-context-task.xml通过xml配置cron
②spring-context-task-pool.xml添加任务线性池的相关配置信息
③spring-context-task-pool-annotation.xml添加注解方式来设置cron
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<!-- applicationContext.xml默认地址目录是/WEB-INF-->
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/applicationContext.xml
classpath:/spring-context-task.xml
classpath:/spring-context-task-pool.xml
classpath:/spring-context-task-pool-annotation.xml
</param-value>
</context-param>
<!-- 加入ContextLoaderListener 启动Web容器时,自动装配ApplicationContext的配置信息-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--DispatcherServlet是前置控制器,拦截匹配的请求,Servlet拦截匹配规则要自己定义,把拦截下来的请求,依据相应的规则分发到目标Controller来处理-->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/dispatcherServlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2. 应用上下文信息
必要信息:
①在xmlns中添加
xmlns:task="http://www.springframework.org/schema/task"
②在xsi:schemaLocation中添加内容
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
xml样例
① spring-context-task.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
">
<bean id="segmentTask" class="com.week7i.share.SegmentTask"/>
<task:scheduled-tasks>
<task:scheduled ref="segmentTask" method="segment" cron="*/1 * * * * ?"/>
</task:scheduled-tasks>
</beans>
② spring-context-task-pool.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="segmentTask" class="com.week7i.share.SegmentTask"/>
<!-- 配置任务线性池 -->
<!--参数pool主要解决,多个调度并行的问题-->
<!--如果有5个scheduled-task任务,建议设置3到5个调度-->
<!--如果配置参数为1,5个task任务会依次执行,如果一个时间超出,后面的任务一直在等待,影响业务-->
<task:scheduler id="sgScheduler" pool-size="10"/>
<task:scheduled-tasks scheduler="sgScheduler">
<task:scheduled ref="segmentTask" method="segmentPool" cron="*/1 * * * * ?"/>
</task:scheduled-tasks>
</beans>
③ spring-context-task-pool-annotation.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.week7i.share"/>
<!-- 配置任务线性池 -->
<!--task:scheduler 参数pool主要解决,多个调度并行的问题-->
<!--如果有5个scheduled-task任务,建议设置3到5个调度-->
<!--如果配置参数为1,5个task任务会依次执行,如果一个时间超出,后面的任务一直在等待,影响业务-->
<!--The size of the ScheduledExecutorService's thread pool. The default is 1.-->
<task:scheduler id="annotationScheduler" pool-size="10"/>
<!--The size of the executor's thread pool as either a single value or a range-->
<!--(e.g. 5-10). If no bounded queue-capacity value is provided, then a max value-->
<!--has no effect unless the range is specified as 0-n. In that case, the core pool-->
<!--will have a size of n, but the 'allowCoreThreadTimeout' flag will be set to true.-->
<!--If a queue-capacity is provided, then the lower bound of a range will map to the-->
<!--core size and the upper bound will map to the max size. If this attribute is not-->
<!--provided, the default core size will be 1, and the default max size will be-->
<!--Integer.MAX_VALUE (i.e. unbounded).-->
<task:executor id="annotationExecutor" pool-size="10"/>
<!-- 启用annotation方式,在方法添加 @Scheduler,可以配置cron定时任务 -->
<task:annotation-driven scheduler="annotationScheduler" executor="annotationExecutor" proxy-target-class="true"/>
<!-- 如果不需要配置线性池,直接添加<task:annotation-driven />即可 -->
</beans>
4. 任务方法
① SegmentTask.java
package com.week7i.share;
import org.springframework.stereotype.Component;
public class SegmentTask {
public void segment(){
System.out.println("spring scheduled task!");
}
public void segmentPool(){
System.out.println("spring scheduled task pool!");
}
}
② SegmentAnnotationTask.java
package com.week7i.share;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
@Lazy(value=false)
public class SegmentAnnotationTask {
@Scheduled(cron = "*/1 * * * * ?")//每隔1秒执行一次
public void segment(){
System.out.println("spring scheduled task annocation!");
}
}