spring Quartz定时任务

本文介绍如何使用 Spring 框架结合 Quartz 实现定时任务的配置与管理。通过 Maven 引入 Quartz 依赖,并详细展示了配置文件及定时任务类的实现方式,包括触发器与任务调度的具体设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

不多说直接上代码  大笑



用的maven管理哈哈

		<dependency>
			<groupId>org.quartz-scheduler</groupId>
			<artifactId>quartz</artifactId>
			<version>1.8.5</version>
		</dependency>



首先是配置文件

<?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-3.1.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.1.xsd">


	<bean id="reloadTask" class="com.demo.schedule.SchedulerTask"
		init-method="initial"></bean>


	<bean id="jobDetail-reload"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject">
			<ref bean="reloadTask" />
		</property>
		<property name="targetMethod">
			<value>reload</value>
		</property>
	</bean>


	<bean id="trigger-reload"
		class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="jobDetail">
			<ref bean="jobDetail-reload" />
		</property>
		<property name="cronExpression">
			<value>0 0 0 * * ? </value>
		</property>
	</bean>

	<bean id="registerQuartz"
		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="trigger-reload" />
			</list>
		</property>
	</bean>

</beans>

定时任务类

package com.demo.schedule;

import java.text.ParseException;

import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class SchedulerTask {

	@Autowired
	@Qualifier("trigger-reload")
	private CronTrigger cronTrigger;

	@Autowired
	@Qualifier("registerQuartz")
	private Scheduler scheduler;

	public void initial() throws ParseException, SchedulerException {
		checkScheduleTime();
	}

	private void checkScheduleTime() throws ParseException, SchedulerException {

           // 5秒一次
		String scheduleTime = "0/5 * * * * ?";
		if (scheduleTime != null) {
			String oriCronExpression = cronTrigger.getCronExpression();
			if (!oriCronExpression.equals(scheduleTime)) {
				cronTrigger.setCronExpression(scheduleTime);
				scheduler.rescheduleJob("trigger-reload", Scheduler.DEFAULT_GROUP, cronTrigger);
			}
		}
	}

	public static int ORIGINAL_VERSION = 0;

	public void reload() throws Exception {

		int versionId = 0; // 可以从数据库取版本号比对更新,对吧 嘿嘿。。

		if (ORIGINAL_VERSION == versionId) {
			ORIGINAL_VERSION = versionId;
			System.out.println("start ==============...");
		}
	}

}


ok   完事   复制到工程中绝对好使  。。。。。。。。。。。。。。。。。。。。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值