本文前提,確保自己的web項目可以正常運行,即spring基本依賴包本地齊全。
spring的定時任務有3種,一一驗證,親測可用。
一、Spring-Task
<?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"/>
<!-- 定時任務 -->
<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>
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>userTask</param-value>
</context-param>