定时器jar引入
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
spring配置quartz的xml
<beans xmlns="http://www.springframework.org/schema/beans" 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-3.2.xsd"> <bean name="thirdCron" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"> <!-- 指向执行任务的类的路径--> <bean class="com.wyn.quartz.CleanZndzJob"/> </property> <!-- 指向执行任务的类的的执行方法--> <property name="targetMethod" value="executeCleanJob"/> <!--作业不并发调度--> <property name="concurrent" value="false"/> </bean> <!--按照一定频率的触发器--> <!-- <bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean"> --> <!-- <property name="jobDetail" ref="thirdCron"/> --> <!-- 调度工厂实例化后,经过0秒开始执行调度 --> <!-- <property name="startDelay" value="0"/> --> <!-- 每2秒调度一次 --> <!-- <property name="repeatInterval" value="2000"/> --> <!-- </bean> --> <!--Cron表达式触发器--> <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="thirdCron"/> <!-- 30分钟执行一次 --> <!-- <property name="cronExpression" value="0 */30 * * * ?"/> --> <property name="cronExpression"> <!--每隔30分钟执行一次 --> <value>0 */30 * * * ?</value> <!--每隔3秒执行一次 --> <!-- <value>*/3 * * * * ?</value> --> </property> </bean> <!--配置调度工厂--> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <!--<ref bean="simpleTrigger"/>--> <ref bean="cronTrigger"/> </list> </property> </bean> </beans>
定时任务的类
package com.wondersgroup.moduls.aidaozhen.quartz; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import com.wondersgroup.moduls.aidaozhen.service.impl.ZndzServiceImpl; public class CleanZndzJob { private static final Logger logger = LogManager.getLogger(CleanZndzJob.class); public void executeCleanJob() { logger.info("定期清理数据。。。。。"); } }
原文地址:https://www.cnblogs.com/wangyanei2017/p/9586144.html
时间: 2024-10-08 02:50:44