Muleのスケジューラ機能

今まではHTTP Listenerを使用して、リクエストが来たらAPIが起動するというやり方でMuleを動かしていました。今回は違った起動方法でフローを起動させてみます。

スポンサーリンク

今回使うコンポーネント

コンポーネントアイコン・領域用途
Scheduler指定した日時や時間間隔でフローを実行する

起動方法は2種類

Schedulerは2種類の起動方法があります。

時間間隔での起動

フローを一定間隔で起動させる場合は、FixedFrequencyを設定します。

項目設定値
Scheduling StrategyFixedFrequencyを設定する
Frequencyフロー実行後、再度フローを実行するまでの時間間隔
Start delayアプリケーション起動からフローを最初に実行するまでの
Time unitFrequencyとStart delayで指定した数値の時間単位

以下のように設定する場合は、1000ミリ秒=1秒間隔でフローが起動することになります。

シンプルな作りですが、1秒間隔でログ出力するようにフローを作成しました。

実行すると1秒間隔でログ出力されます。

INFO  2023-05-03 12:50:06,126 [[MuleRuntime].uber.04: [mulepractice].FixedFrequencyFlow.CPU_LITE @1c4ec725] [processor: FixedFrequencyFlow/processors/0; event: 6701ec60-fb78-11ed-a441-b0be83167517] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: FixedFrequencyFlow Execute
INFO  2023-05-03 12:50:07,124 [[MuleRuntime].uber.08: [mulepractice].FixedFrequencyFlow.CPU_LITE @1c4ec725] [processor: FixedFrequencyFlow/processors/0; event: 679a82e0-fb78-11ed-a441-b0be83167517] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: FixedFrequencyFlow Execute
INFO  2023-05-03 12:50:08,128 [[MuleRuntime].uber.04: [mulepractice].FixedFrequencyFlow.CPU_LITE @1c4ec725] [processor: FixedFrequencyFlow/processors/0; event: 68338e90-fb78-11ed-a441-b0be83167517] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: FixedFrequencyFlow Execute
INFO  2023-05-03 12:50:09,121 [[MuleRuntime].uber.08: [mulepractice].FixedFrequencyFlow.CPU_LITE @1c4ec725] [processor: FixedFrequencyFlow/processors/0; event: 68cbafe0-fb78-11ed-a441-b0be83167517] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: FixedFrequencyFlow Execute

Cronを使用して起動

FixedFrequencyを使用したフローでは、指定した時間でフローが起動する訳ではないためいまいち使い所が少ないです。SchedulerはCronを使って指定した時間帯で起動させることも可能です。毎時や日時で起動させる場合はこちらのCronを使用します。

項目設定値
Scheduling StrategyCronを設定する
ExpressionQuartzのCron式を記載 
Time ZoneタイムゾーンをJavaのタイムゾーンID表記で記載
例)Asia/Tokyo

Cron式については多少の知識が必要です。Cron式の書き方はリファレンスを参考にしてください。Cron式の書き方次第で、毎分や毎時の指定をすることが可能です。

例:毎秒5秒ごとに起動→0/5 * * * * ?
例:毎分5分ごとに起動→0 0/5 * * * ?
例:1時間おきに起動→0 0 * * * ?

Schedulerコンポーネントを以下のように設定することで、5分おきにフローが起動され、5分間隔でログが吐かれます。

INFO  2023-05-03 17:20:00,173 [[MuleRuntime].uber.09: [mulepractice].CronFlow.CPU_LITE @4f358237] [processor: CronFlow/processors/0; event: 1b5eb6f0-fb9e-11ed-bc3b-b0be83167517] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: CronFlow Execute
INFO  2023-05-03 17:25:00,044 [[MuleRuntime].uber.10: [mulepractice].CronFlow.CPU_LITE @4f358237] [processor: CronFlow/processors/0; event: ce2529e0-fb9e-11ed-bc3b-b0be83167517] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: CronFlow Execute
INFO  2023-05-03 17:30:00,078 [[MuleRuntime].uber.10: [mulepractice].CronFlow.CPU_LITE @4f358237] [processor: CronFlow/processors/0; event: 80f8e340-fb9f-11ed-bc3b-b0be83167517] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: CronFlow Execute

Cronは設定を覚えるのが少し大変ですが、FixedFrequencyの上位互換にあたりますので、必ず覚えておきましょう。

MuleSoft
スポンサーリンク
エスパーラボ

コメント