Java Quartz Cron 触发器
我在我的 java 程序中使用 Quartz Cron 触发器。我使用的 Cron 表达式是 0 0 * * * ?
现在我想要一个调度程序每小时执行一次。为了测试相同的情况,启动了我的 jboss 服务器,之后我将 Windows 时间重置为 5:59:45 之类的值,并且在 6:00:00 时调度程序没有触发。
现在,下次我第一次将时间调整为 5:55:00,然后启动服务器,发现调度程序在 6:00:00 触发。
服务器或调度程序是否在内部维护时间,当然最初是用系统计时初始化的?
I am using a Quartz Cron trigger in my java program. The Cron expression I used is 0 0 * * * ?
Now I wanted a scheduler to execute once an hour. To test the same started my jboss server and after that I reset my windows time to something like 5:59:45 and at 6:00:00 the scheduler did not fire.
Now the next time I first adjusted the time to say 5:55:00 and then I started the server and lo the scheduler fired at 6:00:00.
Does the server or scheduler maintain time internally, of course initialized with system timing initially ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据Quartz CronTrigger源码,触发器保留了下次触发的日期。
我假设如果您在 6:15:00 启动服务器,Quartz 会保留 7:00:00 作为下一次触发时间。将服务器时间更改为 5:55:45 不会更新下一次触发时间,并且 7:00:00 会保留为下一次触发时间。因此该事件不会在 6:00:00 触发,而是在 7:00:00 触发。
因此,时间不会直接保留,但会保留触发时间,并且如果您更改系统时钟,它似乎不会更新。
According to Quartz CronTrigger source code, the trigger retains the date of the next time it should fire.
I suppose that if you start the server at 6:15:00, Quartz retains 7:00:00 as the next firing time. Changing the server time to 5:55:45 doesn't update the next firing time, and 7:00:00 is retained as next firing time. So the event won't fire at 6:00:00, but instead at 7:00:00.
So, time isn't retained directly, but firing time is retained, and it appears it isn't updated if you change your system clock.
如果您使用 JDBC 作业存储,quartz 会使用当前系统时间以一分钟的间隔检查数据库,查找要立即运行的作业。
当您向前移动系统时间时,quartz 将在下一次检查时(即在一分钟内)触发适当的作业。
If you use JDBC job store, quartz checks database in one-minute intervals looking for jobs to run for now, using the current system time.
When you move forward the system time, quartz will fire appropriate jobs on next check (that means, within minute).
我也面临同样的问题,这个问题的解决方案是:
表达式意味着0 0 * * * ?即秒、分钟、小时、月份中的某一天、月份、星期几。因此,无论是哪一年,每天、每月、每周的每个小时开始时都会点火。
这个表达式只是表明您想要在每小时开始时解雇您的工作,因此它会记录服务器启动之前的时间,因此如果在服务器启动之前时间传递到任何小时时间,那么它将在下一个小时时间触发,例如-
1 .您在 5:55 启动服务器,但服务器花了 7 分钟才启动,因此时间比 6:00 多了 2 分钟,所以下一次触发时间将是 7:00
2 。但是,如果您的服务器在 5:59:59 成功启动,那么它肯定会在 6:00 触发,因为调度程序应该在每小时开始时运行,并且服务器在 6:00 之前准备就绪。
注意:服务器启动后 Windows 上的时间更改不会使调度程序运行,因为时间是由服务器运行时间记录的。
I also faced the same problem and solution to this problem is:
expression means 0 0 * * * ? i.e. sec, min, hour, day of month, month,day of week. so fire at every start of the hour on every day and every month and every week and whatever be the year that be.
this expression simply states that you want to fire your job and every hour on the start of the hour so it logs the time before your server starts thus if before server start the time passed to any hour time then it will fire on next hour time eg-
1 . you started server at 5:55 but server took 7 min to start thus time is passed 2 min more than 6:00 so next fire time will be 7:00
2 . but if, your server starts successfully at 5:59:59 then it will definately fire at 6:00 for sure because scheduler should run on the start of every hour and server is ready before the 6:00 clock.
note: time changing on windows after the server started will not make the scheduler run as the time is logged by the time server runs.
支持 Vivien Barousse 的回答,石英将存储每个触发器的下一次触发时间。如果您使用 JDBC JobStore,您可以在数据库中找到一个类似于 *_QRTZ_TRIGGERS 的表,并且 COLUMN NEXT_FIRE_TIME 以时间戳格式存储下一次触发时间。
Upvote for Vivien Barousse's answer, quartz will store every trigger's next fire time. If you are using JDBC JobStore, you can find a table looks like *_QRTZ_TRIGGERS in your database , and COLUMN NEXT_FIRE_TIME stores next fire time in timestamp format .