大連網(wǎng)站建設 領超最好電商運營公司排名
Spring boot開啟定時任務的三種方式(內含源代碼+sql文件)
源代碼+sql文件下載鏈接地址:
https://download.csdn.net/download/weixin_46411355/87486580
目錄
- Spring boot開啟定時任務的三種方式(內含源代碼+sql文件)
- `源代碼+sql文件下載鏈接地址:`[https://download.csdn.net/download/weixin_46411355/87486580](https://download.csdn.net/download/weixin_46411355/87486580)
- 零、前言
- 第一種也就是最簡單的一種:基于注解 (@Scheduled)的方式;
- 第二種:基于接口 (SchedulingConfigurer);
- 第三種:基于注解設定多線程定時任務。
- 一、基于@Scheduled注解的方式
- 1.1 cron
- 1.1.1 定時任務的方法在啟動類里面
- 1.1.2 定時任務的方法在啟動類之外
- 1.2 fixedDelay
- 1.3 fixedRate
- 1.4 initialDelay
- 一點五 cron解釋
- 結構
- 取值范圍
- 常例
- 二、基于SchedulingConfigurer接口的方式
- 2.1數(shù)據(jù)庫準備
- 2.2. 創(chuàng)建一個SpringBoot項目
- 2.3 項目結構
- 2.4 數(shù)據(jù)源基本配置:application.properties
- 2.5 mapper也就是dao:
- 2.6 task類:MyTask.java
- 2.7 使用上之前學的SpringBoot整合logback
- 2.7.1 創(chuàng)建logback-spring.xml
- 2.7.2 修改task類:MyTask.java
- 2.8 運行結果:
- 三、 基于注解設定多線程定時任務
- 四、總結
零、前言
spring boot進行定時任務一共有三種方式。
第一種也就是最簡單的一種:基于注解 (@Scheduled)的方式;
第二種:基于接口 (SchedulingConfigurer);
第三種:基于注解設定多線程定時任務。
一、基于@Scheduled注解的方式
1.1 cron
首先,打開idea,創(chuàng)建springboot項目,無需引入任何jar,springboot自帶定時。
然后,在啟動類中用注解@EnableScheduling進行標注,表明此類 存在定時任務。
1.1.1 定時任務的方法在啟動類里面
在定時執(zhí)行的方法之上添加注解@Scheduled(cron =“*/6 * * * * ?”)。
package com.bjpowernode.springboottimedtask01at_scheduled_annotation;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;@EnableScheduling
@SpringBootApplication
public class SpringbootTimedtask01AtScheduledAnnotationApplication {public static void main(String[] args) {SpringApplication.run(SpringbootTimedtask01AtScheduledAnnotationApplication.class, args);}@Scheduled(cron ="*/6 * * * * ?")public void sayHello() {System.out.println("hello");}}
點擊啟動,即可看到控制臺6秒輸出一次“hello”。
1.1.2 定時任務的方法在啟動類之外
當然,定時任務也可以放在其他類中。例如創(chuàng)建類Task1。
package com.bjpowernode.springboottimedtask01at_scheduled_annotation02.task;import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;@Component
public class Task1 {@Scheduled(cron ="*/1 * * * * ?")public void sayWord() {System.out.println("world");}
}
然后可以看到控制臺的輸出結果:
這里有個要注意的細節(jié),就是啟動類需要能掃描到定時任務類,否則定時任務啟動不起來。不僅需要@Component注解,也需要將啟動類位置位于定時任務類之上。如下圖:
筆者就是犯了這樣的錯,一直沒啟動起來。
@Scheduled除過cron還有三種方式:fixedRate,fixedDelay,initialDelay
cron:表達式可以定制化執(zhí)行任務,但是執(zhí)行的方式是與fixedDelay相近的,也是會按照上一次方法結束時間開始算起。
1.2 fixedDelay
fixedDelay:控制方法執(zhí)行的間隔時間,是以上一次方法執(zhí)行完開始算起,如上一次方法執(zhí)行阻塞住了,那么直到上一次執(zhí)行完,并間隔給定的時間后,執(zhí)行下一次。
package com.bjpowernode.springboottimedtask01at_scheduld_annotationfixeddelay.task;import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;@EnableScheduling//開啟定時任務
@Configuration
public class ScheduleTask1 {//每3秒執(zhí)行一次@Scheduled(fixedDelay = 3000)private void myTask(){System.out.println("I do myself per third seconds");}
}
1.3 fixedRate
fixedRate:是按照一定的速率執(zhí)行,是從上一次方法執(zhí)行開始的時間算起,如果上一次方法阻塞住了,下一次也是不會執(zhí)行,但是在阻塞這段時間內累計應該執(zhí)行的次數(shù),當不再阻塞時,一下子把這些全部執(zhí)行掉,而后再按照固定速率繼續(xù)執(zhí)行。
package com.bjpowernode.springboottimetask01at_scheduld_annotationfixedrate.task;import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;@Component
@EnableScheduling//開啟定時事務
public class ScheduleTask2 {//每10s執(zhí)行一次@Scheduled(fixedRate = 10000)public void myTask2(){System.out.println("我是一個定時任務");}
}
1.4 initialDelay
initialDelay:initialDelay = 10000 表示在容器啟動后,延遲10秒后再執(zhí)行一次定時器。
package com.bjpowernode.springboottimedtask01at_scheduld_annotationinitdelay.task;import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;@EnableScheduling//開啟定時任務
@Component
public class ScheduleTask3 {//容器啟動后,延遲10秒再執(zhí)行一次定時器,以后每10秒再執(zhí)行一次定時器@Scheduled(initialDelay = 10000,fixedRate = 10000)private void myTask3(){System.out.println("我是一個定時任務");}}
一點五 cron解釋
cron
cron 用法跟linux下是一摸一樣的,如果你搞過linux下的定時,那么必然很熟悉。
結構
cron表達式是一個字符串,分為6或7個域,每兩個域之間用空格分隔,
其語法格式為:“秒域 分域 時域 日域 月域 周域 年域”
取值范圍
常例
本方法的demo地址: GitHub - SUST-MaZhen/scheduledTask: 基于注解@Scheluded的方式實現(xiàn)定時任務
二、基于SchedulingConfigurer接口的方式
使用@Scheduled 注解很方便,但缺點是當我們調整了執(zhí)行周期的時候,需要重啟應用才能生效,這多少有些不方便。為了達到實時生效的效果,那么可以使用接口來完成定時任務,統(tǒng)一將定時器信息存放在數(shù)據(jù)庫中。
2.1數(shù)據(jù)庫準備
在mysql中創(chuàng)建一個數(shù)據(jù)庫
在mysql中執(zhí)行一下腳本插入定時任務:
drop table if exists `scheduled`;
create table `scheduled` (`cron_id` varchar(30) NOT NULL primary key,`cron_name` varchar(30) NULL,`cron` varchar(30) NOT NULL
);
insert into `scheduled` values ('1','定時器任務一','0/6 * * * * ?');
2.2. 創(chuàng)建一個SpringBoot項目
創(chuàng)建一個springboot 項目:我們這里只添加一個mapper,不要bean也不要service以及controller,只是為了演示定時功能而已。
2.3 項目結構
demo結構:
2.4 數(shù)據(jù)源基本配置:application.properties
application.properties
## mysql數(shù)據(jù)源配置
spring.datasource.url=jdbc:mysql://localhost:13306/powernode_scheduledtask?useUnicode=true&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
2.5 mapper也就是dao:
package com.bjpowenode.springboottimetask02implementsscheduling_configureinterface.mapper;import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;@Mapper
@Repository
public interface CronMapper {@Select("select cron from scheduled where cron_id = #{id}")public String getCron(int id);
}
2.6 task類:MyTask.java
package com.bjpowenode.springboottimetask02implementsscheduling_configureinterface.scheduled;import com.bjpowenode.springboottimetask02implementsscheduling_configureinterface.mapper.CronMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;@EnableScheduling
@Component
public class MyTask implements SchedulingConfigurer{@Autowiredprotected CronMapper cronMapper;@Overridepublic void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {scheduledTaskRegistrar.addTriggerTask(//執(zhí)行定時任務()->process(),//初始化定時任務周期triggerContext -> {String cron = cronMapper.getCron(1);if(cron.isEmpty()){System.out.println("cron is null");}return new CronTrigger(cron).nextExecutionTime(triggerContext);});}private void process(){System.out.println("基于接口定時任務");}
}
2.7 使用上之前學的SpringBoot整合logback
2.7.1 創(chuàng)建logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 日志級別從低到高分為TRACE < DEBUG < INFO < WARN < ERROR < FATAL,如果設置為WARN,則低于WARN的信息都不會輸出 -->
<!-- scan:當此屬性設置為true時,配置文件如果發(fā)生改變,將會被重新加載,默認值為true -->
<!-- scanPeriod:設置監(jiān)測配置文件是否有修改的時間間隔,如果沒有給出時間單位,默認單位是毫秒。當scan為true時,此屬性生效。默認的時間間隔為1分鐘。 -->
<!-- debug:當此屬性設置為true時,將打印出logback內部日志信息,實時查看logback運行狀態(tài)。默認值為false。 -->
<configuration scan="true" scanPeriod="10 seconds"><!--<include resource="org/springframework/boot/logging/logback/base.xml" />--><contextName>logback</contextName><!-- name的值是變量的名稱,value的值時變量定義的值。通過定義的值會被插入到logger上下文中。定義變量后,可以使“${}”來使用變量。 --><property name="log.path" value="D:/mylogs/" /><!-- 彩色日志 --><!-- 彩色日志依賴的渲染類 --><conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" /><conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" /><conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" /><!-- 彩色日志格式 --><property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/><!--輸出到控制臺--><appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"><!--此日志appender是為開發(fā)使用,只配置最低級別,控制臺輸出的日志級別是大于或等于此級別的日志信息--><filter class="ch.qos.logback.classic.filter.ThresholdFilter"><level>info</level></filter><encoder><Pattern>${CONSOLE_LOG_PATTERN}</Pattern><!-- 設置字符集 --><charset>UTF-8</charset></encoder></appender><!--輸出到文件--><!-- 時間滾動輸出 level為 DEBUG 日志 --><appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"><!-- 正在記錄的日志文件的路徑及文件名 --><file>${log.path}/log_debug.log</file><!--日志文件輸出格式--><encoder><pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern><charset>UTF-8</charset> <!-- 設置字符集 --></encoder><!-- 日志記錄器的滾動策略,按日期,按大小記錄 --><rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><!-- 日志歸檔 --><fileNamePattern>${log.path}/debug/log-debug-%d{yyyy-MM-dd}.%i.log</fileNamePattern><timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"><maxFileSize>100MB</maxFileSize></timeBasedFileNamingAndTriggeringPolicy><!--日志文件保留天數(shù)--><maxHistory>15</maxHistory></rollingPolicy><!-- 此日志文件只記錄debug級別的 --><filter class="ch.qos.logback.classic.filter.LevelFilter"><level>debug</level><onMatch>ACCEPT</onMatch><onMismatch>DENY</onMismatch></filter></appender><!-- 時間滾動輸出 level為 INFO 日志 --><appender name="INFO_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"><!-- 正在記錄的日志文件的路徑及文件名 --><file>${log.path}/log_info.log</file><!--日志文件輸出格式--><encoder><pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern><charset>UTF-8</charset></encoder><!-- 日志記錄器的滾動策略,按日期,按大小記錄 --><rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><!-- 每天日志歸檔路徑以及格式 --><fileNamePattern>${log.path}/info/log-info-%d{yyyy-MM-dd}.%i.log</fileNamePattern><timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"><maxFileSize>100MB</maxFileSize></timeBasedFileNamingAndTriggeringPolicy><!--日志文件保留天數(shù)--><maxHistory>15</maxHistory></rollingPolicy><!-- 此日志文件只記錄info級別的 --><filter class="ch.qos.logback.classic.filter.LevelFilter"><level>info</level><onMatch>ACCEPT</onMatch><onMismatch>DENY</onMismatch></filter></appender><!-- 時間滾動輸出 level為 WARN 日志 --><appender name="WARN_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"><!-- 正在記錄的日志文件的路徑及文件名 --><file>${log.path}/log_warn.log</file><!--日志文件輸出格式--><encoder><pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern><charset>UTF-8</charset> <!-- 此處設置字符集 --></encoder><!-- 日志記錄器的滾動策略,按日期,按大小記錄 --><rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><fileNamePattern>${log.path}/warn/log-warn-%d{yyyy-MM-dd}.%i.log</fileNamePattern><timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"><maxFileSize>100MB</maxFileSize></timeBasedFileNamingAndTriggeringPolicy><!--日志文件保留天數(shù)--><maxHistory>15</maxHistory></rollingPolicy><!-- 此日志文件只記錄warn級別的 --><filter class="ch.qos.logback.classic.filter.LevelFilter"><level>warn</level><onMatch>ACCEPT</onMatch><onMismatch>DENY</onMismatch></filter></appender><!-- 時間滾動輸出 level為 ERROR 日志 --><appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"><!-- 正在記錄的日志文件的路徑及文件名 --><file>${log.path}/log_error.log</file><!--日志文件輸出格式--><encoder><pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern><charset>UTF-8</charset> <!-- 此處設置字符集 --></encoder><!-- 日志記錄器的滾動策略,按日期,按大小記錄 --><rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"><fileNamePattern>${log.path}/error/log-error-%d{yyyy-MM-dd}.%i.log</fileNamePattern><timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"><maxFileSize>100MB</maxFileSize></timeBasedFileNamingAndTriggeringPolicy><!--日志文件保留天數(shù)--><maxHistory>15</maxHistory></rollingPolicy><!-- 此日志文件只記錄ERROR級別的 --><filter class="ch.qos.logback.classic.filter.LevelFilter"><level>ERROR</level><onMatch>ACCEPT</onMatch><onMismatch>DENY</onMismatch></filter></appender><!--<logger>用來設置某一個包或者具體的某一個類的日志打印級別、以及指定<appender>。<logger>僅有一個name屬性,一個可選的level和一個可選的addtivity屬性。name:用來指定受此logger約束的某一個包或者具體的某一個類。level:用來設置打印級別,大小寫無關:TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF,還有一個特俗值INHERITED或者同義詞NULL,代表強制執(zhí)行上級的級別。如果未設置此屬性,那么當前l(fā)ogger將會繼承上級的級別。addtivity:是否向上級logger傳遞打印信息。默認是true。--><!--<logger name="org.springframework.web" level="info"/>--><!--<logger name="org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor" level="INFO"/>--><!--使用mybatis的時候,sql語句是debug下才會打印,而這里我們只配置了info,所以想要查看sql語句的話,有以下兩種操作:第一種把<root level="info">改成<root level="DEBUG">這樣就會打印sql,不過這樣日志那邊會出現(xiàn)很多其他消息第二種就是單獨給dao下目錄配置debug模式,代碼如下,這樣配置sql語句會打印,其他還是正常info級別:--><!--root節(jié)點是必選節(jié)點,用來指定最基礎的日志輸出級別,只有一個level屬性level:用來設置打印級別,大小寫無關:TRACE, DEBUG, INFO, WARN, ERROR, ALL 和 OFF,不能設置為INHERITED或者同義詞NULL。默認是DEBUG可以包含零個或多個元素,標識這個appender將會添加到這個logger。--><!--開發(fā)環(huán)境:打印控制臺--><springProfile name="dev"><logger name="com.nmys.view" level="debug"/></springProfile><root level="info"><appender-ref ref="CONSOLE" /><appender-ref ref="DEBUG_FILE" /><appender-ref ref="INFO_FILE" /><appender-ref ref="WARN_FILE" /><appender-ref ref="ERROR_FILE" /></root><!--生產環(huán)境:輸出到文件--><!--<springProfile name="pro">--><!--<root level="info">--><!--<appender-ref ref="CONSOLE" />--><!--<appender-ref ref="DEBUG_FILE" />--><!--<appender-ref ref="INFO_FILE" />--><!--<appender-ref ref="ERROR_FILE" />--><!--<appender-ref ref="WARN_FILE" />--><!--</root>--><!--</springProfile>--></configuration>
2.7.2 修改task類:MyTask.java
package com.bjpowenode.springboottimetask02implementsscheduling_configureinterface.scheduled;import com.bjpowenode.springboottimetask02implementsscheduling_configureinterface.mapper.CronMapper;import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
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;@Slf4j//直接采用lombok的注解即可
@EnableScheduling
@Component
public class MyTask implements SchedulingConfigurer{@Autowiredprotected CronMapper cronMapper;@Overridepublic void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {scheduledTaskRegistrar.addTriggerTask(//執(zhí)行定時任務()->process(),//初始化定時任務周期triggerContext -> {String cron = cronMapper.getCron(1);if(cron.isEmpty()){System.out.println("cron is null");}return new CronTrigger(cron).nextExecutionTime(triggerContext);});}private void process(){log.info(new Date().toString());System.out.println("基于接口定時任務");}
}
2.8 運行結果:
從結果中可以看出,是按照每6秒也就是數(shù)據(jù)庫中查詢的結果來進行的。
需求:我現(xiàn)在需要每10秒執(zhí)行一次定時任務,該怎么辦呢?對!只需要修改數(shù)據(jù)庫值即可,server無需重啟。觀察修改后的結果。
demo地址:GitHub - SUST-MaZhen/scheduledtask2: springboot基于接口的定時任務
三、 基于注解設定多線程定時任務
前面講到了@Scheduled執(zhí)行周期任務會受到上次一個任務的執(zhí)行時間影響。那么可以開啟多線程執(zhí)行周期任務。
創(chuàng)建springboot項目
創(chuàng)建一個多線程定時任務類如下:
package com.bjpowernode.springboottimedtask03at_scheduld_annotationmultithreadtask.task;import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;import java.time.LocalDateTime;@EnableAsync//2.開啟多線程
@EnableScheduling//1.開啟定時任務
@Component
public class MultiThreadTask {@Async@Scheduled(fixedDelay = 1000) //間隔1秒public void first() throws InterruptedException {System.out.println("第一個定時任務開始 : " + LocalDateTime.now().toLocalTime() + "\r\n線程 : " + Thread.currentThread().getName());Thread.sleep(1000 * 10);}@Async@Scheduled(fixedDelay = 2000)public void second() {System.out.println("第二個定時任務開始 : " + LocalDateTime.now().toLocalTime() + "\r\n線程 : " + Thread.currentThread().getName());}
}
執(zhí)行結果如下:
從結果可以看出:第一個任務的執(zhí)行時間也不受其本身執(zhí)行時間的限制。兩個任務也互不影響。
demo地址: GitHub - SUST-MaZhen/scheduledtash3: spring boot基于多線程的定時任務
四、總結
本文介紹了spring boot創(chuàng)建定時任務的三種方式,當然還有其他方式,