SpringBoot下定時任務


參考江南一點雨大佬的文章:

https://www.cnblogs.com/lenve/p/10728897.html

cron工具網站:

  https://tool.lu/crontab/

  https://www.bejson.com/othertools/cron/

寫在前面:Linux下的Cron跟Spring下的Cron表達式有些許的不一樣,注意到網站上去驗證

一、SpringBoot中實現定時任務的兩種方式

  在 Spring + SpringMVC 環境中,一般來說,要實現定時任務,我們有兩中方案,一種是使用 Spring 自帶的定時任務處理器 @Scheduled 注解,另一種就是使用第三方框架 Quartz 。

Spring Boot 源自 Spring+SpringMVC ,因此天然具備這兩個 Spring 中的定時任務實現策略,當然也支持 Quartz,本文我們就來看下 Spring Boot 中兩種定時任務的實現方式;

一、@Scheduled

使用 @Scheduled 非常容易,直接創建一個 Spring Boot 項目,並且添加 web 依賴 spring-boot-starter-web,項目創建成功后,添加 @EnableScheduling 注解

1、開啟定時任務:

@EnableScheduling
@SpringBootApplication
public class Springboot19ScheduledApplication {

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

2、配置定時任務:具體的表達式請參考上面的網址自行驗證

cron表達式示例:

  • second(秒), minute(分), hour(時), day of month(日), month(月), day of week(周幾).
  • 【0 0/5 14,18 * * ?】 每天14點整,和18點整,每隔5分鍾執行一次
  • 【0 15 10 ? * 1-6】 每個月的周一至周六10:15分執行一次
  • 【0 0 2 ? * 6L】每個月的最后一個周六凌晨2點執行一次
  • 【0 0 2 LW * ?】每個月的最后一個工作日凌晨2點執行一次
  • 【0 0 2-4 ? * 1#1】每個月的第一個周一凌晨2點到4點期間,每個整點都執行一次;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Date;

/**
 * @ClassName ScheduledTaskConfig
 * @Author zhangzhixi
 * @Description 定時任務配置類
 * @Date 2022-2-27 19:20
 * @Version 1.0
 */
@Configuration
public class ScheduledTaskConfig {
    @Scheduled(fixedRate = 2000)
    public void fixedRate() {
        System.out.println("fixedRate>>>" + new Date());
    }

    @Scheduled(fixedDelay = 2000)
    public void fixedDelay() {
        System.out.println("fixedDelay>>>" + new Date());
    }

    @Scheduled(initialDelay = 2000, fixedDelay = 2000)
    public void initialDelay() {
        System.out.println("initialDelay>>>" + new Date());
    }

    /**
     * 每5秒執行一次
     */
    @Scheduled(cron = "0/5 * * * * *")
    public void cron() {
        System.out.println("cronExpression>>>" + new Date());
    }
}
  1. 首先使用 @Scheduled 注解開啟一個定時任務。
  2. fixedRate 表示任務執行之間的時間間隔,具體是指兩次任務的開始時間間隔,即第二次任務開始時,第一次任務可能還沒結束。
  3. fixedDelay 表示任務執行之間的時間間隔,具體是指本次任務結束到下次任務開始之間的時間間隔。
  4. initialDelay 表示首次任務啟動的延遲時間。
  5. 所有時間的單位都是毫秒。

二、Quatz

一般在項目中,除非定時任務涉及到的業務實在是太簡單,使用 @Scheduled 注解來解決定時任務,否則大部分情況可能都是使用 Quartz 來做定時任務。在 Spring Boot 中使用 Quartz ,只需要在創建項目時,添加 Quartz 依賴即可:

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

1:開啟定時任務的注解

@EnableScheduling
@SpringBootApplication
public class Springboot18QuartzApplication {
    public static void main(String[] args) {
        SpringApplication.run(Springboot18QuartzApplication.class, args);
    }
}

Quartz 在使用過程中,有兩個關鍵概念,一個是JobDetail(要做的事情),另一個是觸發器(什么時候做),要定義 JobDetail,需要先定義 Job,Job 的定義有兩種方式:  

2.1:Job的第一種定義方式:直接定義bean

/**
 * @ClassName MyJobOne
 * @Author zhangzhixi
 * @Description
 * @Date 2022-2-27 16:58
 * @Version 1.0
 */
@Component
public class MyJobOne {
    public void sayHello() {
        System.out.println(MyJobOne.class + "========>" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }
}

關於這種定義方式說兩點:

  1. 首先將這個 Job 注冊到 Spring 容器中。
  2. 這種定義方式有一個缺陷,就是無法傳參。

2.2:Job的第二種定義方式:繼承 QuartzJobBean 並實現默認的方法

/**
 * @ClassName MyJonTwo
 * @Author zhangzhixi
 * @Description
 * @Date 2022-2-27 17:01
 * @Version 1.0
 */
@Component
public class MyJonTwo extends QuartzJobBean {

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) {
        new HelloService().sayHello();
    }
}

HelloService:

/**
 * @ClassName HelloService
 * @Author zhangzhixi
 * @Description
 * @Date 2022-2-27 17:07
 * @Version 1.0
 */
public class HelloService {
    public void sayHello() {
        System.out.println(HelloService.class + "========>" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }
}

和第1種方式相比,這種方式支持傳參,任務啟動時,executeInternal 方法將會被執行。

Job 有了之后,接下來創建類,配置 JobDetail 和 Trigger 觸發器,如下:

3:配置觸發器

/**
 * @ClassName QuartzConfig
 * @Author zhangzhixi
 * @Description
 * @Date 2022-2-27 17:05
 * @Version 1.0
 */
@Configuration
public class QuartzConfig {
    /**
     * JobDetail的配置1:
     *      使用 MethodInvokingJobDetailFactoryBean 可以配置目標 Bean 的名字和目標方法的名字,這種方式不支持傳參。
     * @return
     */
    @Bean
    MethodInvokingJobDetailFactoryBean methodInvokingJobDetailFactoryBean() {
        MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean();
        bean.setTargetBeanName("myJobOne");
        bean.setTargetMethod("sayHello");
        return bean;
    }

    /**
     * JobDetail的配置2:
     *      使用 JobDetailFactoryBean 可以配置 JobDetail ,任務類繼承自 QuartzJobBean ,這種方式支持傳參,將參數封裝在 JobDataMap 中進行傳遞。
     * @return
     */
    @Bean
    JobDetailFactoryBean jobDetailFactoryBean() {
        JobDetailFactoryBean bean = new JobDetailFactoryBean();
        bean.setJobClass(MyJonTwo.class);

        JobDataMap map = new JobDataMap();
        map.put("helloService", helloService());

        bean.setJobDataMap(map);
        return bean;
    }


    /**
     * 觸發器:simpleTrigger
     * @return
     */
    @Bean
    SimpleTriggerFactoryBean simpleTriggerFactoryBean() {
        SimpleTriggerFactoryBean bean = new SimpleTriggerFactoryBean();
        /*1.設置觸發器開始的時間*/
        bean.setStartTime(new Date());
        /*2.設置觸發器執行的次數*/
        bean.setRepeatCount(5);
        /*3.設置執行間隔(單位:ms)*/
        bean.setRepeatInterval(3000);
        /*4.添加MethodInvokingJobDetailFactoryBean觸發器的設置*/
        bean.setJobDetail(Objects.requireNonNull(methodInvokingJobDetailFactoryBean().getObject()));
        return bean;
    }

    /**
     * 觸發器:cronTrigger
     * @return
     */
    @Bean
    CronTriggerFactoryBean cronTrigger() {
        CronTriggerFactoryBean bean = new CronTriggerFactoryBean();
        /*1、添加cron表達式*/
        bean.setCronExpression("0/10 * * * * ?");
        /*2、添加JobDetailFactoryBean的設置*/
        bean.setJobDetail(Objects.requireNonNull(jobDetailFactoryBean().getObject()));
        return bean;
    }

    /**
     * 觸發器程序調度工廠,傳入觸發器
     * @return
     */
    @Bean
    SchedulerFactoryBean schedulerFactoryBean() {
        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        /*傳入觸發器:參數是可變長參數*/
        bean.setTriggers(cronTrigger().getObject(), simpleTriggerFactoryBean().getObject());
        return bean;
    }

    @Bean
    HelloService helloService() {
        return new HelloService();
    }
}

啟動SpringBoot項目即可:

關於這個配置說如下幾點:

  1. JobDetail 的配置有兩種方式:MethodInvokingJobDetailFactoryBean 和 JobDetailFactoryBean 。
  2. 使用 MethodInvokingJobDetailFactoryBean 可以配置目標 Bean 的名字和目標方法的名字,這種方式不支持傳參。
  3. 使用 JobDetailFactoryBean 可以配置 JobDetail ,任務類繼承自 QuartzJobBean ,這種方式支持傳參,將參數封裝在 JobDataMap 中進行傳遞。
  4. Trigger 是指觸發器,Quartz 中定義了多個觸發器,這里向大家展示其中兩種的用法,SimpleTrigger 和 CronTrigger 。
  5. SimpleTrigger 有點類似於前面說的 @Scheduled 的基本用法。
  6. CronTrigger 則有點類似於 @Scheduled 中 cron 表達式的用法。

二、動態修改定時任務時間

1、編寫application.properties配置文件,配置定時任務的時間

printTime.cron=0/10 * * * * ?

2、定時任務執行類

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
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.time.LocalDateTime;

@Data
@Slf4j
@Component
public class ScheduleTask implements SchedulingConfigurer {

    @Value("${printTime.cron}")
    private String cron;

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        // 動態使用cron表達式設置循環間隔
        taskRegistrar.addTriggerTask(() -> {
            // 執行定時任務的代碼處
            log.info("Current time: {}", LocalDateTime.now());
        }, triggerContext -> {
            // 使用CronTrigger觸發器,可動態修改cron表達式來操作循環規則
            CronTrigger cronTrigger = new CronTrigger(cron);
            return cronTrigger.nextExecutionTime(triggerContext);
        });
    }
}

3、編寫一個接口,使得可以通過調用接口動態修改該定時任務的執行時間

import com.zhixi.config.ScheduleTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@Slf4j
@RestController
@RequestMapping("/cron")
public class TestController {
 
    private final ScheduleTask scheduleTask;
 
    @Autowired
    public TestController(ScheduleTask scheduleTask) {
        this.scheduleTask = scheduleTask;
    }
 
    @GetMapping("/updateCron")
    public String updateCron(String cron) {
        log.info("new cron :{}", cron);
        scheduleTask.setCron(cron);
        return "ok";
    }
}

4、測試發送請求修改cron執行時間

localhost:8080/cron/updateCron?cron=0/5 * * * * ?

三、定時任務巡檢網站,應用宕機,及時發送短信/郵件

  這里定時任務代碼編寫就不說了,上面的示例都有,下面主要寫如何實現。

1、定義枚舉類:給哪些網站/IP發送請求

package com.mixky.app.bjcc.sms;

/**
 * @author zhixi
 */
@SuppressWarnings("all")
public enum SendHttpEnum {

    SITE_NAME_CCW_WEB_1("長城網網站-WEB-1", "http://192.168.148.225/index.html"),
    SITE_NAME_CCW_WEB_2("長城網網站-WEB-2", "http://192.168.148.226/index.html"),
    SITE_NAME_CCW_WEB_3("長城網網站-WEB-3", "http://192.168.148.216/index.html"),
    SITE_NAME_CCW_WEB_4("長城網后台-WEB", "http://192.168.182.77:8080/manager/home.do"),
    SITE_NAME_CCW_WEB_5("長城網后台-搜索", "https://www.bjcc.gov.cn/getSearchPagination.html?keyWords=%E5%9B%BD%E5%BA%86&st=sp&searchType=1");

    /**
     * 網站/ip名稱
     */
    private String siteName;

    /**
     * url地址
     */
    private String siteUrl;

    SendHttpEnum() {
    }

    SendHttpEnum(String siteName, String siteUrl) {
        this.siteName = siteName;
        this.siteUrl = siteUrl;
    }

    public String getSiteName() {
        return siteName;
    }

    public String getSiteUrl() {
        return siteUrl;
    }
}

2、編寫實現的邏輯,主要是通過發送Http請求,短信/郵件自行實現即可

package com.mixky.app.bjcc.sms;

import com.mixky.app.bjcc.member.entity.SMSClient;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName SmSendByCheckEveryDay
 * @Author zhangzhixi
 * @Description 每日巡檢定時任務
 * @Date 2023-03-02 9:46
 * @Version 1.0
 */
@Slf4j
@SuppressWarnings("all")
public class SmSendByCheckEveryDay implements Runnable {

    // 給誰發送短信,如果使用郵箱,自行填寫發送郵箱邏輯即可
    private static final String phone = "手機號";

    // 枚舉類
    private SendHttpEnum sendHttpEnum;

    public SmSendByCheckEveryDay() {
    }

    public SmSendByCheckEveryDay(SendHttpEnum sendHttpEnum) {
        this.sendHttpEnum = sendHttpEnum;
    }

    public void checkEveryDay() throws UnsupportedEncodingException {
        log.info("******************************長城網-巡檢-開始執行******************************");
        // 1、添加需要進行訪問的網站/IP地址
        List<SendHttpEnum> enumList = new ArrayList<>();
        enumList.add(SendHttpEnum.SITE_NAME_CCW_WEB_1);
        enumList.add(SendHttpEnum.SITE_NAME_CCW_WEB_2);
        enumList.add(SendHttpEnum.SITE_NAME_CCW_WEB_3);
        enumList.add(SendHttpEnum.SITE_NAME_CCW_WEB_4);
        enumList.add(SendHttpEnum.SITE_NAME_CCW_WEB_5);

        // 2、執行任務
        for (SendHttpEnum anEnum : enumList) {
            SmSendByCheckEveryDay smSenderByManDao = new SmSendByCheckEveryDay(anEnum);
            smSenderByManDao.run();
        }
        log.info("******************************長城網-巡檢-執行結束******************************");
    }

    @Override
    public void run() {
        SendHttpEnum sendHttpEnum = this.sendHttpEnum;
        String siteUrl = sendHttpEnum.getSiteUrl();

        HttpURLConnection connection = null;
        try {
            URL url = new URL(siteUrl);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            // 置連接超時時間,即在請求連接時等待的最長時間,如果在指定時間內沒有連接成功,則拋出異常
            connection.setConnectTimeout(20000);
            // 設置讀取超時時間,即在請求連接成功后,等待響應數據的最長時間,如果在指定時間內沒有讀取到數據,則拋出異常
            connection.setReadTimeout(20000);
            connection.connect();
            int responseCode = connection.getResponseCode();

            if (responseCode != 200) {
                log.info("=====網站:" + sendHttpEnum.getSiteName() + "無法連接或訪問超時,狀態碼是:" + responseCode + "====");
                senSms("【" + sendHttpEnum.getSiteName() + ":" + sendHttpEnum.getSiteUrl() + "】無法連接或訪問超時,請及時處理!");
            }

        } catch (IOException e) {
            // 超時
            log.info("=====網站:" + sendHttpEnum.getSiteName() + "無法連接或訪問超時====");
            senSms("【" + sendHttpEnum.getSiteName() + ":" + sendHttpEnum.getSiteUrl() + "】無法連接或訪問超時,請及時處理!");
            e.printStackTrace();
        } finally {
            connection.disconnect();
        }
    }

    /**
     * 發送短信的方法,可替換為發送郵件
     * @param sendMessage 發送消息的內容
     */
    @SneakyThrows
    private void senSms(String sendMessage) {
        //漫道短信接口接入
        String content = sendMessage;
        // xml轉義
        if (content.indexOf("&") >= 0) {
            content = content.replace("&", "&");
        }
        if (content.indexOf("<") >= 0) {
            content = content.replace("<", "<");
        }
        if (content.indexOf(">") >= 0) {
            content = content.replace(">", ">");
        }
        content = "【北京長城網】" + content;

        String SN = "SDX-010-27451";
        String PWD = "d-10e";
        SMSClient client = new SMSClient(SN, PWD);
        String result_mt = client.mt(phone, content, "", "", "");
        System.out.println(content);
        if (result_mt.startsWith("-") || result_mt.equals("")) {
            log.info("發送失敗!返回值為:" + result_mt + "。請查看webservice返回值對照表");
        } else {
            log.info("發送成功,返回值為:" + result_mt);
        }
    }
}

3、編寫定時任務,對此方法checkEveryDay進行執行即可

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM