Springboot2.5.3 集成quartZ

  1. 
        org.springframework.boot
        spring-boot-starter-quartz
    
  2. spring:
      quartz:
        #相关属性配置
        properties:
          org:
            quartz:
              scheduler:
                instanceName: jobScheduler
                instanceId: AUTO
             # jobStore:
              #  class: org.quartz.impl.jdbcjobstore.JobStoreTX
               # driverDelegateClass: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
                #tablePrefix: QRTZ_
               # isClustered: true
              #  clusterCheckinInterval: 10000
               # useProperties: false
              threadPool:
                class: org.quartz.simpl.SimpleThreadPool
                threadCount: 50
                threadPriority: 10
                threadsInheritContextClassLoaderOfInitializingThread: true
        #数据库方式
       # job-store-type: jdbc
        #初始化表结构
        #jdbc:
          #initialize-schema: never
  3. @Autowired
    private Scheduler scheduler;
  4. ​
    private void schedulerJob(QuartzJob job) throws Exception {
        //构建job信息
        Class cls = Class.forName(job.getJobClassName()) ;
        // cls.newInstance(); // 检验类是否存在
        JobDetail jobDetail =                   JobBuilder.newJob(cls).withIdentity(job.getJobName(),job.getJobGroup())
                .withDescription(job.getDescription()).build();
    
        // 触发时间点
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression().trim());
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity(TRIGGER_IDENTITY + job.getJobName(), job.getJobGroup())
                .startNow().withSchedule(cronScheduleBuilder).build();
        //交由Scheduler安排触发
        scheduler.scheduleJob(jobDetail, trigger);
    }
    
    ​

  5. 此处可实现任务内装载spring容器bean 重点extends QuartzJobBean

    public class SyncDevicesJob extends QuartzJobBean {
    
        @Autowired
        private RestClientUtils sync;

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