
cron式の解析、検証、移行、記述のためのJavaライブラリ。複数の形式(Unix、Quartz、Spring)と人間が読めるロケール固有の出力をサポート。
私たちはcronを定義し、そしてそれをサポートします。
cron-utils は、cron を定義、解析、検証、移行し、それらの人間が読める説明を取得するための Java ライブラリです。このプロジェクトは セマンティックバージョニング規約 に従い、OSGi メタデータを提供し、Apache 2.0 ライセンスを使用しています。
ダウンロード
cron-utils は Maven central リポジトリで利用できます。
<dependency>
<groupId>com.cronutils</groupId>
<artifactId>cron-utils</artifactId>
<version>9.2.1</version>
</dependency>
Android 開発者の方へ: cron-utils 7.0.0 は Android 26+ を前提としています。それ以前の Android バージョンでは cron-utils 6.0.6 の使用を検討してください。 Java EE の ScheduleExpression を使用する場合、これは実行時依存関係として提供する必要があります。
現在の開発状況
現在、JDK 16 に向けてコードベースを更新中です。リリース時に JDK 17 と完全に互換性があることを確認します。
現在、ニューラル翻訳を使用した次世代の cron 記述子を開発中です! データセット生成の支援から機械学習モデルのトレーニング、それらのロード用ユーティリティまで、あらゆる種類の貢献を歓迎します! 興味があれば、issue #3 をフォローしてください。
特徴
使用例
以下にいくつかの例を示します。これらおよびその他の例は、cron-utils ライブラリを紹介するために作成した サンプルリポジトリ にあります!
cron 定義の構築
// Define your own cron: arbitrary fields are allowed and last field can be optional
CronDefinition cronDefinition =
CronDefinitionBuilder.defineCron()
.withSeconds().and()
.withMinutes().and()
.withHours().and()
.withDayOfMonth()
.supportsHash().supportsL().supportsW().and()
.withMonth().and()
.withDayOfWeek()
.withIntMapping(7, 0) //we support non-standard non-zero-based numbers!
.supportsHash().supportsL().supportsW().and()
.withYear().optional().and()
.instance();
// or get a predefined instance
cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(QUARTZ);
cron 式の構築
// Create a cron expression. CronMigrator will ensure you remain cron provider agnostic
import static com.cronutils.model.field.expression.FieldExpressionFactory.*;
Cron cron = CronBuilder.cron(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ))
.withYear(always())
.withDoM(between(SpecialChar.L, 3))
.withMonth(always())
.withDoW(questionMark())
.withHour(always())
.withMinute(always())
.withSecond(on(0))
.instance();
// Obtain the string expression
String cronAsString = cron.asString(); // 0 * * L-3 * ? *
解析
// Create a parser based on provided definition
CronParser parser = new CronParser(cronDefinition);
Cron quartzCron = parser.parse("0 23 * ? * 1-5 *");
... さらにマルチ cron 式も! 複数の cron を1行にまとめてみませんか? 0 0 9 * * ? *、0 0 10 * * ? *、0 30 11 * * ? *、0 0 12 * * ? * と書く代わりに、0 0|0|30|0 9|10|11|12 * * ? * にまとめることができます。
説明
// Create a descriptor for a specific Locale
CronDescriptor descriptor = CronDescriptor.instance(Locale.UK);
// Parse some expression and ask descriptor for description
String description = descriptor.describe(parser.parse("*/45 * * * * ?"));
// Description will be: "every 45 seconds"
description = descriptor.describe(quartzCron);
// Description will be: "every hour at minute 23 every day between Monday and Friday"
// which is the same description we get for the cron below:
descriptor.describe(parser.parse("0 23 * ? * MON-FRI *"));
移行
// Migration between cron libraries has never been so easy!
// Turn cron expressions into another format by using CronMapper:
CronMapper cronMapper = CronMapper.fromQuartzToCron4j();
Cron cron4jCron = cronMapper.map(quartzCron);
// and to get a String representation of it, we can use
cron4jCron.asString();//will return: 23 * * * 1-5
検証
cron4jCron.validate()
実行からの / までの時間計算
// Get date for last execution
ZonedDateTime now = ZonedDateTime.now();
ExecutionTime executionTime = ExecutionTime.forCron(parser.parse("* * * * * ? *"));
ZonedDateTime lastExecution = executionTime.lastExecution(now);
// Get date for next execution
ZonedDateTime nextExecution = executionTime.nextExecution(now);
// Time from last execution
Duration timeFromLastExecution = executionTime.timeFromLastExecution(now);
// Time to next execution
Duration timeToNextExecution = executionTime.timeToNextExecution(now);
ライブラリ間の定数マッピング
// Map day of week value from Quartz to JodaTime
int jodatimeDayOfWeek =
ConstantsMapper.weekDayMapping(
ConstantsMapper.QUARTZ_WEEK_DAY,
ConstantsMapper.JODATIME_WEEK_DAY
);
人間のための日付と時刻のフォーマット!
htime を使用してください - Java のための人間が読める日時フォーマット! この機能は同じ jar にバンドルされていませんが、cron-utils プロジェクトの一部であり、役立つかもしれません。
// You no longer need to remember "YYYY-MM-dd KK a" patterns.
DateTimeFormatter formatter =
HDateTimeFormatBuilder
.getInstance()
.forJodaTime()
.getFormatter(Locale.US)
.forPattern("June 9, 2011");
String formattedDateTime = formatter.print(lastExecution);
// formattedDateTime will be lastExecution in "dayOfWeek, Month day, Year" format
cron-utils CLI
新しいプロジェクトを作成せずに、コンソールから直接 cron-utils を使用するためのシンプルな CLI インターフェースを提供しています! CLI はサテライトプロジェクトであり、cron-utils-cli で利用できます。
使用法: java -jar cron-utils.jar com.cronutils.cli.CronUtilsCLI --validate -f [CRON4J|QUARTZ|UNIX] -e '<cron expression>'
例: java -jar cron-utils.jar com.cronutils.cli.CronUtilsCLI --validate -f UNIX -e '* 1 * * *'
'cp' を必要としないスタンドアロン jar が必要な場合は、以下のコマンドで uber jar をビルドします:
mvn assembly:assembly -DdescriptorId=jar-with-dependencies
次に、(target ディレクトリにビルドされた) cli-utils を次のコマンドで起動します:
java -jar cron-utils-<version>-jar-with-dependencies.jar com.cronutils.cli.CronUtilsCLI --validate -f [CRON4J|QUARTZ|UNIX] -e '<cron expression>'`
貢献とサポート!
貢献を歓迎します! 以下の方法で貢献できます
私たちのページ をチェックしてください! プロジェクトの統計については、OpenHUB プロフィール を参照できます。
その他の cron-utils プロジェクト
以下の cron-utils プロジェクトもぜひご覧ください: