SpringBoot整合Quartz并持久化任务

前言:在开发过程中我们会遇到一些定时任务,对于这些任务我们需要对其进行管理,此时使用 Spring自带的定时任务无法满足我们的需求,我们可以使用Quartz定时任务框架。多的不说了,直接上代码

SpringBoot整合Quartz:

1、pom文件中添加依赖

2、添加配置文件(.properties或者.yml)

pom.xml文件

<!-- quartz开始 -->
<dependency>
	<groupId>org.quartz-scheduler</groupId>
	<artifactId>quartz</artifactId>
	<version>2.2.3</version>
	<exclusions>  
		<exclusion>  
			<artifactId>slf4j-api</artifactId>  
			<groupId>org.slf4j</groupId>  
		</exclusion>  
	</exclusions>  
</dependency>
<dependency>
	<groupId>org.quartz-scheduler</groupId>
	<artifactId>quartz-jobs</artifactId>
	<version>2.2.3</version>
</dependency>
<!-- 该依赖必加,里面有sping对schedule的支持 --> 
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
</dependency>
<!-- 结束 -->

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 = 10
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true


#将定时任务的信息保存在内存中的,当项目重启后将丢失任务信息
#org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore 
#使用数据库存储定时任务信息,当项目重启后仍保留定时任务信息 
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.StdJDBCDelegate  
org.quartz.jobStore.useProperties = false
org.quartz.jobStore.tablePrefix = QRTZ_  
org.quartz.jobStore.isClustered = false  
# Configure Datasources 
#============================================================================ 
org.quartz.jobStore.dataSource = myDS
org.quartz.dataSource.myDS.driver = com.mysql.jdbc.Driver 
org.quartz.dataSource.myDS.URL = jdbc:mysql://XXXX:XXXX/mall?useUnicode=true&characterEncoding=utf-8
org.quartz.dataSource.myDS.user = root 
org.quartz.dataSource.myDS.password =root
org.quartz.dataSource.myDS.maxConnections = 10 
org.quartz.dataSource.myDS.validateOnCheckout = true
org.quartz.dataSource.myDS.validationQuery = SELECT 'x'

加载核心类:

package com.macro.mall.config;

import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import com.macro.mall.quartz.MyFactory;

/**
 * quartz配置信息
 * @author YJX
 */
@Configuration
public class QuartzConfiguration {
	 @Autowired
	 private MyFactory myFactory;
	 
	 @Bean
	 public SchedulerFactoryBean schedulerFactoryBean() {
		 SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
		 schedulerFactoryBean.setJobFactory(myFactory);
		 //延长启动
	     schedulerFactoryBean.setStartupDelay(1);
		 //设置加载的配置文件
	     schedulerFactoryBean.setConfigLocation(new ClassPathResource("/quartz.properties"));
		 return schedulerFactoryBean;
	 }
	 
	 @Bean
     public Scheduler scheduler() {
        return schedulerFactoryBean().getScheduler();
     }
}
package com.macro.mall.quartz;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;
import org.springframework.stereotype.Component;

/**
 * 定时任务 中的job中注入的service接口为空解决方法
 * @author YJX
 *
 */
@Component
public class MyFactory extends SpringBeanJobFactory implements ApplicationContextAware{
	private ApplicationContext context;
	
	@Override
	public void setApplicationContext(ApplicationContext context) throws BeansException {
		this.context = context;
	}

	@Override
	protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
		Object jobInstance = super.createJobInstance(bundle);
		
		//把Job交给Spring来管理,这样Job就能使用由Spring产生的Bean了
		context.getAutowireCapableBeanFactory().autowireBean(jobInstance);
		 
		return jobInstance;
	}

	
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值