定时调度任务quartz
依赖
<!-- quartz --> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.3</version> </dependency>
applicationContext-quartz.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd"> <!--/////////////////////////////定时任务调度///////////////////////////// --> <!-- ================================================================================== --> <!-- ======================== 定义核心调度器 ======================== --> <!-- <bean id="SpringJobSchedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> --> <!-- <property name="triggers"> --> <!-- <list> --> <!-- <ref bean="CronTriggerBean"/> --> <!-- </list> --> <!-- </property> --> <!-- </bean> --> <!-- ======================== 调度触发器 ======================== --> <!--注: quartz1.8.6版本使用org.springframework.scheduling.quartz.CronTriggerBean --> <!-- <bean id="CronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> --> <!-- <property name="jobDetail" > --> <!-- <ref bean="jobDetail" /> --> <!-- </property> --> <!-- 00点02分 --> <!-- <property name="cronExpression" value="0 02 00 * * ? *"/> --> <!-- 00点00分 --> <!-- <property name="cronExpression" value="0 00 00 * * ? *"/> --> <!-- 每1分钟触发一次 --> <!-- <property name="cronExpression" value="0 0/1 * * * ?"/> --> <!-- </bean> --> <!-- ======================== 定义jobDetail ======================== --> <!-- <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> --> <!-- 目标类 --> <!-- <property name="targetObject"> --> <!-- <ref bean="updateStatusQuartz" /> --> <!-- </property> --> <!-- 目标类中要执行的方法 ,这个参数是必须的 --> <!-- <property name="targetMethod"> --> <!-- <value>updateStatus</value> --> <!-- </property> --> <!-- </bean> --> <!-- ======================== 定义具体的任务类 ======================== --> <!-- <bean id="updateStatusQuartz" class="com.mta.caseauditclound.common.quartz.UpdateStatusQuartz"/> --> <!-- ================================================================================== --> <bean id="updateStatusQuartz" class="com.ra.quartz.UpdateStatusQuartz"></bean> <task:scheduled-tasks> <!-- 晚上1点触发 --> <task:scheduled ref="updateStatusQuartz" method="updateStatus" cron="0 0 1 * * ?" /> <!-- 每5秒钟触发 --> <task:scheduled ref="updateStatusQuartz" method="calculateDeviceData" cron= "0/5 * * * * ?"/> <!-- =============集群中第一个tomcat的定时器============= --> <!-- 状态为1,5的定时器,每分钟的第9,19,29,39,49,59秒触发 --> <!-- <task:scheduled ref="updateStatusQuartz" method="auditStatus15" cron= "9,19,29,39,49,59 * * * * ?" /> --> <!-- 状态为3的定时器,每分钟的第9,19,29,39,49,59秒触发 --> <!-- <task:scheduled ref="updateStatusQuartz" method="auditStatus3" cron= "9,19,29,39,49,59 * * * * ?" /> --> <!-- =============集群中第二个tomcat的定时器============= --> <!-- 状态为1,5的定时器,每分钟的第4,14,24,34,44,54秒触发 --> <!-- <task:scheduled ref="updateStatusQuartz" method="auditStatus15" cron= "4,14,24,34,44,54 * * * * ?" /> --> <!-- 状态为3的定时器,每分钟的第9,19,29,39,49,59秒触发 --> <!-- <task:scheduled ref="updateStatusQuartz" method="auditStatus3" cron= "4,14,24,34,44,54 * * * * ?" /> --> <!-- <task:scheduled ref="updateStatusQuartz" method="InsertWeatherWarningInfo" initial-delay="5000" fixed-delay="50000000"/> --> </task:scheduled-tasks> </beans>
package com.ra.quartz; import java.text.SimpleDateFormat; import java.util.Date; import javax.annotation.Resource; import com.ra.truck.service.DataBakService; import com.ra.truck.service.DbWeatherWarningService; import com.ra.truck.service.TdRiskEvaluationInfoService; import com.ra.truck.service.riskModel.RiskModelService; public class UpdateStatusQuartz {/** * 每天凌晨定时任务 */ public void updateStatus() { System.err.println("===每天凌晨的定时任务开始执行,启动时间为==="+ new SimpleDateFormat("yyyy-MM-dd hh:mm:ss EE").format(new Date())); System.out.println("*****我凌晨1点开始执行*****"); } /** * 间隔调用 */ public void calculateDeviceData(){ System.out.println("******我每隔5秒执行一次********"); } public void InsertWeatherWarningInfo(){ System.out.println("******定时执行查询天气预警********"); } }