springBoot 定时器

程序入口类中加入注解

@EnableScheduling

配置定时任务为并行

@Slf4j
@Configuration
public class ScheduledConfig implements SchedulingConfigurer {

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.setScheduler(Executors.newScheduledThreadPool(10));
    }
}//end

编写定时器类

@Component
@Slf4j
public class timerController {
    private int fixedDelayCount = 1;
    private int fixedDelayCount1 = 1;

    @Scheduled(fixedDelay = 5000)        
    public void testFixDelay() throws InterruptedException {
        Thread.sleep(5000);
        log.info("===fixedDelay: 第{}次执行方法", fixedDelayCount++);
    }
    @Scheduled(fixedDelay = 3000)        
    public void testFixDelay1() {
        try {
            Thread.sleep(3000);
            log.info("===fixedDelay1: 第{}次执行方法", fixedDelayCount1++);
            if (fixedDelayCount1 == 3) {
                int i = 1 / 0;
            }
        } catch (Exception e) {
            log.error(e.toString());
        }
    }
}

如果定时器遇到异常,并不会使定时器断掉,抛出异常后会重新运行。

posted @ 2018-10-24 14:45  点点_滴滴  阅读(159)  评论(0编辑  收藏  举报