1. 程式人生 > >SpringBoot多工Quartz動態管理Scheduler,時間配置,頁面+原始碼

SpringBoot多工Quartz動態管理Scheduler,時間配置,頁面+原始碼

  • 頁面展現

  • 後臺任務處理:恢復任務 15s執行一次

    後臺列印訊息

  • 不BB了,直接上程式碼
    import...
    
    /**
     * 排程工廠類
     * Created by jinyu on 2018/4/14/014.
     */
    @Service
    @Component
    public class MySchedulerFactory {
        private static Logger logger = LoggerFactory.getLogger(MySchedulerFactory.class);
        @Autowired
        SchedulerFactoryBean schedulerFactoryBean;
    
        // 任務配置讀取服務
        @Autowired
        private AsyncQuartzConfService asyncQuartzConfService;
    
        public void scheduleJobs() throws SchedulerException {
            Scheduler scheduler = getScheduler();
            startJob(scheduler);
        }
    
        // 獲取scheduler
        private Scheduler getScheduler(){
           return schedulerFactoryBean.getScheduler();
        }
        // 專案啟動 開啟任務
        private void startJob(Scheduler scheduler)  {
            try {
                List<Quartz_Config> jobList = asyncQuartzConfService.getJobList();
                for (Quartz_Config config : jobList) {
                    // 1-暫停的任務 0-正常執行任務
                    if (1l==config.getStatus()){
                        continue;
                    }
                    @SuppressWarnings("unchecked")
                    Class<? extends Job> clazz = (Class<? extends Job>) Class.forName(config.getQuartz_Class());
                    JobDetail jobDetail = JobBuilder.newJob(clazz)
                            .withIdentity(config.getName(), config.getGroup()).build();
                    CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(config.getCron());
                    CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(config.getName(), config.getGroup())
                            .withSchedule(scheduleBuilder).build();
                    scheduler.scheduleJob(jobDetail, cronTrigger);
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }catch (SchedulerException e) {
                e.printStackTrace();
            }
        }
    
        // 任務暫停
        public void pauseJob(long id) throws SchedulerException {
            Scheduler scheduler = getScheduler();
            Quartz_Config quartz_config = asyncQuartzConfService.findById(id);
            JobKey jobKey = JobKey.jobKey(quartz_config.getName(), quartz_config.getGroup());
            scheduler.deleteJob(jobKey);
        }
    
        // 任務恢復
        public void resumeJob(long id) throws SchedulerException, ClassNotFoundException {
            Scheduler scheduler = getScheduler();
            Quartz_Config quartz_config = asyncQuartzConfService.findById(id);
            JobKey jobKey = JobKey.jobKey(quartz_config.getName(), quartz_config.getGroup());
            Class<? extends Job> clazz = (Class<? extends Job>) Class.forName(quartz_config.getQuartz_Class());
            JobDetail jobDetail1 = scheduler.getJobDetail(jobKey);
            if (jobDetail1==null){
                JobDetail jobDetail = JobBuilder.newJob(clazz)
                        .withIdentity(quartz_config.getName(), quartz_config.getGroup()).build();
                CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(quartz_config.getCron());
                CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(quartz_config.getName(), quartz_config.getGroup())
                        .withSchedule(scheduleBuilder).build();
                scheduler.scheduleJob(jobDetail, cronTrigger);
            }else {
                scheduler.resumeJob(jobKey);
            }
        }
    }
  • 啟動Springboot監聽
    import ...
    
    /**
     * 定時任務執行工廠類
     * Created by jinyu on 2018/4/12/012.
     */
    @Configuration
    public class StartSchedulerListener implements ApplicationListener<ContextRefreshedEvent> {
        @Autowired
        public MySchedulerFactory mySchedulerFactory;
    
        // springboot 啟動監聽
        @Override
        public void onApplicationEvent(ContextRefreshedEvent event) {
            try {
                mySchedulerFactory.scheduleJobs();
            } catch (SchedulerException e) {
                e.printStackTrace();
            }
        }
    
        //注入SchedulerFactoryBean
        @Bean
        public SchedulerFactoryBean schedulerFactoryBean() {
            SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
            return schedulerFactoryBean;
        }
    }

  • 任務類,按需建立
    /**
     * 定時任務實現類
     * Created by jinyu on 2018/4/12/012.
     */
    @Configuration
    @Component
    @EnableScheduling
    public class ScheduleTask1 implements Job {
    
        private static Logger logger = LoggerFactory.getLogger(ScheduleTask1.class);
    
        @Override
        public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
            try {
                System.out.print("任務執行1 :");
                System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    
            } catch (Exception e) {
                logger.error(e.getMessage());
            }
        }
  • 資料庫

補充:原始碼地址