springboot定时器

2025-10-16 22:22:34

1、第一种:使用IDEA创建springboot项目。

具体操作参考引用

springboot定时器

springboot定时器

springboot定时器

springboot定时器

springboot定时器

2、第二种:使用eclipse创建springboot项目。

具体操作参考引用

1、第一步:代码实现。

1、首先在启动类ExcelimportApplication添加注解

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling

@SpringBootApplication

public class ExcelimportApplication {

public static void main(String[] args) {

SpringApplication.run(ExcelimportApplication.class, args);

}

}

2、定时任务类

import org.springframework.stereotype.Component;

import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;

import java.time.format.DateTimeFormatter;

/**

 * 打卡定时任务

 */

@Component

public class ClockScheduled {

/**

* 定时器

*/

@Scheduled(cron = "0 0/1 * * * ?")

// 一分钟调用一次

public void insertClock() {

// 业务逻辑,每天早上8:45调用

LocalDateTime localDateTime = LocalDateTime.now();

System.out.println("ClockScheduled.insertClock:"

+ localDateTime.format(DateTimeFormatter

.ofPattern("yyyy-MM-dd HH:mm:ss")));

}

}

springboot定时器

springboot定时器

2、第二步:测试功能。

1、启动springboot主类:ExcelimportApplication

2、查看运行日志结果,运行成功。

springboot定时器

1、第一步:代码实现。

1、首先在启动类ExcelimportApplication添加注解

2、定时任务实现类

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.stereotype.Component;

import java.util.Date;

@Component

public class CclockScheduledTask implements SchedulingConfigurer {

@Override

public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {

scheduledTaskRegistrar.addTriggerTask(new Runnable() {

@Override

public void run() {

// 定时任务的业务逻辑

System.out.println("提醒打卡");

}

}, new Trigger() {

@Override

public Date nextExecutionTime(TriggerContext triggerContext) {

String cron = "0 0/1 * * * ?";

System.out.println(cron);

CronTrigger trigger = new CronTrigger(cron); // 定时任务触发,可修改定时任务的执行周期

Date nextExecDate = trigger.nextExecutionTime(triggerContext);

return nextExecDate;

}

});

}

}

2、第二步:测试功能。

1、启动springboot主类:ExcelimportApplication

2、查看运行日志结果,运行成功。

springboot定时器

3、第三步:总结。

1、动态定时任务的特点是,首先根据nextExecutionTime方法中设置的定时参数cron运行一次,下一次可以再根据业务从新设置新的任务规则。它是动态的可以根据具体的业务动态调整。

2、动态定时任务仍然需要借助启动类的@EnableScheduling注解

3、定时任务的参数cron与注解的使用方式一致

1、第一种:fixedRate属性。

1、如下单位毫秒,30秒调用一次

@Scheduled(fixedRate=30000)

public void testTasks() {    

logger.info("每30秒执行一次);

2、第二步:cron属性知识点。

1、cron总共7个位数分别是 秒 ,分,小时 ,日期,月份,星期,年(可选)

2、常用表达式,主要可分为,按固定时间点触发,按固定时间间隔循环触发,按固定时间点或区间间隔一定时间循环触发。

"0 0 12 * * ?" 每天中午12点触发

"0 15 10 ? * *" 每天上午10:15触发

"0 15 10 * * ?" 每天上午10:15触发

"0 15 10 * * ? *" 每天上午10:15触发

"0 15 10 * * ? 2005" 2005年的每天上午10:15  触发

"0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发

"0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发

"0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每 5分钟触发

"0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发

"0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发

"0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发

"0 15 10 15 * ?" 每月15日上午10:15触发

"0 15 10 L * ?" 每月最后一日的上午10:15触发

"0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发

"0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午 10:15触发

"0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发

0 6 * * * 每天早上6点 0 */2 * * * 每两个小时 0 23-7/2,8 * * * 晚上11点到早上8点之间每两个小时,早上八点 0 11 4 * 1-3 每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点 0 4 1 1 *     1月1日早上4点 

3、特殊字符如下图所示:

4、cron的值设置可以使用这个在线工具获得:http://cron.qqe2.com/

springboot定时器

springboot定时器

springboot定时器

3、第三步:自定义类替代springboot启动类

1、安照如下方式可以替代springboot启动类加@EnableScheduling注解的方式。

import org.springframework.context.annotation.Configuration;

import org.springframework.scheduling.annotation.EnableScheduling;

@Configuration

// 证明这个类是一个配置文件

@EnableScheduling

// 打开quartz定时器总开关

public class TestTimer {

}

springboot定时器

4、第四步:应用场景。

1、如考情打开软件 “钉钉”在打卡前15分钟左右会提醒

2、每天定时检查客户生日是否是今天,如果是则推荐祝福短信或邮寄。

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢