Spring+Quartz实现定时任务的配置方法

复制代码
<?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"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--  Scheduler配置 -->
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="teachingProcessTrigger" />
			</list>
		</property>
		<property name="quartzProperties">
			<props>
				<prop key="org.quartz.threadPool.threadCount">3</prop>
			</props>
		</property>
	</bean>
<!--  Trigger配置 -->
	<bean id="teachingProcessTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		<property name="jobDetail">
			<ref bean="teachingProcess" />
		</property>
		<property name="cronExpression">
			<!--  每天凌晨0点执行一次 -->
			<value>0 0 0 * * ?</value>
		</property>
	</bean>
<!--  JobDetail配置 -->
	<bean id="teachingProcess"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject">
    <!-- hrQuartzServiceImpl是一个业务类,在其他地方声明了bean,这里直接引用就可以 -->
			<ref bean="hrQuartzServiceImpl" />
		</property>
		<property name="targetMethod">
    <!-- hrQuartzServiceImpl类里作为执行入口的方法名 -->
			<value>doTeachingProcess</value>
		</property>
	</bean>
</beans>

任务有并行和串行之分,并行是指:一个定时任务,当执行时间到了的时候,立刻执行此任务,不管当前这个任务是否在执行中;串行是指:一个定时任务,当执行时间到了的时候,需要等待当前任务执行完毕,再去执行下一个任务。

quartz框架中可以设置是否允许任务并行:

如果是通过MethodInvokingJobDetailFactoryBean在运行中动态生成的Job,配置的xml文件有个concurrent属性,这个属性的功能是配置此job是否可以并行运行,如果为false则表示不可以并行运行,否则可以并行。如果一个job的业务处理发费的时间超过了job的启动的间隔时间(repeatInterval),这个属性非常有用。如果为false,那么,在这种情况下,当前job还在运行,那么下一个job只能延时运行。如果为true,那么job就会并行运行,配置示例如下:

复制代码
<bean id="jobCompareB2cAndLocal" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
	<property name="targetObject " ref="delegateJob " />
	<property name="targetMethod " value="方法名" />
	<property name="concurrent " value="false " />
</bean >

如果不配置,默认是true的,就是允许并行。

相关推荐
小二·13 分钟前
Python Web 开发进阶实战:AI 伦理审计平台 —— 在 Flask + Vue 中构建算法偏见检测与公平性评估系统
前端·人工智能·python
走粥22 分钟前
选项式API与组合式API的区别
开发语言·前端·javascript·vue.js·前端框架
We་ct25 分钟前
LeetCode 12. 整数转罗马数字:从逐位实现到规则复用优化
前端·算法·leetcode·typescript
AC赳赳老秦26 分钟前
跨境科技服务的基石:DeepSeek赋能多语言技术文档与合规性说明的深度实践
android·大数据·数据库·人工智能·科技·deepseek·跨境
changzehai26 分钟前
Rust + VSCode + probe-rs搭建stm32-rs嵌入式开发调试环境
vscode·后端·stm32·rust·嵌入式·probe-rs
终端域名32 分钟前
如何选择有利于品牌宣传的网站域名
java·后端·struts·数字货币域名·网站域名
拽着尾巴的鱼儿33 分钟前
Spring:定时任务@Scheduled cron 的实现原理
java·后端·spring
方安乐33 分钟前
react笔记之useMemo
前端·笔记·react.js
郑州光合科技余经理35 分钟前
源码部署同城O2O系统:中台架构开发指南
java·开发语言·后端·架构·系统架构·uni-app·php
晚霞的不甘37 分钟前
解决 Flutter for OpenHarmony 构建失败:HVigor ERROR 00303168 (SDK component missing)
android·javascript·flutter