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的,就是允许并行。

相关推荐
TYC98 分钟前
Mac创建Vue3+TypeScript或者React+TypeScript项目
前端·javascript·typescript
程序员小孔.9 分钟前
基于 SpringBoot + UniApp + Vue 的家政服务系统设计与实现
前端·数据库·spring boot·python·docker·微信小程序
黄鹂鸟科技11 分钟前
软件定制开发设计要点:从需求梳理、原型确认到敏捷交付与持续维护
后端·物联网·系统架构
Profile排查笔记15 分钟前
指纹浏览器 GitHub 项目怎么选?先看仓库交付的是工具、内核还是管理器
前端·人工智能·后端·自动化
高频因子挖掘机19 分钟前
多股票行情为什么容易对不齐?用 Pandas MultiIndex 管理标的、日期与缺失值
后端·github·api
风骏时光牛马20 分钟前
云原生驱动的分布式高可用架构设计与落地实践
前端
梦曦i26 分钟前
uni-app x 专属 Pinia 库正式发布
前端·pinia·uni-app x
高频因子挖掘机37 分钟前
Python 量化数据接口设计实战:用数据契约解决字段混乱与回测不一致
后端·github·api
一次旅行1 小时前
DeepSeek‑V4.1‑Flash‑1017完整解读:模型升级+Search/Fetch搜索API开放,Agent开发者要注意哪些坑
前端·网络·人工智能
猿究院_夏燊8 小时前
Spring Bean 生命周期
java·后端·spring