美文网首页
SpringBoot关于设置定时器quartz

SpringBoot关于设置定时器quartz

作者: lew1sss | 来源:发表于2019-08-06 17:26 被阅读0次

    pom.xml

    <!-- 定时器 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-quartz</artifactId>
            </dependency>
    

    设置配置类QuartzConfiguration

    @Configuration
    public class QuartzConfiguration {
        //  一分钟的间隔
        private static final int interval = 1;
    
        @Bean
        public JobDetail weatherDataSyncJobDetail() {
            return JobBuilder.newJob(IndexDataSyncJob.class).withIdentity("indexDataSyncJob")
                    .storeDurably().build();
        }
    
        @Bean
        public Trigger weatherDataSyncTrigger() {
            SimpleScheduleBuilder schedBuilder = SimpleScheduleBuilder.simpleSchedule()
                    .withIntervalInMinutes(interval).repeatForever();
    
            return TriggerBuilder.newTrigger().forJob(weatherDataSyncJobDetail())
                    .withIdentity("indexDataSyncTrigger").withSchedule(schedBuilder).build();
        }
    }
    

    添加定时任务IndexDataSyncJob

    public class IndexDataSyncJob extends QuartzJobBean {
    
        @Autowired
        private IndexService indexService;
    
        @Autowired
        private IndexDataService indexDataService;
    
        @Override
        protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
            System.out.println("定时启动:" + DateUtil.now());
            List<Index> indexes = indexService.fresh();
            for (Index index : indexes) {
                indexDataService.fresh(index.getCode());
            }
            System.out.println("定时结束:" + DateUtil.now());
    
        }
    
    }
    

    相关文章

      网友评论

          本文标题:SpringBoot关于设置定时器quartz

          本文链接:https://www.haomeiwen.com/subject/fumwdctx.html