Springboot 使用定時任務

2021-10-07 14:41:41 字數 791 閱讀 3807

springboot 自帶有定時任務,但是適用場景:

單體架構

單機部署

在某些場景下,還是很不錯的,springboot自帶的方式有2種

1、通過註解的方式

@component

public class myjob

}

2、實現 schedulingconfigurer 介面

@component

@enablescheduling

public class myjob2 implements schedulingconfigurer ,"0/3 * * * * ?"));

}}

預設情況下,springboot的定時任務是單執行緒去執行的,如果需要多執行緒執行,則可以進行修改

步驟就2步:

在啟動類或者配置檔案上新增註解@enableasync

在方法上新增@async

@scheduled(cron = "0/3 * * * * ?")

@async

public void scheduledtaskv1()

@override

@async

public void configuretasks(scheduledtaskregistrar taskregistrar) ,"0/3 * * * * ?"));

}

Spring Boot 定時任務的使用

本文介紹在 spring boot 中如何使用定時任務,使用非常簡單,就不做過多說明了。下面是 類 package org.springboot.sample.config import org.slf4j.logger import org.slf4j.logge ctory import org...

springboot定時任務的使用

有乙個需求是需要從公司內網獲得某些資訊,由於需要定時爬取就想到了定時器。以前曾經用過spring quartz定時器,現在用的springboot好像自帶了乙個微型的spring quartz定時器scheduled.使用如下 component enablescheduling public cl...

SpringBoot 定時任務的使用

springboot自帶了定時任務的功能,不需要額外新增依賴。1 在引導類上加 enablescheduling enablescheduling 啟用定時任務 public class public static void main string args 2 在要定時執行的方法上加 schedu...