springboot2.6x集成quartz 2.3x问题

最近相继曝出log4j2等严重漏洞,还直接黑了我的服务器。痛定思痛,直接转用sl4j,将springboot版本升至2.6.1直接解决漏洞问题。然而带来的就是quartz服务的问题,老是找不到数据源,因为我之前用的springboot2.0x版本,相应的配置也发生了改变。

1:pom.xml升级


  
      org.springframework.boot
      spring-boot-starter-parent
      2.6.1
  

   	
        org.springframework.boot
        spring-boot-starter
    
    
    
        org.springframework.boot
        spring-boot-starter-web
    
     
        org.springframework.boot
        spring-boot-starter-aop
       
    
	
        org.springframework.boot
        spring-boot-starter-quartz
    

2:quratz.properties,这里最好重命名,如果还叫quratz.properties有可能会导致直接使用包内的quratz.properties。我这里重命名为了我项目的zbox-quratz.properties。

org.quartz.scheduler.instanceName = quartzScheduler
org.quartz.scheduler.instanceId = AUTO
org.quartz.scheduler.rmi.export = false
org.quartz.scheduler.rmi.proxy = false
org.quartz.scheduler.wrapJobExecutionInUserTransaction = false

#\u7EBF\u7A0B\u6C60\u914D\u7F6E
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 10
org.quartz.threadPool.threadPriority = 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread = true

#\u6301\u4E45\u5316\u914D\u7F6E
org.quartz.jobStore.misfireThreshold = 50000
org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
#\u652F\u6301\u96C6\u7FA4
org.quartz.jobStore.isClustered = true
org.quartz.jobStore.useProperties:true
org.quartz.jobStore.clusterCheckinInterval = 15000
#\u4F7F\u7528weblogic\u8FDE\u63A5Oracle\u9A71\u52A8
#org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.oracle.weblogic.WebLogicOracleDelegate
org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.dataSource = qzDS
#\u6570\u636E\u6E90\u8FDE\u63A5\u4FE1\u606F\uFF0Cquartz\u9ED8\u8BA4\u4F7F\u7528c3p0\u6570\u636E\u6E90\u53EF\u4EE5\u88AB\u81EA\u5B9A\u4E49\u6570\u636E\u6E90\u8986\u76D6
org.quartz.dataSource.qzDS.driver = com.mysql.jdbc.Driver
org.quartz.dataSource.qzDS.URL = jdbc:mysql://127.0.0.1:3306/zbox_cms?useUnicode=true&characterEncoding=utf8
org.quartz.dataSource.qzDS.user = root
org.quartz.dataSource.qzDS.password = root
org.quartz.dataSource.qzDS.maxConnections = 10

3:定义bean,这里在SchedulerConfig 内注入工厂类bean的时候添加properties

@Component
public class JobFactory extends AdaptableJobFactory {

	@Autowired
	private AutowireCapableBeanFactory capableBeanFactory;

	@Override
	protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
		// 调用父类的方法
		Object jobInstance = super.createJobInstance(bundle);
		// 进行注入
		capableBeanFactory.autowireBean(jobInstance);
		return jobInstance;
	}
}


@Configuration
public class SchedulerConfig implements ApplicationListener{

	@Autowired
    private JobFactory jobFactory;
    @Autowired
    @Qualifier("cmsBiosDataSource")
    private DataSource primaryDataSource;
    
    @Override
     public void onApplicationEvent(ContextRefreshedEvent event) {
        System.out.println("任务已经启动..."+event.getSource());
    }
    
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() throws IOException {        
       //获取配置属性
        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
        propertiesFactoryBean.setLocation(new ClassPathResource("/zbox-quartz.properties"));
        //在quartz.properties中的属性被读取并注入后再初始化对象
        propertiesFactoryBean.afterPropertiesSet();
        //创建SchedulerFactoryBean
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        factory.setQuartzProperties(propertiesFactoryBean.getObject());
        //使用数据源,自定义数据源
        factory.setDataSource(this.primaryDataSource);
        factory.setJobFactory(jobFactory);
        factory.setWaitForJobsToCompleteOnShutdown(true);//这样当spring关闭时,会等待所有已经启动的quartz job结束后spring才能完全shutdown。
        factory.setOverwriteExistingJobs(false); 
        factory.setStartupDelay(1);  
        return factory;
    }
    
    
    /*
     * 	通过SchedulerFactoryBean获取Scheduler的实例
     */
    @Bean(name="scheduler")
    public Scheduler scheduler() throws IOException {
        return schedulerFactoryBean().getScheduler();
    }
    
    
    @Bean
    public QuartzInitializerListener executorListener() {
       return new QuartzInitializerListener();
    }
}

4:遇到的问题

有可能循环注入,在springboot2.6.x开始,会对循环引用进行提示,就是不建议使用循环引用这里直接配置

spring:
  main:
    allow-circular-references: true

在spring配置,也就是application配置内的spring配置加上这个就行。

你可能感兴趣的:(java,spring,boot,java,spring,boot,quartz)