この記事の内容は SpringBoot によるスケジュールされたタスクの動的管理の実装コードです。必要な方は参考にしていただければ幸いです。
SpringBoot はスケジュールされたタスクを動的に管理します
最初に @EnableScheduling を使用してスケジュールされたタスクを有効にします
package com.fighting; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling public class ScheduledApplication { public static void main(String[] args) { SpringApplication.run(ScheduledApplication.class, args); } }
ログ レベルを構成します
logging.level.com= debug logging.file=springboot-scheduled.log
固定期間のスケジュールされたタスク
package com.fighting; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; /** * Spring静态周期定时任务 * * @author fighting * @date 2018-09-11 */ @Component public class SpringStaticCronTask { public static final Logger logger = LoggerFactory.getLogger(SpringStaticCronTask.class); @Scheduled(cron = "0/5 * * * * ?") public void staticCornTask() { logger.debug("staticCronTask is running..."); } }
スケジュールされた期間を動的に変更します
SchedulingConfigurer インターフェイスを実装し、configureTasks をオーバーライドします方法
package com.fighting; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.springframework.scheduling.config.ScheduledTaskRegistrar; import org.springframework.scheduling.support.CronTrigger; import org.springframework.stereotype.Component; /** * 动态定时任务 * * @author fighting * @date 2018-09-11 */ @Component public class SpringDynamicCronTask implements SchedulingConfigurer { private static final Logger logger = LoggerFactory.getLogger(SpringDynamicCronTask.class); private static String cron = "0/5 * * * * ?"; @Override public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) { scheduledTaskRegistrar.addTriggerTask(() -> { // 任务逻辑 logger.error("dynamicCronTask is running..."); }, triggerContext -> { // 任务触发,在这里可修改任务的执行周期,因为每次调度都会执行这里 CronTrigger cronTrigger = new CronTrigger(cron); return cronTrigger.nextExecutionTime(triggerContext); }); } public SpringDynamicCronTask() { //模拟业务修改周期,可以在具体业务中修改参数cron new Thread(() -> { try { Thread.sleep(15000); } catch (InterruptedException e) { e.printStackTrace(); } cron = "0/2 * * * * ?"; }).start(); } }
観察 結果を印刷します。サイクルは変更されましたが、プロジェクトは再開されていません。
2018-09-11 13:36:50.009 DEBUG 16708 --- [pool-1-thread-1] com.fighting.SpringStaticCronTask : staticCronTask is running... 2018-09-11 13:36:50.010 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask : dynamicCronTask is running... 2018-09-11 13:36:55.001 DEBUG 16708 --- [pool-1-thread-1] com.fighting.SpringStaticCronTask : staticCronTask is running... 2018-09-11 13:36:55.002 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask : dynamicCronTask is running... 2018-09-11 13:37:00.009 DEBUG 16708 --- [pool-1-thread-1] com.fighting.SpringStaticCronTask : staticCronTask is running... 2018-09-11 13:37:00.016 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask : dynamicCronTask is running... 2018-09-11 13:37:05.016 DEBUG 16708 --- [pool-1-thread-1] com.fighting.SpringStaticCronTask : staticCronTask is running... 2018-09-11 13:37:05.016 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask : dynamicCronTask is running... 2018-09-11 13:37:06.013 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask : dynamicCronTask is running... 2018-09-11 13:37:08.008 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask : dynamicCronTask is running... 2018-09-11 13:37:10.002 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask : dynamicCronTask is running... 2018-09-11 13:37:10.003 DEBUG 16708 --- [pool-1-thread-1] com.fighting.SpringStaticCronTask : staticCronTask is running... 2018-09-11 13:37:12.002 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask : dynamicCronTask is running... 2018-09-11 13:37:14.006 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask : dynamicCronTask is running... 2018-09-11 13:37:15.015 DEBUG 16708 --- [pool-1-thread-1] com.fighting.SpringStaticCronTask : staticCronTask is running... 2018-09-11 13:37:16.012 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask : dynamicCronTask is running... 2018-09-11 13:37:18.002 ERROR 16708 --- [pool-1-thread-1] com.fighting.SpringDynamicCronTask : dynamicCronTask is running...
関連する推奨事項:
SpringBoot のタスクのスケジュール設定と一般的なタスク式
以上がスケジュールされたタスクの SpringBoot 動的管理の実装コードの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

Javaは、Java Virtual Machines(JVMS)とBytecodeに依存している「Write and Averywherewherewherewherewherewherewhere」の哲学のために、プラットフォームに依存しません。 1)Javaコードは、JVMによって解釈されるか、地元でその場でコンパイルされたBytecodeにコンパイルされます。 2)ライブラリの依存関係、パフォーマンスの違い、環境構成に注意してください。 3)標準ライブラリを使用して、クロスプラットフォームのテストとバージョン管理がプラットフォームの独立性を確保するためのベストプラクティスです。

java'splatformindepenceisnotsimple; itinvolvescomplexities.1)jvmcompatibilitymustbeensuredacrosplatforms.2)nativeLibrariesandsystemCallSneedCarefulHandling.3)依存症の依存症の依存症と依存症の依存症と依存関係の増加 - プラットフォームのパフォーマンス

java'splatformentedentencebenefitswebapplicationsbyAllowingCodeTorunOnySystemwithajvm、simpledifyifieddeploymentandscaling.itenables:1)easydeploymentddifferentservers、2)Seamlessscalingacroscloudplatforms、および3)deminvermentementmentmentmentmentementtodeploymentpoce

jvmistheruntimeenvironment forexecutingjavabytecode、Curivalforjavaの「writeonce、runanywhere」capability.itmanagesmemory、executessuressecurity、makingestessentionentionalforjavadevadedertionserstunterstanderforeffication devitivationdevation

JavareMainsAtopChoiceFordevelopersDuetoitsPlatformEndepentence、Object-OrientedDesign、stryngting、automaticmemorymanagement、およびcomprehensivestandardlibrary.thesefeaturesmavaversatilatileandpowerful、sustableforawiderangeofplications、daspitesomech

java'splatformentencemeansdeveloperscancancodecodeonceanddevicewithoutrocompilling.cancodecodecodecodecodecodecodecodecodecodecodecode compilling

JVMをセットアップするには、次の手順に従う必要があります。1)JDKをダウンロードしてインストールする、2)環境変数を設定する、3)インストールの確認、4)IDEを設定する、5)ランナープログラムをテストします。 JVMのセットアップは、単に機能するだけでなく、メモリの割り当て、ガベージコレクション、パフォーマンスチューニング、エラー処理の最適化を行い、最適な動作を確保することも含まれます。

toensurejavaplatformindopendence、soflowthesesteps:1)compileandrunyourapplicationOnMultiplePlatformsusingDifferentosAndjvversions.2)utilizeci/cdpipelines


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

SublimeText3 英語版
推奨: Win バージョン、コードプロンプトをサポート!

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。
