0
点赞
收藏
分享

微信扫一扫

TestTask implements SchedulingConfigure

import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.scheduling.support.PeriodicTrigger;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

import lombok.Data;


/**
 * 定时任务
 */
@Data
@Slf4j
@Component
@PropertySource("classpath:/task-config.ini")
public class TestTask implements SchedulingConfigurer {

    @Value("${testTime.cron}")
    private String cron;

    private Long timer = 10000L;

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        //  动态使用cron表达式设置循环间隔
        taskRegistrar.addTriggerTask(() -> log.info("处理业务逻辑time:{}", LocalDateTime.now()), triggerContext -> {
            //  使用CronTrigger触发器,可动态修改cron表达式来操作循环规则
            CronTrigger cronTrigger = new CronTrigger(cron);
            return cronTrigger.nextExecutionTime(triggerContext);


            //   使用不同的触发器,为设置循环时间的关键,区别于CronTrigger触发器,该触发器可随意设置循环间隔时间,单位为毫秒
            //                    PeriodicTrigger   periodicTrigger   =   new   PeriodicTrigger(timer);
            //                    Date   nextExecutionTime   =   periodicTrigger.nextExecutionTime(triggerContext);
            //                    return   nextExecutionTime;

        });
    }
}

src/main/resources/task-config.ini  :

testTime.cron=0 * 16 * * ?



举报

相关推荐

0 条评论