• 同时上3种手段,保障Quartz不重复执行任务


    Quartz是Java比较流行的定时任务框架,使用定时任务最烦的是不受控制的多线程,定时任务被重复执行。如何控制Quartz让定时任务不被重复执行呢?总结了3中方法和大家分享。

    1. ##修改Quartz的默认线程数,从默认10个线程改为1个线程。
    配置如下:
    schedulerFactoryBean添加属性:configLocation。quartz.properties在Quartz.jar包中拷贝。

    	<bean id="schedulerFactoryBean"
    		class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    		<property name="triggers">
    			<list>
    				<ref bean="cronTrigger">ref>
    			list>
    		property>
    		<property name="configLocation" value="classpath:quartz.properties">property>
    	bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    quartz.properties配置

    org.quartz.scheduler.instanceName: DefaultQuartzScheduler
    org.quartz.scheduler.rmi.export: false
    org.quartz.scheduler.rmi.proxy: false
    org.quartz.scheduler.wrapJobExecutionInUserTransaction: false
    org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool
    org.quartz.threadPool.threadCount: 1
    org.quartz.threadPool.threadPriority: 5
    org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: true
    org.quartz.jobStore.misfireThreshold: 60000
    org.quartz.jobStore.class: org.quartz.simpl.RAMJobStore
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2. MethodInvokingJobDetailFactoryBean的concurrent属性设值为false

    	<bean id="myJob"
    		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    		<property name="targetObject" ref="mySchedule">property>
    		
    		<property name="targetMethod" value="jobHandler">property>
    		
    		<property name="concurrent" value="false">property>
    	bean>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3. 多个待执行任务分别封装初始化时入队,执行时出队。
    例如这样:

    	public AdapterJob() {
    		super();
    		System.out.println("AdapterJob 初始化");
    		//初始化一个队列
    		queue = new LinkedBlockingDeque<Integer>();
    		for (int i = 0; i < 50; i++) {
    			queue.offer(i);
    		}
    		startTime = System.currentTimeMillis();
    	}
    
    	public void jobHandler() {
    		String name = Thread.currentThread().getName();
    		System.out.println(name);
    		int i = queue.poll();
    		System.out.println(i);
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    运行效果
    在这里插入图片描述

    总结
    以上3种措施一般使用一种就可以保证任务只执行一次,在一些可靠性要求高的定制任务执行场景可以考虑3中措施同时上,数据库的操作加并发锁。

  • 相关阅读:
    云贝教育 |【PostgreSQL PGCA】pg15安装pg_hint_plan扩展包
    JWT 详解,当前主流java token 框架之一
    Vue笔记_01双向数据绑定原理
    Neo4j图数据科学及2.0版本新功能介绍
    开发公众号怎么选择公司
    昇思25天学习打卡营第19天 | 基于MindSpore通过GPT实现情感分类
    elasticsearch命令大全
    Python高光谱遥感数据处理与机器学习实践技术
    【项目整理】安卓应用商店评论监控平台
    查找两个总和为特定值的索引
  • 原文地址:https://blog.csdn.net/lenovo96166/article/details/126563504