实现Spring定时任务
实现过程
- 在spring-servlet.xml文件中引入task的命名空间;
xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"
- 配置task的扫描注解;
<task:annotation-driven scheduler="qbScheduler" mode="proxy" /> <task:scheduler id="qbScheduler" />
- 使用@Scheduled(cron = “时间格式串”) 实现定时任务;
@Scheduled(cron = "* * * * * ?")//每分钟都执行一次 public void test(){ System.out.println("执行"); }
cron 参数设置
一个cron表达式有至少6个(也可能7个)有空格分隔的时间元素。
按顺序依次为
秒(0~59)
分钟(0~59)
小时(0~23)
天(月)(0~31,但是你需要考虑你月的天数)
月(0~11)
天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)
年份(1970-2099)
"0/5 * * * * ?" 每5秒一次触发
"0 0 8 * * ?" 每天早晨8点触发
"0 30 12 ? * *" 每天早中午12:30触发
"0 0/5 14,18 * * ?" 每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发
spring-servlet.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"
xmlns:task="http://www.springframework.org/schema/task">
<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
<context:component-scan base-package="com.bmw.hdms"
use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
<context:include-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice" />
</context:component-scan>
<!-- 定义跳转的文件的前后缀 ,视图模式配置 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
<property name="order" value="100"></property>
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<property name="maxUploadSize" value="10485760000"></property>
<property name="maxInMemorySize" value="40960"></property>
</bean>
<mvc:default-servlet-handler />
<mvc:annotation-driven />
<mvc:view-controller path="" view-name="home/index" />
<mvc:view-controller path="/index" view-name="home/index" />
<mvc:view-controller path="/welcome" view-name="home/welcome" />
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="i18n"></property>
</bean>
<!-- 定时任务 -->
<task:annotation-driven scheduler="qbScheduler"
mode="proxy" />
<task:scheduler id="qbScheduler" />
</beans>
java代码展示
代码必须放在Controller层,不然Spring可能检测不到
@Scheduled(cron = "0/5 * * * * ?") //每隔5秒执行一次定时任务
public void consoleInfo(){
System.out.println("定时任务"+new Date());
}