欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot多任务Quartz动态管理Scheduler,时间配置,页面+源码

程序员文章站 2024-03-22 13:56:16
...
  • 页面展现
    SpringBoot多任务Quartz动态管理Scheduler,时间配置,页面+源码
  • 后台任务处理:恢复任务 15s执行一次
    SpringBoot多任务Quartz动态管理Scheduler,时间配置,页面+源码
    后台打印消息
    SpringBoot多任务Quartz动态管理Scheduler,时间配置,页面+源码
  • 不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());
            }
        }
  • 数据库

补充:源码地址