SpringBoot定時(shí)任務(wù)實(shí)現(xiàn)的兩種方式介紹
今天給大家介紹SpringBoot定時(shí)任務(wù)實(shí)現(xiàn)的幾種方式,希望對(duì)大家能有所幫助!
1、SpringTask 用法
框架介紹:SpringTask是Spring自帶的輕量級(jí)定時(shí)任務(wù)工具,相比于Quartz使用更加簡(jiǎn)單方便,并且不需要不需要引入其他依賴(lài)即可使用。今天主要介紹注解的實(shí)現(xiàn)方式:
SpringBoot啟動(dòng)類(lèi)配置 @EnableScheduling 注解
- package my.springboot.task;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.scheduling.annotation.EnableScheduling;
- @SpringBootApplication
- @EnableScheduling
- public class TaskApplication {
- public static void main(String[] args) {
- SpringApplication.run(TaskApplication.class, args);
- }
- }
創(chuàng)建測(cè)試類(lèi) TaskTest.java
- package my.springboot.task.controller;
- import cn.hutool.core.date.DateUtil;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Component;
- import java.util.Date;
- @Component
- public class TaskTest {
- //每隔20秒執(zhí)行一次
- @Scheduled(cron = "0/20 * * * * ?")
- public void Test()
- {
- System.out.println("執(zhí)行測(cè)試"+ DateUtil.now());
- }
- }
然后啟動(dòng)項(xiàng)目就可以了,運(yùn)行效果如下:
2、Quartz用法 介紹
添加依賴(lài)
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-quartz</artifactId>
- </dependency>
添加測(cè)試類(lèi) QuartzJobTest.java
- package my.springboot.mybatis.controller;
- import cn.hutool.core.date.DateUtil;
- import org.quartz.JobExecutionContext;
- import org.quartz.JobExecutionException;
- import org.springframework.scheduling.quartz.QuartzJobBean;
- public class QuartzJobTest extends QuartzJobBean {
- @Override
- protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
- String userName = (String) jobExecutionContext.getJobDetail().getJobDataMap().get("userName");
- String type = (String) jobExecutionContext.getJobDetail().getJobDataMap().get("type");
- System.out.println("測(cè)試類(lèi)型:"+type+",你好,"+userName+",當(dāng)前執(zhí)行時(shí)間為:"+ DateUtil.now());
- }
- }
添加配置類(lèi) QuartzConfig.java
- package my.springboot.mybatis.common;
- import my.springboot.mybatis.controller.QuartzJobTest;
- import my.springboot.mybatis.controller.TaskTest;
- import org.quartz.*;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- @Configuration
- public class QuartzConfig {
- @Bean
- public JobDetail testJobDetail() {
- JobDetail jobDetail= JobBuilder.newJob(QuartzJobTest.class)
- .usingJobData("type","Trigger")
- .usingJobData("userName", "小明") //設(shè)置參數(shù)(鍵值對(duì))
- .storeDurably()
- .build();
- return jobDetail;
- }
- /**
- * 定時(shí)任務(wù)1:
- * Trigger觸發(fā)器使用
- */
- @Bean
- public Trigger testJobTrigger() {
- //每隔5秒執(zhí)行一次
- CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/15 * * * * ?");
- //創(chuàng)建觸發(fā)器
- Trigger trigger = TriggerBuilder.newTrigger()
- .forJob(testJobDetail())//關(guān)聯(lián)上述的JobDetail
- .withSchedule(cronScheduleBuilder)
- .build();
- return trigger;
- }
- @Bean
- public JobDetail testSampleJobDetail() {
- JobDetail jobDetail= JobBuilder.newJob(QuartzJobTest.class)
- .usingJobData("type","SimpleTrigger")
- .usingJobData("userName", "小王") .storeDurably()
- .build();
- return jobDetail;
- }
- /**
- * 定時(shí)任務(wù)2:
- * Simple觸發(fā)器使用
- * */
- @Bean
- public SimpleTrigger testSimpleTrigger(){
- SimpleScheduleBuilder ssb = SimpleScheduleBuilder.simpleSchedule()
- .withIntervalInSeconds(10).repeatForever();
- SimpleTrigger sTrigger = TriggerBuilder.newTrigger()
- .forJob(testSampleJobDetail())//
- .withSchedule(ssb).build();
- return sTrigger;
- }
- }
運(yùn)行效果
3、常用Cron表達(dá)式
- “0 0 13,16,17 * * ?” 每天下午1、4、7點(diǎn)執(zhí)行一次
- “0 0 10 ? * WED” 表示每周三中午10點(diǎn)執(zhí)行一次
- “0 0 10 * * ?” 每天中午10點(diǎn)執(zhí)行一次
- “0 15 13 * * ?” 每天下午1:15執(zhí)行一次
- “0 15 10 * * ? " 每天上午10:15執(zhí)行一次
- “0 30 10 * * ? 2021” 2021年的每天上午10:30執(zhí)行一次
- “0 10 9 ? * MON-FRI” 周一至周五的上午9:10執(zhí)行一次
- “0 15 10 15 * ?” 每月15日上午10:15執(zhí)行一次
- “0 15 10 L * ?” 每月最后一日的上午10:15執(zhí)行一次
- “0 15 10 ? * 6L” 每月的最后一個(gè)星期五上午10:15執(zhí)行一次
- "/5 * * * * ?” 每隔5秒執(zhí)行一次
- “0 */1 * * * ?” 每隔1分鐘執(zhí)行一次
- “0 0 23 * * ?” 每天23點(diǎn)執(zhí)行一次
- “0 0 1 * * ?” 每天凌晨1點(diǎn)執(zhí)行一次
- “0 0 1 1 * ?” 每月1號(hào)凌晨1點(diǎn)執(zhí)行一次
- “0 0 23 L * ?” 每月最后一天23點(diǎn)執(zhí)行一次
- “0 0 1 ? * L” 每周星期天凌晨1點(diǎn)實(shí)行一次
- “0 26,29,33 * * * ?” 在26分、29分、33分執(zhí)行一次
- “0 0 0,13,18,21 * * ?” 每天的0點(diǎn)、13點(diǎn)、18點(diǎn)、21點(diǎn)都執(zhí)行一次
本文轉(zhuǎn)載自微信公眾號(hào)「IT技術(shù)分享社區(qū)」,可以通過(guò)以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系IT技術(shù)分享社區(qū)公眾號(hào)。
個(gè)人博客網(wǎng)站:https://programmerblog.xyz