1.アプリケーションからスケジュールされたタスクを開始します
(1)説明:スケジュールされたタスクが開始され、アプリケーションスタートアップクラスと共に実行されます
(2)クラスコードの開始:Application.java
パッケージcom.test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @ComponentScan({ "com.vsked.autostart"、 "com.vsked.task" }) @EnableScheduling パブリック クラスアプリケーション{ 公共 静的 ボイドメイン(文字列[]引数)を{ SpringApplication.run(応用クラス、引数) ; } }
(3)タイマーコード:TimeTask.java
パッケージcom.test .config; import java.util.Date; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component パブリッククラスTimeTask { プライベート静的最終ロガーログ= LoggerFactory.getLogger(TimeTask。クラス)。 @Scheduled(cron = "0 * / 1 * * *?")// 定時:每一分钟执行一次public void cleanLog1DataTask(){ log.info( "1分------------> |" + 新しい日付()+ "|" ); } }
2. yamlファイルからスケジュールされたタスク時間を構成する
(1)説明:yamlファイルからスケジュールされたタスクのタイミングを構成しますアプリケーションスタートアップクラスが開始されると、スケジュールされたタスクも一緒に開始されます。
(2)設定ファイル:application.yaml
ジョブ:
スケジュール:
-1 * * * * *
備考:1分でスケジュールされたタスクを実行する
(3)スケジュールされたタスクのBean:CronConfig.java
パッケージcom.jcdz.hbdservice.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import java.util.List; @Configuration @ConfigurationProperties(prefix = "job" ) @PropertySource( "classpath:application.yml" ) public class CronConfig { private List <String> schedules; @Bean パブリックリスト<文字列>スケジュール(){ 戻り 、この.schedulesと、 } public List <String> getSchedules(){ スケジュールを返す; } public void setSchedules(List <String> schedules){ this .schedules = schedules; } }
(4)時限タスク実行コード:TimeTask.java
パッケージcom.jcdz.hbdservice.config; import org.springframework.stereotype.Component; @Component パブリック クラス TimeTask はRunnableを 実装します{ @Override public void run(){ System.out.println( "!行中!!!!!!!!!" ); } }
備考:スケジュールされたタスクを必要とするコードはここに配置されます
(5)ScheduledTasks.java
パッケージcom.jcdz.hbdservice.config; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.support.CronTrigger; import org.springframework.stereotype.Component; @Component public class ScheduledTasks { @Autowired private TaskScheduler taskScheduler; @Autowired プライベートCronConfig cronConfig; @Autowired プライベートTimeTask timeTask; プライベート 静的 最終ロガーログ= LoggerFactory.getLogger(ScheduledTasks。クラス); public void scheduleAllCrons(){ cronConfig.getSchedules()。forEach( cron-> taskScheduler.schedule(timeTask、new CronTrigger(cron))); } }
(6)スタートアップクラス:Application.java
import com.jcdz.hbdservice.config.ScheduledTasks; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.scheduling.TaskScheduler; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler; @SpringBootApplication @ComponentScan({"com.jcdz.hbdservice.config" }) @EnableAsync public class Application { @Bean public TaskScheduler taskScheduler(){ return new ConcurrentTaskScheduler(); } パブリック 静的 ボイドメイン(文字列[]引数)をスロー例外{ ApplicationContextのCTX = SpringApplication.run(アプリケーションクラス)。 ScheduledTasks scheduledTasks = ctx.getBean(ScheduledTasks。クラス)。 cheduledTasks.scheduleAllCrons(); } }
(7)ディレクトリ構造
(8)概要:構成ファイルを介して複数の時間指定タスクを制御する場合は、構成ファイルで複数のジョブを構成し、他のコードに対応する複数のジョブを記述します。
リファレンス:http://www.chinaoc.com.cn/p/1277431.html