SpringBoot整合quartz(自启动 job -- 数据库可配置版本)

首先导入配置类(项目启动预加载)

  • 背景:项目启动后,SpringBoot 通过数据库配置实现可配置job任务工作,目前的功能算个半自动,更改数据库配置信息的话 就需要重新重启服务了。

      SpringBoot整合quartz(支持多任务和job,支持spring管理)
      框架:SpringBoot + quartz
      
      所需要的依赖:
         <!--quartz-->
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-quartz</artifactId>
          </dependency>
    
  • 1.导入配置类
package xxx;

import xxx.entity.ScheduleJob;
import xxx.repository.ScheduleJobRepository;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import java.util.List;

@Configuration
public class SchedulerListener implements ApplicationListener<ContextRefreshedEvent> {
   
   

    @Autowired
    Scheduler Scheduler;

    @Autowired
    JobFactory JobFactory;

    @Autowired
    ScheduleJobRepository ScheduleJobRepository;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
   
   
    	//数据库查询配置信息  换你自己的逻辑
        List<ScheduleJob> ScheduleJobAll = ScheduleJobRepository.findAll(); 
        try {
   
   
            Scheduler.schedulerJob(ScheduleJobAll);
            System.out.println("SynchronizedData job  start...");
        } catch (SchedulerException | ClassNotFoundException e) {
   
   
            e.printStackTrace();
        }
    }

    @Bean(name ="mySchedulerFactoryBean")
    public SchedulerFactoryBean mySchedulerFactory() {
   
   
        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        bean.setOverwriteExistingJobs(true);
        bean.setStartupDelay(1);
        bean.setJobFactory(JobFactory);
        return bean;
    }
}

  • 2.导入应用程序启动时自动扫描并加载的组件1
package xxx.conf.quartz;

import xxx.entity.ScheduleJob;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.quartz.impl.StdScheduler;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class Scheduler {
   
   

    public static JobKey getJobKey(String jobId, String jobGroup) {
   
   
        return JobKey.jobKey(jobId, jobGroup);
    }

    public void schedulerJob(List<ScheduleJob> scheduleJobList) throws SchedulerException, ClassNotFoundException {
   
   
        ApplicationContext annotationContext = SpringUtil.getApplicationContext();
        StdScheduler stdScheduler = (StdScheduler) annotationContext.getBean("mySchedulerFactoryBean");//获得上面创建的bean
        org.quartz.Scheduler myScheduler 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值