springBoot 定时器任务

1、新建一个计划任务类(只能和主类平级或在主类的下级) 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

import java.text.SimpleDateFormat;

import java.util.Date;

 

 

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import  org.springframework.scheduling.annotation.Scheduled;

import  org.springframework.stereotype.Component;

 

 

@Component

public class ScheduledTasks {

    private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);

    private  static final SimpleDateFormat dataFromat = new SimpleDateFormat("HH:mm:ss");

 

    @Scheduled(fixedRate = 1000)

    public void reportCurrent(){

        logger.info("现在时间:{}",dataFromat.format(new Date()));

    }

 

}

 

本示例中使用的是 fixedRate函数,它指定的是从调用开始时间到指定时间之后,单位毫秒。还有 fixedDelay指定从

  完成任务测量的时间间隔。还可以指定具体时间,使用 Scheduled(cron="... ")

  cron参数说明: 0 0 10,14,16 * * ? 每天上午10点,下午2点,4点

  0 0/30 9-17 * * ?   朝九晚五工作时间内每半小时

  0 0 12 ? * WED 表示每个星期三中午12点 
  "0 0 12 * * ?" 每天中午12点触发

  其中 按顺序依次为:

    秒(0~59)

    分钟(0~59)

    小时(0~23)

    天(月)(0~31,但是你需要考虑你月的天数)

    月(0~11)

    天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)

    7.年份(1970-2099)

 

2、启用定时任务

 当上面一切被设置好之后,还需要在主类中加入 @EnableScheduling 注解来启动任务,否则定时任务不会被执行

1

2

3

4

5

6

7

8

9

10

11

12

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.scheduling.annotation.EnableScheduling;

 

 

 

@EnableScheduling //必须加此注解

public class DemoApplication  {

    public static void main(String[] args) {

        SpringApplication.run(DemoApplication.class, args);

    }

}

 3、启动,当一切编译完毕时,可以启动来测试了,在类中右键-->RunAs-->SpringBoot App,一切正常就会看到如下结果

 

 

 

 

转自:https://www.cnblogs.com/cyrfr/p/9165591.html

Spring Boot定时器是一种可以在指定时间间隔内执行任务的功能。它可以使用`@EnableScheduling`注解来开启定时器功能,并且通过`@Scheduled`注解来指定任务的执行时间。 在Spring Boot项目中,可以创建一个定时任务配置类,使用`@Configuration`和`@EnableScheduling`注解来标识该类是一个配置类,并开启定时功能。然后,可以在该类中定义具体的定时任务方法,使用`@Scheduled`注解来指定任务的执行时间。比如,在给定的示例代码中,`SchedulingConfig`类中的`getToken()`方法被标记为定时任务,使用`@Scheduled(cron = "*/5 * * * * ?")`来指定每5秒执行一次。 另外,可以在定时任务方法中调用其他业务逻辑方法。例如,在给定的示例代码中,`getToken()`方法中调用了`testService.test("刚刚")`方法来执行具体的业务逻辑。 总结来说,Spring Boot定时器通过使用`@EnableScheduling`和`@Scheduled`注解来开启和指定定时任务的执行时间,可以执行一些需要在固定时间间隔内运行的任务。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Spring boot实现定时器](https://blog.youkuaiyun.com/qq_43799161/article/details/123502124)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Springboot定时器](https://blog.youkuaiyun.com/xyy1028/article/details/80627155)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值