首先說明一下,這里使用的是Springboot2.2.6.RELEASE版本,由於Springboot迭代很快,所以要注意版本問題。
1、Scheduled定時任務器:是Spring3.0以后自帶的一個定時任務器。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.2.6.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.bie.springboot</groupId> 12 <artifactId>springboot-job</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>springboot-job</name> 15 <description>Demo project for Spring Boot</description> 16 17 <properties> 18 <java.version>1.8</java.version> 19 </properties> 20 21 <dependencies> 22 <!-- springBoot 的啟動器 --> 23 <dependency> 24 <groupId>org.springframework.boot</groupId> 25 <artifactId>spring-boot-starter-web</artifactId> 26 </dependency> 27 <!-- lombok的依賴包 --> 28 <dependency> 29 <groupId>org.projectlombok</groupId> 30 <artifactId>lombok</artifactId> 31 <optional>true</optional> 32 </dependency> 33 <!-- springBoot測試的啟動器 --> 34 <dependency> 35 <groupId>org.springframework.boot</groupId> 36 <artifactId>spring-boot-starter-test</artifactId> 37 <scope>test</scope> 38 <!--<exclusions> 39 <exclusion> 40 <groupId>org.junit.vintage</groupId> 41 <artifactId>junit-vintage-engine</artifactId> 42 </exclusion> 43 </exclusions>--> 44 </dependency> 45 <!-- 添加 Scheduled坐標 --> 46 <dependency> 47 <groupId>org.springframework</groupId> 48 <artifactId>spring-context-support</artifactId> 49 </dependency> 50 51 </dependencies> 52 53 <build> 54 <plugins> 55 <plugin> 56 <groupId>org.springframework.boot</groupId> 57 <artifactId>spring-boot-maven-plugin</artifactId> 58 </plugin> 59 </plugins> 60 </build> 61 62 </project>
編寫定時任務類,代碼如下所示:
1 package com.bie.springboot.utils; 2 3 import org.springframework.scheduling.annotation.Scheduled; 4 import org.springframework.stereotype.Component; 5 6 import java.text.SimpleDateFormat; 7 import java.util.Date; 8 9 /** 10 * @ProjectName: springboot-job 11 * @Package: com.bie.springboot.utils 12 * @ClassName: ScheduledDemo 13 * @Author: biehl 14 * @Description: ${description} 15 * @Date: 2020/5/21 15:05 16 * @Version: 1.0 17 * <p> 18 * Scheduled 定時任務 19 */ 20 @Component 21 public class ScheduledDemo { 22 23 /** 24 * 定時任務方法。 25 * 26 * @Scheduled:設置定時任務。 27 * 28 * cron 屬性:cron表達式,定時任務觸發是時間的一個字符串表達形式。 29 */ 30 @Scheduled(cron = "0/2 * * * * ?") //每2秒鍾觸發一次方法。 31 public void scheduledMethod() { 32 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 33 System.out.println("定時器被觸發: " + sdf.format(new Date())); 34 } 35 36 }
在啟動類中開啟定時任務的使用,默認不開啟:
1 package com.bie.springboot; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.scheduling.annotation.EnableScheduling; 6 7 @SpringBootApplication 8 @EnableScheduling // 在啟動類中開啟定時任務的使用,默認不開啟 9 public class SpringbootJobApplication { 10 11 public static void main(String[] args) { 12 SpringApplication.run(SpringbootJobApplication.class, args); 13 } 14 15 }
運行效果,如下所示:
2、cron表達式講解,Cron 表達式是一個字符串,分為6或7個域,每一個域代表一個含義,Cron有如下兩種語法格式:
2.1)、第一種:Seconds Minutes Hours Day Month Week Year。秒、分鍾、小時、天、月、周、年。
2.2)、第二種:Seconds Minutes Hours Day Month Week。秒、分鍾、小時、天、月、周,推薦此種寫法。
2.3)、corn從左到右(用空格隔開):秒、分、小時、月份中的日期、月份、星期中的日期、年份。
2.4)、各字段的含義。
位置 | 時間域名 | 允許值 | 允許的特殊字符 |
1 | 秒 | 0-59 | , - * / |
2 | 分鍾 | 0-59 | , - * / |
3 | 小時 | 0-23 | , - * / |
4 | 日 | 1-31 | , - * / L W C |
5 | 月 | 1-12 | , - * / |
6 | 星期 | 1-7 | , - * / ? L C # |
7 | 年(可選) | 1970-2099 | , - * / |
2.5、Cron 表達式的時間字段除允許設置數值外,還可使用一些特殊的字符,提供列表、范圍、通配符等功 能,細說如下:
1)、星號(*) :可用在所有字段中,表示對應時間域的每一個時刻,例如,*在分鍾字段時,表示"每分鍾"。
2)、問號(?):該字符只在日期和星期字段中使用,它通常指定為"無意義的值",相當於占位符。
3)、減號(-) :表達一個范圍,如在小時字段中使用"10-12",則表示從 10 到 12 點,即 10,11,12。
4)、逗號(,) :表達一個列表值,如在星期字段中使用"MON,WED,FRI",則表示星期一,星期三和星期五。
5)、斜杠(/) :x/y 表達一個等步長序列,x 為起始值,y 為增量步長值。如在分鍾字段中使用 0/15,則 表示為 0,15,30 和 45 秒,而 5/15 在分鍾字段中表示 5,20,35,50,你也可以使用*/y,它等同於 0/y。
注意:斜杠/和不使用斜杠的寫法,比如第一位,如果不寫斜杠/,那么表示每分鍾的第幾秒執行,如果寫斜杠/表示每隔幾秒執行。
6)、L :該字符只在日期和星期字段中使用,代表"Last"的意思,但它在兩個字段中意思不同。L 在日期 字段中,表示這個月份的最后一天,如一月的 31 號,非閏年二月的 28 號;如果 L 用在星期中,則表示星 期六,等同於 7。但是,如果 L 出現在星期字段里,而且在前面有一個數值 X,則表示"這個月的最后 X 天", 例如,6L 表示該月的最后星期五;
7)、W :該字符只能出現在日期字段里,是對前導日期的修飾,表示離該日期最近的工作日。例如 15W 表示離該月 15 號最近的工作日,如果該月 15 號是星期六,則匹配 14 號星期五;如果 15 日是星期日, 則匹配 16 號星期一;如果 15 號是星期二,那結果就是 15 號星期二。但必須注意關聯的匹配日期不能夠 跨月,如你指定 1W,如果 1 號是星期六,結果匹配的是 3 號星期一,而非上個月最后的那天。W 字符串 只能指定單一日期,而不能指定日期范圍。
8)、LW 組合 :在日期字段可以組合使用 LW,它的意思是當月的最后一個工作日。
井號(#):該字符只能在星期字段中使用,表示當月某個工作日。如 6#3 表示當月的第三個星期五(6 表示星期五,#3 表示當前的第三個),而 4#5 表示當月的第五個星期三,假設當月沒有第五個星期三, 忽略不觸發。
9)、C :該字符只在日期和星期字段中使用,代表"Calendar"的意思。它的意思是計划所關聯的日期, 如果日期沒有被關聯,則相當於日歷中所有日期。例如 5C 在日期字段中就相當於日歷 5 日以后的第一天。 1C 在星期字段中相當於星期日后的第一天。 Cron 表達式對特殊字符的大小寫不敏感,對代表星期的縮寫英文大小寫也不敏感。
10)、案例說明:Seconds Minutes Hours Day Month Week。秒、分鍾、小時、天、月、周
a、@Scheduled(cron = "0 0 1 1 1 ?") //每年一月的一號的 1:00:00 執行一次。
b、@Scheduled(cron = "0 0 1 1 1,6 ?") //一月和六月的一號的 1:00:00 執行一次。
c、@Scheduled(cron = "0 0 1 1 1,4,7,10 ?") //每個季度的第一個月的一號的 1:00:00 執行一次。
d、@Scheduled(cron = "0 0 1 1 * ?") //每月一號 1:00:00 執行一次。
e、@Scheduled(cron="0 0 1 * * *") //每天凌晨 1 點執行一次。
3、Quartz是OpenSymphony開源組織在Job scheduling領域又一個開源項目,它可以與J2EE與J2SE應用程序相結合也可以單獨使用。Quartz可以用來創建簡單或為運行十個,百個,甚至是好幾萬個Jobs這樣復雜的程序。Jobs可以做成標准的Java組件或 EJBs。
3.1、Quartz 的使用思路:
1)、job任務:你要做什么事?
2)、Trigger觸發器:你什么時候去做?
3)、Scheduler任務調度:你什么時候需要去做什么事?
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <parent> 6 <groupId>org.springframework.boot</groupId> 7 <artifactId>spring-boot-starter-parent</artifactId> 8 <version>2.2.6.RELEASE</version> 9 <relativePath/> <!-- lookup parent from repository --> 10 </parent> 11 <groupId>com.bie.springboot</groupId> 12 <artifactId>springboot-quartz</artifactId> 13 <version>0.0.1-SNAPSHOT</version> 14 <name>springboot-quartz</name> 15 <description>Demo project for Spring Boot</description> 16 17 <properties> 18 <java.version>1.8</java.version> 19 </properties> 20 21 <dependencies> 22 <!-- springboot啟動器 --> 23 <dependency> 24 <groupId>org.springframework.boot</groupId> 25 <artifactId>spring-boot-starter-web</artifactId> 26 </dependency> 27 <!-- springboot測試啟動器 --> 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-starter-test</artifactId> 31 <scope>test</scope> 32 <!--<exclusions> 33 <exclusion> 34 <groupId>org.junit.vintage</groupId> 35 <artifactId>junit-vintage-engine</artifactId> 36 </exclusion> 37 </exclusions>--> 38 </dependency> 39 <!-- Quartz坐標 --> 40 <dependency> 41 <groupId>org.quartz-scheduler</groupId> 42 <artifactId>quartz</artifactId> 43 <version>2.2.1</version> 44 <!-- 去除Quartz坐標自帶的日志記錄 --> 45 <exclusions> 46 <exclusion> 47 <artifactId>slf4j-api</artifactId> 48 <groupId>org.slf4j</groupId> 49 </exclusion> 50 </exclusions> 51 </dependency> 52 <!-- 添加Scheduled坐標,主要使用Cron定時時間 --> 53 <dependency> 54 <groupId>org.springframework</groupId> 55 <artifactId>spring-context-support</artifactId> 56 </dependency> 57 <!-- Sprng tx事務坐標 --> 58 <dependency> 59 <groupId>org.springframework</groupId> 60 <artifactId>spring-tx</artifactId> 61 </dependency> 62 </dependencies> 63 64 <build> 65 <plugins> 66 <plugin> 67 <groupId>org.springframework.boot</groupId> 68 <artifactId>spring-boot-maven-plugin</artifactId> 69 </plugin> 70 </plugins> 71 </build> 72 73 </project>
首先使用Quartz單獨練習,看Quartz如何進行任務調度。
1 package com.bie.springboot.job; 2 3 import org.quartz.*; 4 import org.quartz.impl.StdSchedulerFactory; 5 6 import java.text.SimpleDateFormat; 7 import java.util.Date; 8 9 /** 10 * 定義任務類 11 */ 12 public class JobDemo implements Job { 13 14 /** 15 * 任務被觸發時所執行的方法 16 * 17 * @param jobExecutionContext 18 * @throws JobExecutionException 19 */ 20 @Override 21 public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { 22 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 23 System.out.println("任務調度器: " + sdf.format(new Date())); 24 } 25 26 public static void main(String[] args) throws SchedulerException { 27 // 1、創建 Job 對象:你要做什么事? 28 JobDetail job = JobBuilder.newJob(JobDemo.class).build(); 29 30 // 2、創建 Trigger 對象:在什么時間做? 31 // 第一種,簡單的trigger觸發時間:通過 Quartz 提供一個方法來完成簡單的重復 調用 cron。 32 // 第二種,Trigger:按照 Cron 的表達式來給定觸發的時間。 33 Trigger trigger = TriggerBuilder.newTrigger().withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(2)).build(); 34 35 Trigger trigger2 = TriggerBuilder.newTrigger().withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?")).build(); 36 // 3、創建 Scheduler 對象:在什么時間做什么事? 37 Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); 38 scheduler.scheduleJob(job, trigger); 39 // scheduler.scheduleJob(job, trigger2); 40 41 // 啟動 42 scheduler.start(); 43 } 44 45 }
Spring Boot整合Quartz定時框架。Quartz配置類,完成Springboot對Quartz整合的配置信息。
1 package com.bie.springboot.config; 2 3 import com.bie.springboot.job.JobDemo; 4 import org.springframework.context.annotation.Bean; 5 import org.springframework.context.annotation.Configuration; 6 import org.springframework.scheduling.quartz.CronTriggerFactoryBean; 7 import org.springframework.scheduling.quartz.JobDetailFactoryBean; 8 import org.springframework.scheduling.quartz.SchedulerFactoryBean; 9 10 /** 11 * Quartz配置類,完成Springboot對Quartz整合的配置信息 12 */ 13 @Configuration 14 public class QuartzConfig { 15 16 /** 17 * 第一步,創建Job對象。 18 * 19 * @return 20 */ 21 @Bean 22 public JobDetailFactoryBean jobDetailFactoryBean() { 23 JobDetailFactoryBean factoryBean = new JobDetailFactoryBean(); 24 // 關聯我們創建的Job類 25 factoryBean.setJobClass(JobDemo.class);// 經過反射創建對象 26 return factoryBean; 27 } 28 29 /** 30 * 創建Trigger對象,簡單的Trigger對象。 31 * 32 * @param jobDetailFactoryBean 33 * @return 34 */ 35 // @Bean 36 // public SimpleTriggerFactoryBean simpleTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean) { 37 // SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean(); 38 // // 關聯JobDetail對象 39 // factoryBean.setJobDetail(jobDetailFactoryBean.getObject()); 40 // // 該參數表示一個執行的毫秒數 41 // factoryBean.setRepeatInterval(2000); 42 // // 重復次數 43 // factoryBean.setRepeatCount(5); 44 // return factoryBean; 45 // } 46 47 /** 48 * Cron Trigger 49 * 50 * @param jobDetailFactoryBean 51 * @return 52 */ 53 @Bean 54 public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean) { 55 CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean(); 56 // 關聯JobDetail對象 57 factoryBean.setJobDetail(jobDetailFactoryBean.getObject()); 58 // 設置觸發時間 59 factoryBean.setCronExpression("0/2 * * * * ?"); 60 return factoryBean; 61 } 62 63 64 /** 65 * 第三步,創建Scheduler對象 66 * 67 * @param simpleTriggerFactoryBean 68 * @return 69 */ 70 // @Bean 71 // public SchedulerFactoryBean schedulerFactoryBean(SimpleTriggerFactoryBean simpleTriggerFactoryBean) { 72 // SchedulerFactoryBean factoryBean = new SchedulerFactoryBean(); 73 // // 關聯trigger 74 // factoryBean.setTriggers(simpleTriggerFactoryBean.getObject()); 75 // return factoryBean; 76 // } 77 78 /** 79 * @param cronTriggerFactoryBean 80 * @return 81 */ 82 @Bean 83 public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean cronTriggerFactoryBean) { 84 SchedulerFactoryBean factoryBean = new SchedulerFactoryBean(); 85 // 關聯trigger 86 factoryBean.setTriggers(cronTriggerFactoryBean.getObject()); 87 return factoryBean; 88 } 89 90 }
Spring Boot整合Quartz,啟動類。
1 package com.bie.springboot; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.scheduling.annotation.EnableScheduling; 6 7 /** 8 * spring Boot整合Quartz 9 */ 10 @SpringBootApplication 11 @EnableScheduling // 開啟時間調度 12 public class SpringbootQuartzApplication { 13 14 public static void main(String[] args) { 15 SpringApplication.run(SpringbootQuartzApplication.class, args); 16 } 17 18 }
運行效果,如下所示:
Spring Boot定時任務,Job類對象注入,比如在Job的實現類里面要使用業務層下面某個對象里面的某個方法,就需要將業務層下面的對象注入到該Job實現類里面。
1 package com.bie.springboot.service; 2 3 import org.springframework.stereotype.Service; 4 5 @Service 6 public class UserService { 7 8 public void show() { 9 System.out.println("我喜好你啊,Springboot!"); 10 } 11 12 }
在定義的任務類中注入需要使用的業務層對象,進行方法調用。
1 package com.bie.springboot.job; 2 3 import com.bie.springboot.service.UserService; 4 import org.quartz.*; 5 import org.quartz.impl.StdSchedulerFactory; 6 import org.springframework.beans.factory.annotation.Autowired; 7 8 import java.text.SimpleDateFormat; 9 import java.util.Date; 10 11 /** 12 * 定義任務類 13 */ 14 public class JobDemo implements Job { 15 16 @Autowired 17 private UserService userService; 18 19 /** 20 * 任務被觸發時所執行的方法 21 * 22 * @param jobExecutionContext 23 * @throws JobExecutionException 24 */ 25 @Override 26 public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException { 27 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 28 System.out.println("任務調度器: " + sdf.format(new Date())); 29 30 // 調用業務層的方法。 31 this.userService.show(); 32 } 33 34 public static void main(String[] args) throws SchedulerException { 35 // 1、創建 Job 對象:你要做什么事? 36 JobDetail job = JobBuilder.newJob(JobDemo.class).build(); 37 38 // 2、創建 Trigger 對象:在什么時間做? 39 // 第一種,簡單的trigger觸發時間:通過 Quartz 提供一個方法來完成簡單的重復 調用 cron。 40 // 第二種,Trigger:按照 Cron 的表達式來給定觸發的時間。 41 Trigger trigger = TriggerBuilder.newTrigger().withSchedule(SimpleScheduleBuilder.repeatSecondlyForever(2)).build(); 42 43 Trigger trigger2 = TriggerBuilder.newTrigger().withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?")).build(); 44 // 3、創建 Scheduler 對象:在什么時間做什么事? 45 Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); 46 scheduler.scheduleJob(job, trigger); 47 // scheduler.scheduleJob(job, trigger2); 48 49 // 啟動 50 scheduler.start(); 51 } 52 53 }
啟動主啟動類,出現如下所示的錯誤,那么可以分析出JobDetailFactoryBean創建Job對象的時候是通過反射創建對象的,但是我們此時需要將所使用的對象都放到Spring的Ioc容器中才可以進行依賴。
1 . ____ _ __ _ _ 2 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ 3 ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ 4 \\/ ___)| |_)| | | | | || (_| | ) ) ) ) 5 ' |____| .__|_| |_|_| |_\__, | / / / / 6 =========|_|==============|___/=/_/_/_/ 7 :: Spring Boot :: (v2.2.6.RELEASE) 8 9 2020-05-22 10:25:39.490 INFO 12556 --- [ main] c.b.s.SpringbootQuartzApplication : Starting SpringbootQuartzApplication on DESKTOP-V37QSSE with PID 12556 (D:\program\idea\IntelliJ IDEA 2019.1.3\workspace_idea\springboot-quartz\target\classes started by biehl in D:\program\idea\IntelliJ IDEA 2019.1.3\workspace_idea\springboot-quartz) 10 2020-05-22 10:25:39.494 INFO 12556 --- [ main] c.b.s.SpringbootQuartzApplication : No active profile set, falling back to default profiles: default 11 2020-05-22 10:25:41.160 INFO 12556 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 12 2020-05-22 10:25:41.167 INFO 12556 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 13 2020-05-22 10:25:41.168 INFO 12556 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.33] 14 2020-05-22 10:25:41.244 INFO 12556 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 15 2020-05-22 10:25:41.244 INFO 12556 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1648 ms 16 2020-05-22 10:25:41.317 INFO 12556 --- [ main] org.quartz.impl.StdSchedulerFactory : Using default implementation for ThreadExecutor 17 2020-05-22 10:25:41.324 INFO 12556 --- [ main] org.quartz.core.SchedulerSignalerImpl : Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl 18 2020-05-22 10:25:41.325 INFO 12556 --- [ main] org.quartz.core.QuartzScheduler : Quartz Scheduler v.2.2.1 created. 19 2020-05-22 10:25:41.326 INFO 12556 --- [ main] org.quartz.simpl.RAMJobStore : RAMJobStore initialized. 20 2020-05-22 10:25:41.326 INFO 12556 --- [ main] org.quartz.core.QuartzScheduler : Scheduler meta-data: Quartz Scheduler (v2.2.1) 'schedulerFactoryBean' with instanceId 'NON_CLUSTERED' 21 Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally. 22 NOT STARTED. 23 Currently in standby mode. 24 Number of jobs executed: 0 25 Using thread pool 'org.quartz.simpl.SimpleThreadPool' - with 10 threads. 26 Using job-store 'org.quartz.simpl.RAMJobStore' - which does not support persistence. and is not clustered. 27 28 2020-05-22 10:25:41.326 INFO 12556 --- [ main] org.quartz.impl.StdSchedulerFactory : Quartz scheduler 'schedulerFactoryBean' initialized from an externally provided properties instance. 29 2020-05-22 10:25:41.326 INFO 12556 --- [ main] org.quartz.impl.StdSchedulerFactory : Quartz scheduler version: 2.2.1 30 2020-05-22 10:25:41.327 INFO 12556 --- [ main] org.quartz.core.QuartzScheduler : JobFactory set to: org.springframework.scheduling.quartz.AdaptableJobFactory@620aa4ea 31 2020-05-22 10:25:41.468 INFO 12556 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 32 2020-05-22 10:25:41.596 INFO 12556 --- [ main] o.s.s.c.ThreadPoolTaskScheduler : Initializing ExecutorService 'taskScheduler' 33 2020-05-22 10:25:41.621 INFO 12556 --- [ main] o.s.s.quartz.SchedulerFactoryBean : Starting Quartz Scheduler now 34 2020-05-22 10:25:41.621 INFO 12556 --- [ main] org.quartz.core.QuartzScheduler : Scheduler schedulerFactoryBean_$_NON_CLUSTERED started. 35 2020-05-22 10:25:41.653 INFO 12556 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 36 2020-05-22 10:25:41.656 INFO 12556 --- [ main] c.b.s.SpringbootQuartzApplication : Started SpringbootQuartzApplication in 2.729 seconds (JVM running for 4.369) 37 任務調度器: 2020-05-22 10:25:42 38 2020-05-22 10:25:42.012 ERROR 12556 --- [ryBean_Worker-1] org.quartz.core.JobRunShell : Job DEFAULT.jobDetailFactoryBean threw an unhandled Exception: 39 40 java.lang.NullPointerException: null 41 at com.bie.springboot.job.JobDemo.execute(JobDemo.java:31) ~[classes/:na] 42 at org.quartz.core.JobRunShell.run(JobRunShell.java:202) ~[quartz-2.2.1.jar:na] 43 at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [quartz-2.2.1.jar:na] 44 45 2020-05-22 10:25:42.013 ERROR 12556 --- [ryBean_Worker-1] org.quartz.core.ErrorLogger : Job (DEFAULT.jobDetailFactoryBean threw an exception. 46 47 org.quartz.SchedulerException: Job threw an unhandled exception. 48 at org.quartz.core.JobRunShell.run(JobRunShell.java:213) ~[quartz-2.2.1.jar:na] 49 at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [quartz-2.2.1.jar:na] 50 Caused by: java.lang.NullPointerException: null 51 at com.bie.springboot.job.JobDemo.execute(JobDemo.java:31) ~[classes/:na] 52 at org.quartz.core.JobRunShell.run(JobRunShell.java:202) ~[quartz-2.2.1.jar:na] 53 ... 1 common frames omitted
解決方法,重寫繼承AdaptableJobFactory的類,重寫其方法,將對象注入到Ioc容器中。
1 package com.bie.springboot.utils; 2 3 import org.quartz.spi.TriggerFiredBundle; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.beans.factory.config.AutowireCapableBeanFactory; 6 import org.springframework.scheduling.quartz.AdaptableJobFactory; 7 import org.springframework.stereotype.Component; 8 9 /** 10 * 重寫繼承AdaptableJobFactory的類,重寫其方法,將對象注入到Ioc容器中。 11 */ 12 @Component("myAdaptableJobFactory") 13 public class MyAdaptableJobFactory extends AdaptableJobFactory { 14 15 //AutowireCapableBeanFactory 可以將一個對象添加到 SpringIOC 容器中, 並且完成該對象注入 16 @Autowired 17 private AutowireCapableBeanFactory autowireCapableBeanFactory; 18 19 /** 20 * 該方法需要將實例化的任務對象手動的添加到 spring Ioc 容器中並且完成對 象的注入 21 * 22 * @param bundle 23 * @return 24 * @throws Exception 25 */ 26 @Override 27 protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception { 28 Object obj = super.createJobInstance(bundle); 29 // 將 obj 對象添加 Spring IOC 容器中,並完成注入 30 this.autowireCapableBeanFactory.autowireBean(obj); 31 return obj; 32 } 33 34 }
修改 QuartzConfig 類schedulerFactoryBean方法。
1 package com.bie.springboot.config; 2 3 import com.bie.springboot.job.JobDemo; 4 import com.bie.springboot.utils.MyAdaptableJobFactory; 5 import org.springframework.context.annotation.Bean; 6 import org.springframework.context.annotation.Configuration; 7 import org.springframework.scheduling.quartz.CronTriggerFactoryBean; 8 import org.springframework.scheduling.quartz.JobDetailFactoryBean; 9 import org.springframework.scheduling.quartz.SchedulerFactoryBean; 10 11 /** 12 * Quartz配置類,完成Springboot對Quartz整合的配置信息 13 */ 14 @Configuration 15 public class QuartzConfig { 16 17 /** 18 * 第一步,創建Job對象。 19 * 20 * @return 21 */ 22 @Bean 23 public JobDetailFactoryBean jobDetailFactoryBean() { 24 JobDetailFactoryBean factoryBean = new JobDetailFactoryBean(); 25 // 關聯我們創建的Job類 26 factoryBean.setJobClass(JobDemo.class); 27 return factoryBean; 28 } 29 30 /** 31 * 創建Trigger對象,簡單的Trigger對象。 32 * 33 * @param jobDetailFactoryBean 34 * @return 35 */ 36 // @Bean 37 // public SimpleTriggerFactoryBean simpleTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean) { 38 // SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean(); 39 // // 關聯JobDetail對象 40 // factoryBean.setJobDetail(jobDetailFactoryBean.getObject()); 41 // // 該參數表示一個執行的毫秒數 42 // factoryBean.setRepeatInterval(2000); 43 // // 重復次數 44 // factoryBean.setRepeatCount(5); 45 // return factoryBean; 46 // } 47 48 /** 49 * Cron Trigger 50 * 51 * @param jobDetailFactoryBean 52 * @return 53 */ 54 @Bean 55 public CronTriggerFactoryBean cronTriggerFactoryBean(JobDetailFactoryBean jobDetailFactoryBean) { 56 CronTriggerFactoryBean factoryBean = new CronTriggerFactoryBean(); 57 // 關聯JobDetail對象 58 factoryBean.setJobDetail(jobDetailFactoryBean.getObject()); 59 // 設置觸發時間 60 factoryBean.setCronExpression("0/2 * * * * ?"); 61 return factoryBean; 62 } 63 64 65 /** 66 * 第三步,創建Scheduler對象 67 * 68 * @param simpleTriggerFactoryBean 69 * @return 70 */ 71 // @Bean 72 // public SchedulerFactoryBean schedulerFactoryBean(SimpleTriggerFactoryBean simpleTriggerFactoryBean) { 73 // SchedulerFactoryBean factoryBean = new SchedulerFactoryBean(); 74 // // 關聯trigger 75 // factoryBean.setTriggers(simpleTriggerFactoryBean.getObject()); 76 // return factoryBean; 77 // } 78 79 /** 80 * @param cronTriggerFactoryBean 81 * @return 82 */ 83 @Bean 84 public SchedulerFactoryBean schedulerFactoryBean(CronTriggerFactoryBean cronTriggerFactoryBean, MyAdaptableJobFactory myAdaptableJobFactory) { 85 SchedulerFactoryBean factoryBean = new SchedulerFactoryBean(); 86 // 關聯trigger 87 factoryBean.setTriggers(cronTriggerFactoryBean.getObject()); 88 // 設置JobFactory 89 factoryBean.setJobFactory(myAdaptableJobFactory); 90 return factoryBean; 91 } 92 93 }
實現效果,如下所示: