SpringBoot定時任務實現的兩種方式介紹

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

                

今天給你們介紹SpringBoot定時任務實現的幾種方式,但願對你們能有所幫助!java

一、SpringTask 用法

框架介紹:SpringTask是Spring自帶的輕量級定時任務工具,相比於Quartz使用更加簡單方便,而且不須要不須要引入其餘依賴便可使用。今天主要介紹註解的實現方式:程序員

SpringBoot啓動類配置 @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);
    }


}

建立測試類 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秒執行一次
@Scheduled(cron = "0/20 * * * * ?")
public void Test()
    {
        System.out.println("執行測試"+ DateUtil.now());
    }
}

而後啓動項目就能夠了,運行效果以下:spring

       watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=        

二、Quartz用法 介紹

添加依賴

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
</dependency>

添加測試類 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("測試類型:"+type+",你好,"+userName+",當前執行時間爲:"+ DateUtil.now());
    }
}

添加配置類 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", "小明") //設置參數(鍵值對)
                .storeDurably()
                .build();
return jobDetail;
    }
/**
     * 定時任務1:
     * Trigger觸發器使用
     */
    @Bean
    public Trigger testJobTrigger() {
//每隔5秒執行一次
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/15 * * * * ?");
//建立觸發器
        Trigger trigger = TriggerBuilder.newTrigger()
                .forJob(testJobDetail())//關聯上述的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;
    }


/**
     * 定時任務2:
     * Simple觸發器使用
     * */
    @Bean
    public SimpleTrigger testSimpleTrigger(){
        SimpleScheduleBuilder ssb = SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(10).repeatForever();
        SimpleTrigger sTrigger = TriggerBuilder.newTrigger()
                .forJob(testSampleJobDetail())//
                .withSchedule(ssb).build();
return sTrigger;
    }




}

運行效果

       watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=        

三、經常使用Cron表達式

「0 0 13,16,17 * * ?」 天天下午一、四、7點執行一次
「0 0 10 ? * WED」 表示每週三中午10點執行一次
「0 0 10 * * ?」 天天中午10點執行一次
「0 15 13 * * ?」 天天下午1:15執行一次
「0 15 10 * * ? " 天天上午10:15執行一次
「0 30 10 * * ? 2021」 2021年的天天上午10:30執行一次
「0 10 9 ? * MON-FRI」 週一至週五的上午9:10執行一次
「0 15 10 15 * ?」 每個月15日上午10:15執行一次
「0 15 10 L * ?」 每個月最後一日的上午10:15執行一次
「0 15 10 ? * 6L」 每個月的最後一個星期五上午10:15執行一次
"/5 * * * * ?」 每隔5秒執行一次
「0 */1 * * * ?」 每隔1分鐘執行一次
「0 0 23 * * ?」 天天23點執行一次
「0 0 1 * * ?」 天天凌晨1點執行一次
「0 0 1 1 * ?」 每個月1號凌晨1點執行一次
「0 0 23 L * ?」 每個月最後一天23點執行一次
「0 0 1 ? * L」 每週星期天凌晨1點實行一次
「0 26,29,33 * * * ?」 在26分、29分、33分執行一次
「0 0 0,13,18,21 * * ?」 天天的0點、13點、18點、21點都執行一次

 

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

文章推薦程序員效率:畫流程圖經常使用的工具程序員效率:整理經常使用的在線筆記軟件遠程辦公:經常使用的遠程協助軟件,你都知道嗎?51單片機程序下載、ISP及串口基礎知識硬件:斷路器、接觸器、繼電器基礎知識typescript

相關文章
相關標籤/搜索