SpringBoot 定时任务

1.简单的基于注解形式

package com.syyj.corp.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @Title: SaticScheduleTask
 * @ProjectName corp
 * @date 2019/5/2917:26
 */
@Slf4j
@Component
@Configuration
@EnableScheduling
public class SaticScheduleTask_A {
//    @Scheduled(cron = "0/5 * * * * ?") 5秒执行
    @Scheduled(fixedRate = 5000)
    private void configureTasks() {
        log.info("执行静态定时任务时间: " + LocalDateTime.now());
    }
}

2.基于接口实现

package com.syyj.corp.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * @Title: SaticScheduleTask
 * @ProjectName corp
 * @date 2019/5/2917:26
 */
@Slf4j
@Component
@Configuration
@EnableScheduling
public class SaticScheduleTask_B implements SchedulingConfigurer {

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.addTriggerTask(
                () ->
                        log.info("执行动态定时任务: " + LocalDateTime.now().toLocalTime()),
                triggerContext -> {
                    //2.1 从数据库获取执行周期
                    String cron = "0/5 * * * * ?";
                    //2.2 合法性校验.
                    if (StringUtils.isEmpty(cron)) {
                        // Omitted Code ..
                    }
                    //2.3 返回执行周期(Date)
                    return new CronTrigger(cron).nextExecutionTime(triggerContext);
                }
        );
    }
//    如果在数据库修改时格式出现错误,则定时任务会停止,即使重新修改正确;此时只能重新启动项目才能恢复。
}

3.多线程定时任务

package com.syyj.corp.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.*;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * @Title: SaticScheduleTask
 * @ProjectName corp
 * @date 2019/5/2917:26
 */
@Slf4j
@Component
@Configuration
@EnableScheduling
@EnableAsync
public class SaticScheduleTask_C {
    @Async
    @Scheduled(fixedDelay = 1000)  //间隔1秒
    public void first() throws InterruptedException {
        System.out.println("第一个定时任务开始 : " + LocalDateTime.now().toLocalTime() + "
线程 : " + Thread.currentThread().getName());
        System.out.println();
        Thread.sleep(1000 * 10);
    }

    @Async
    @Scheduled(fixedDelay = 2000)
    public void second() {
        System.out.println("第二个定时任务开始 : " + LocalDateTime.now().toLocalTime() + "
线程 : " + Thread.currentThread().getName());
        System.out.println();
    }
}

 参考: https://www.cnblogs.com/mmzs/p/10161936.html 很棒

原文地址:https://www.cnblogs.com/412013cl/p/10945288.html