zoukankan      html  css  js  c++  java
  • 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 很棒

  • 相关阅读:
    7.12-7.19 id、w、who、last、lastb、lastlog
    查询登录信息 w, who*, id, tty, last, finger
    [rhel-media] :Yum软件仓库唯一标识符,避免与其他仓库冲突。
    :整数 跳转到该行 Vim中常用的命令
    spec2006与spec2000的对比简要说明
    Linux性能监控与分析之--- CPU
    Android实现登录小demo
    python学习笔记——旧类与新类继承中的构造函数
    tair ldb存储引擎性能測试方案
    串口通讯方式1编程
  • 原文地址:https://www.cnblogs.com/412013cl/p/10945288.html
Copyright © 2011-2022 走看看