Java 中的 Cron Job 第六个参数
Cron Job sixth parameter in Java
我对 Java 很陌生。作为我的第一个项目,我将使用 cron 作业调度程序。我想澄清一下日程安排。我有一个代码,每小时 运行。
CronTrigger ct = new CronTrigger("cronTrigger", "group2", "0 1/0 * * * ?");
我已经阅读了有关调度的文档,但我感到困惑
在一份文件中,我读到如下
("0 0 * * * ?")
- 第一个0表示秒
- 第二表示分钟
- 第 3 个小时
- 4 号是一个月中的哪一天
- 第 5 个月。
在一些文档中我读到 1st 表示分钟 2nd - 小时等
任何人都可以向我解释一下这个 (0 1/0 * * * 吗?) 以及 (1/0) 是什么意思?
我想每六个小时 运行 一份工作。
如果我这样给(0 */6 * * * ?
)是否会每六个小时运行?
如果你签入crontab.guru,这两个几乎是等价的:
* * * * *
* 1/0 * * *
这是因为X/Y
的意思是:从X
开始,每隔Y
。即所有 X + Yn。所以如果你说 */2
它会每 2 小时执行一次。
在这种情况下:1/0
表示 "starting from 1, every hour",因此它匹配从 1 到 23,而 *
匹配从 0 到 23。
根据您的问题,*/6
每 6 小时匹配一次,因此它会在第 0、6、12 和 18 小时恰好 运行。
关于第 6 个参数 ?
做什么的问题,I read 是:
I believe that's processed by the CronExpression class which has six
constants: minute, hour, day, month, weekday, year. Cron uses minute,
hour, day, month, weekday. The addition of the year for the yearly()
method seems to be the reason for the extra *.
所以不用通用语法
+---------------- minute (0 - 59)
| +------------- hour (0 - 23)
| | +---------- day of month (1 - 31)
| | | +------- month (1 - 12)
| | | | +---- day of week (0 - 6) (Sunday=0 or 7)
| | | | |
* * * * * command to be executed
有了 Java 你有
+----------------- minute (0 - 59)
| +-------------- hour (0 - 23)
| | +----------- day of month (1 - 31)
| | | +-------- month (1 - 12)
| | | | +----- day of week (0 - 6) (Sunday=0 or 7)
# | | | | | +-- year <-- this is extra !!
| | | | | |
* * * * * * command to be executed
最后一个参数也可以有一个值,但在您的情况下它指定 ?
。至于我在crontab.guru里面看到的意思是:
? blank (non-standard)
所以我会用 5 个常用参数正常安排它,然后在末尾添加 ?
,以便它在所有年份中 运行s。
我对 Java 很陌生。作为我的第一个项目,我将使用 cron 作业调度程序。我想澄清一下日程安排。我有一个代码,每小时 运行。
CronTrigger ct = new CronTrigger("cronTrigger", "group2", "0 1/0 * * * ?");
我已经阅读了有关调度的文档,但我感到困惑
在一份文件中,我读到如下
("0 0 * * * ?")
- 第一个0表示秒
- 第二表示分钟
- 第 3 个小时
- 4 号是一个月中的哪一天
- 第 5 个月。
在一些文档中我读到 1st 表示分钟 2nd - 小时等
任何人都可以向我解释一下这个 (0 1/0 * * * 吗?) 以及 (1/0) 是什么意思?
我想每六个小时 运行 一份工作。
如果我这样给(0 */6 * * * ?
)是否会每六个小时运行?
如果你签入crontab.guru,这两个几乎是等价的:
* * * * *
* 1/0 * * *
这是因为X/Y
的意思是:从X
开始,每隔Y
。即所有 X + Yn。所以如果你说 */2
它会每 2 小时执行一次。
在这种情况下:1/0
表示 "starting from 1, every hour",因此它匹配从 1 到 23,而 *
匹配从 0 到 23。
根据您的问题,*/6
每 6 小时匹配一次,因此它会在第 0、6、12 和 18 小时恰好 运行。
关于第 6 个参数 ?
做什么的问题,I read 是:
I believe that's processed by the CronExpression class which has six constants: minute, hour, day, month, weekday, year. Cron uses minute, hour, day, month, weekday. The addition of the year for the yearly() method seems to be the reason for the extra *.
所以不用通用语法
+---------------- minute (0 - 59)
| +------------- hour (0 - 23)
| | +---------- day of month (1 - 31)
| | | +------- month (1 - 12)
| | | | +---- day of week (0 - 6) (Sunday=0 or 7)
| | | | |
* * * * * command to be executed
有了 Java 你有
+----------------- minute (0 - 59)
| +-------------- hour (0 - 23)
| | +----------- day of month (1 - 31)
| | | +-------- month (1 - 12)
| | | | +----- day of week (0 - 6) (Sunday=0 or 7)
# | | | | | +-- year <-- this is extra !!
| | | | | |
* * * * * * command to be executed
最后一个参数也可以有一个值,但在您的情况下它指定 ?
。至于我在crontab.guru里面看到的意思是:
? blank (non-standard)
所以我会用 5 个常用参数正常安排它,然后在末尾添加 ?
,以便它在所有年份中 运行s。