spring boot quartz配置

旧街凉风 提交于 2020-12-30 14:00:30
##分布式定时任务##
spring:
  quartz:
    properties:
      org:
        quartz:
          scheduler:
            instance-name: scheduleSchema
            instance-id: AUTO
          thread-pool:
            thread-count: 5
          job-store:
            class: org.quartz.impl.jdbcjobstore.JobStoreTX
            driver-delegate-class: org.quartz.impl.jdbcjobstore.StdJDBCDelegate
            use-properties: false
            table-prefix: QRTZ_
            is-clustered: true
    job-store-type: jdbc
    jdbc:
      initialize-schema: never
    overwrite-existing-jobs: true
    auto-startup: true
import org.quartz.spi.JobFactory;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;

/**
 * @author fendy
 */
@Configuration
public class SchedulerConfig {

    @Bean
    public JobFactory jobFactory(ApplicationContext applicationContext) {
        AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
        jobFactory.setApplicationContext(applicationContext);
        return jobFactory;
    }


    public static class AutowiringSpringBeanJobFactory
        extends SpringBeanJobFactory implements ApplicationContextAware {
        private transient AutowireCapableBeanFactory beanFactory;

        @Override
        public void setApplicationContext(final ApplicationContext applicationContext) {
            beanFactory = applicationContext.getAutowireCapableBeanFactory();
        }

        @Override
        protected Object createJobInstance(final TriggerFiredBundle bundle) throws Exception {
            final Object job = super.createJobInstance(bundle);
            beanFactory.autowireBean(job);
            return job;
        }
    }

}

 

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author fendy
 **/
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface SchedulerJob {
    String name();

    String group() default "DEFAULT_GROUP";

    String cronExp();
}

 

/**
 * @author fendy
 */
@Slf4j
@Component
public class JobListener implements BeanPostProcessor {

    @Autowired
    private Scheduler scheduler;

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        SchedulerJob schedulerJob = bean.getClass().getAnnotation(SchedulerJob.class);
        if(schedulerJob != null && bean instanceof Job) {
            JobKey jobKey = JobKey.jobKey(schedulerJob.name(), schedulerJob.group());
            JobDetail jobDetail = JobBuilder.newJob(((Job)bean).getClass()).withIdentity(jobKey).build();
            Trigger trigger = TriggerBuilder.newTrigger().forJob(jobDetail)
                .withIdentity(schedulerJob.name().concat("Trigger"), schedulerJob.group())
                .withSchedule(CronScheduleBuilder.cronSchedule(schedulerJob.cronExp())).build();
            try {
                if (!scheduler.checkExists(jobKey)) {
                    scheduler.scheduleJob(jobDetail, trigger);
                }
            } catch (SchedulerException e) {
                log.error("The job " + schedulerJob.name() + " happened error: ", e);
            }
        }

        return bean;
    }
}
/**
 * @author fendy
 */
@Slf4j
@Component
@SchedulerJob(name = "refreshMoodIcon", cronExp = "0 0 0/1 * * ?")
public class RefreshMoodIconsJob implements Job {

    @Autowired
    private DynamicMoodIconService dynamicMoodIconService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        List<DynamicMoodIcons> icons = dynamicMoodIconService.page();
        dynamicMoodIconService.loadIcons(icons);
        log.info("refreshMoodIcons: {}", Thread.currentThread().getName());
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!