本文前提,确保自己的web项目可以正常运行,即spring基本依赖包本地齐全。
spring的定时任务有3种,一一验证,亲测可用。
一、Spring-Task
xml配置 (注意不要漏掉
xmlns:task="http://www.springframework.org/schema/task")
pojo
<?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.xsd"
>
<!-- Spring定时器注解开关-->
<task:annotation-driven />
<bean id="userTask" class="com.camelot.openplatform.usercenter.web.UserTask"/>
<task:scheduled-tasks >
<task:scheduled ref="userTask" method="pringTask" cron="0/2 * * * * *"/>
</task:scheduled-tasks>
</beans>
package com.camelot.openplatform.usercenter.web;
public class UserTask {
public void pringTask(){
System.out.println("task:scheduled-tasks-----------------tests");
}
}
二、Quartz (需要添加quartz-2.2.0.jar)
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<!-- 要调用的工作类 -->
<bean id="userTask" class="com.camelot.openplatform.usercenter.web.UserTask"/>
<!-- 定义调用对象和调用对象的方法 -->
<bean id="userTaskDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 调用的类 -->
<property name="targetObject">
<ref bean="userTask"/>
</property>
<!-- 调用类中的方法 -->
<property name="targetMethod">
<value>quartzTask</value>
</property>
</bean>
<!-- 定义触发时间 -->
<bean id="userTaskTime" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail">
<ref bean="userTaskDetail"/>
</property>
<!-- cron表达式 -->
<property name="cronExpression">
<value>0/5 * * * * ?</value>
</property>
</bean>
<!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="userTaskTime"/>
</list>
</property>
</bean>
</beans>
package com.camelot.openplatform.usercenter.web;
public class UserTask {
public void quartzTask(){
System.out.println("quartz-tasks-----------------tests");
}
}
三、注解方式
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.xsd">
<!-- pool-size="1-4",表示线程池活跃的线程数为1,最大线程数为4 -->
<task:executor id="executor" pool-size="5" />
<task:scheduler id="scheduler" pool-size="10" />
<task:annotation-driven executor="executor" scheduler="scheduler" />
</beans>
pojo
package com.camelot.openplatform.usercenter.web;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class TaskJob {
@Scheduled(cron="0/5 * * * * ? ") //每5秒执行一次
public void job1() {
System.out.println("TaskJob---任务进行中。。。\n");
System.out.println(Thread.currentThread().getName());
}
}
注意,此种方式需要在主配置文件中加入,组件扫描,否则@comment扫描不到,定时任务不会执行
<context:component-scan base-package="com.camelot"/>
四、引用
方式一以及方式二,作者都是以 spring-task.xml文件单独存放便于管理 通常在 spring-context.xml 中引入
<!-- 定时任务 -->
<import resource="spring/spring-task.xml" />
<beans profile="userTask">
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="userTaskTime"/>
</list>
</property>
</bean>
</beans>
同时 web.xml中需要添加
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>userTask</param-value>
</context-param>