在各自岗位上尽职尽责,无需豪言壮语,默默行动会诠释一切。这世界,虽然没有绝对的公平,但是努力就会增加成功和变好的可能性!而这带着未知变量的可能性,就足以让我们普通人拼命去争取了。
欢迎来到~一支会记忆的笔~博客主页

基于 @Scheduled 注解的 ----定时任务

最常用的方法
@Scheduled 注解表示起开定时任务

依赖
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

 

在启动类上添加 这个注解即可自动开启任务    

@EnableScheduling//开启定时任务

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling//开启定时任务
public class ScheduledApplication {

    public static void main(String[] args) {
        SpringApplication.run(ScheduledApplication.class, args);
    }

}

 

任务类

 

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class HelloComponent {
    /**
     * @Scheduled 注解表示起开定时任务
     *
     * fixedDelay  属性表示下一个任务在本次任务执行结束 2000 ms 后开始
     * fixedRate 表示下一个任务在本次任务开始 2000ms 之后开始
     * initialDelay 表示启动延迟时间
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public void hello() {
        System.out.println("hello:"+new Date());
    }
}

 

可以 动态的添加 schedul 任务   

在任务类的方法上 添加
@Scheduled(cron = "${jobs.schedule}")

在配置文件 application.properties文件中 中添加
jobs.schedule = 0 0/45 * * * ?



posted @ 2019-07-26 15:32  一支会记忆的笔  阅读(436)  评论(0编辑  收藏  举报
返回顶部
【学无止境❤️谦卑而行】