Quartz 调度程序和作业“触发”早一秒
有人可以建议吗?我有一个工作每小时运行一次,并且由 Quartz 1.8.3 在 Java Web 应用程序中控制。
但我看到的是,Quartz 有时似乎会提前一秒钟开始工作。但由于没有明显的模式,因此很难纠正。
我的代码是:
Scheduler_Job sj = Scheduler_Job_DB.get(1);
sj.setJobRunning(Valid.TRUE);
Scheduler_Job_DB.update(sj);
SQLDateTime currPollDateTime = SQLDateTime.getSQLDateTimeNow();
currDateTimeNow 被设置为 17:59:59,而它应该是 18:00:00。
Quartz 作业的 cron 触发器是 0 0 * * * ?
方法 SQLDateTime.getSQLDateTimeNow();只需获取以毫秒为单位的当前计数并将其转换为 java.sql.Date 和 java.sql.time 元素,如下所示:
public static SQLDateTime getSQLDateTimeNow() {
long milliSecs = DateTime_Utils.getTimeInMilliseconds();
Date date = new Date(milliSecs);
Time time = new Time(milliSecs);
SQLDateTime sqldt = new SQLDateTime(Date.valueOf(date.toString()), Time.valueOf(time.toString()));
return sqldt;
}
SQLDateTime 类仅具有两个变量,如下所示:
private Date sqlDate;
private Time sqlTime;
在使用 yyyy 中存储在数据库中的日期和时间时很方便 - mm-dd HH:mm:ss 格式。
在 99.9% 的情况下,这没问题,但我不明白为什么这项工作似乎提前了一秒被解雇?
我应该补充一点,升级到 Quartz 2.x 不是一个选项。
有人有什么想法吗?
我的解决方案是保持简单。因此,如果 Quartz 作业在给定时间触发,并且如果我将作业的详细信息存储在数据库表中,那么我还可以存储当前运行时间和日期,例如 YYYY-MM-DD 18:00:00。
因此,作业会触发,它从数据库中读取包括日期和时间在内的详细信息,并进行 18:00:00 的所有更新。
然后,在作业结束时,当前运行时间和日期会增加一小时,并写回数据库以用作下一次运行日期和时间。因此,18:00:00 变为 19:00:00,并且日期保持不变,除非作业在 23:00:00 运行。
所以一切都是同步的,没有毫秒的影响。
无论如何,理论上听起来不错......
Can anyone advise? I have a job which runs on the hour, every hour, and which is controlled in a Java webapp by Quartz 1.8.3.
What I'm seeing though is that Quartz seems to start the job a second early sometimes. But as there's no apparent pattern to this it is difficult to correct.
My code is:
Scheduler_Job sj = Scheduler_Job_DB.get(1);
sj.setJobRunning(Valid.TRUE);
Scheduler_Job_DB.update(sj);
SQLDateTime currPollDateTime = SQLDateTime.getSQLDateTimeNow();
Where currDateTimeNow is being set to 17:59:59 when it should be 18:00:00.
The cron trigger for the Quartz job is 0 0 * * * ?
The method SQLDateTime.getSQLDateTimeNow(); simply gets the current count in milliseconds and converts it into java.sql.Date and java.sql.time elements as follows:
public static SQLDateTime getSQLDateTimeNow() {
long milliSecs = DateTime_Utils.getTimeInMilliseconds();
Date date = new Date(milliSecs);
Time time = new Time(milliSecs);
SQLDateTime sqldt = new SQLDateTime(Date.valueOf(date.toString()), Time.valueOf(time.toString()));
return sqldt;
}
Class SQLDateTime simply has two variables as follows:
private Date sqlDate;
private Time sqlTime;
And is convenient when using dates and times stored in the database in yyyy-mm-dd HH:mm:ss format.
In 99.9% of cases this is fine but I don't understand why the job seems to fire one second early?
I should add that upgrading to Quartz 2.x is not an option.
Any ideas anyone?
My solution is to keep it simple. So if the Quartz job fires at a given hour, and if I have the job's details stored in a database table, then I can also store the current run time and date, e.g. YYYY-MM-DD 18:00:00.
So the job fires, it reads the details including the date and time from the database, and makes all the updates for 18:00:00.
Then at the end of the job, the current run time and date get incremented by an hour and are written back to the database for use as the next run date and time. So 18:00:00 becomes 19:00:00 and the date stays the same unless the job is running at 23:00:00.
So everything is in synch and there's no playing with milliseconds.
It sounds good in theory anyway...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能是不同的时钟略有偏移,因此当石英调度程序在18:00:00 005启动时,数据库可能仍然认为其17:59:59 980< /强>。
如果这对你来说确实是个问题,你可能想让线程休眠几百毫秒,这样它总是正确的。
It could be that the different clocks are marginally offset, so when the quartz scheduler starts on 18:00:00 005 it could be that the database still thinks its 17:59:59 980.
If its really an issue to you, you might want to sleep the thread some hundred millis so its always correct.