spring boot 整合quartz,并实现quartz的job实现自动注入功能

本篇废话不多,直接上代码,quartz和spring整合的其他相关知识可以看

spring4和quartz2.x整合,对任务做动态操作(增删改查)

spring4整合quartz2.x集群配置,并实现任务计数功能

quartz当机器挂掉重启后定时任务后执行的策略

使用的jar pom配置添加如下:quartz版本是2.x

<!-- quartz -->
		<dependency>
			<groupId>org.quartz-scheduler</groupId>
			<artifactId>quartz</artifactId>
			<version>2.2.3</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<!-- <version>4.1.2.RELEASE</version> -->
		</dependency>

quartz配置如下:红色标记的是一个难点,需要注意理解

package com.zyc.springboot.config;

import java.util.Properties;

import javax.sql.DataSource;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import com.zyc.springboot.quartz.MyJobFactory;

/**
 * quartz配置类
 * 
 * @author Administrator
 * 
 */
@Configuration
public class QuartzConfig {

	@Autowired
	Environment ev;

	@Autowired
	DataSource dataSource;

	@Bean
	public SchedulerFactoryBean schedulerFactoryBean(MyJobFactory myJobFactory) {
		SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();

		schedulerFactoryBean.setDataSource(dataSource);
		// 使job实例(本文中job实例是MyJobBean)支持spring 容器管理
		schedulerFactoryBean.setOverwriteExistingJobs(true);
		schedulerFactoryBean.setJobFactory(myJobFactory);
		// schedulerFactoryBean.setApplicationContextSchedulerContextKey("applicationContext");
		// org.springframework.core.io.Resource res=new
		// ClassPathResource("quartz.properties");
		// schedulerFactoryBean.setConfigLocation(res);
		schedulerFactoryBean.setQuartzProperties(quartzProperties());
		// 延迟60s启动quartz
		schedulerFactoryBean.setStartupDelay(60);

		return schedulerFactoryBean;
	}

	@Bean
	public Scheduler scheduler(SchedulerFactoryBean schedulerFactoryBean) {
		Scheduler scheduler = schedulerFactoryBean.getScheduler();
		try {
			scheduler.start();
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return scheduler;
	}

	public Properties quartzProperties() {
		Properties prop = new Properties();
		prop.put("quartz.scheduler.instanceName", "schedulerFactoryBean");
		prop.put("org.quartz.scheduler.instanceId", "AUTO");
		prop.put("org.quartz.scheduler.skipUpdateCheck", "true");
		// prop.put("org.quartz.scheduler.jobFactory.class",
		// "org.quartz.simpl.SimpleJobFactory");
		// JobStoreTX
		// prop.put("org.quartz.jobStore.class",
		// "org.quartz.impl.jdbcjobstore.JobStoreTX");
		prop.put("org.quartz.jobStore.class",
				"org.quartz.impl.jdbcjobstore.JobStoreCMT");
		prop.put("org.quartz.jobStore.driverDelegateClass",
				"org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
		prop.put("org.quartz.jobStore.dataSource", "quartzDataSource");
		prop.put("org.quartz.jobStore.tablePrefix", "QRTZ_");
		prop.put("org.quartz.jobStore.isClustered", "true");
		prop.put("org.quartz.threadPool.class",
				"org.quartz.simpl.SimpleThreadPool");
		prop.put("org.quartz.threadPool.threadCount", "5");

		prop.put("org.quartz.jobStore.maxMisfiresToHandleAtATime", "1");
		prop.put("org.quartz.jobStore.misfireThreshold", "50000");
		// org.quartz.jobStore.txIsolationLevelSerializable 如果为true 会出现无法连续事物的错误
		prop.put("org.quartz.jobStore.txIsolationLevelSerializable", "false");
		prop.put("org.quartz.jobStore.useProperties", "true");

		// prop.put("org.quartz.dataSource.quartzDataSource.driver",
		// "oracle.jdbc.driver.OracleDriver");
		// prop.put("org.quartz.dataSource.quartzDataSource.URL",
		// "jdbc:oracle:thin:@127。0.0.1/orcl");
		// prop.put("org.quartz.dataSource.quartzDataSource.user", "spcp");
		// prop.put("org.quartz.dataSource.quartzDataSource.password", "spcp");
		// prop.put("org.quartz.dataSource.quartzDataSource.maxConnections",
		// "10");

		prop.put("org.quartz.jobStore.dontSetAutoCommitFalse", "false");

		return prop;
	}

}

MyJobFactory实现如下:注意代码中红字标记的注释

package com.zyc.springboot.quartz;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;
import org.springframework.stereotype.Component;

import com.zyc.springboot.util.SpringContext;

/**
 * ClassName: MyJobFactory
 * 
 * @author zyc-admin
 * @date 2018年3月5日
 * @Description: TODO
 */
@Component("myJobFactory")
public class MyJobFactory extends AdaptableJobFactory  {

	@Autowired
	private AutowireCapableBeanFactory beanFactory;

	@Override
	protected Object createJobInstance(TriggerFiredBundle bundle)
			throws Exception {
		// TODO Auto-generated method stub
		Object jobInstance = super.createJobInstance(bundle);
		//AutowireCapableBeanFactory只是让jobInstance具备了自动配置属性的功能,并不是把jobInstance放入到了spring容器中
		beanFactory.autowireBean(jobInstance);
		return jobInstance;
	}
}

对quartz进行增删改查请看 :spring4和quartz2.x整合,对任务做动态操作(增删改查)

可以在MyJob2中直接使用@Autowired注入属性

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值