如何处理用户时区,以节省cron触发器石英的日光? [英] how to handle user timezone for daylight savings in quartz for cron Triggers?
问题描述
我的服务api接受startDate(用于石英作业)和要执行作业的月份。
在内部,我将其转换为cron表达式并保存为石英。
My service api takes in startDate for quartz Job and day of Month for the job to executed. Internally, I convert this to cron expression and save in quartz.
例如,PST中的用户今天(2017年11月3日)以
For example, a user in PST submits a job request today (Nov 3 2017) as follows.
{
"start": "2017-11-03T18:00:00-07:00",
"dayOfMonth" : 15
}
此处用户要安排从2017-11-03开始,在每月的15日下午6点解雇的工作。因此第一天的石英将在2017年11月15日发射。
这就是上述请求如何转换为cron表达式 0 0 18 15 *? *
,这是正确的。
这就是QRTZ_CRON_TRIGGERS表的样子。
Here the user wants to schedule a job that fires on 15th of each month at 6 PM, starting from 2017-11-03. so the first-day quartz will fire will be 2017-11-15.
This is how the above request gets converted to cron expression 0 0 18 15 * ? *
, which is correct.
Here is how, QRTZ_CRON_TRIGGERS table looks like.
您注意到,time_zone_id已保存为GMT-07:00,但是一旦11月5日开始实行夏令时,则必须为GMT-08:00。否则我的石英作业将提前一小时解雇。实际上,当我查询 nextFireTime 时,我确实得到了1510794000000,这确实是 2017年11月15日,星期三,美国/洛杉矶(PST)时区
As you notice, time_zone_id is saved as GMT-07:00, but once daylight savings kick-in on Nov 5, it must be GMT-08:00. or else my quartz job will fire one hour earlier. In fact, when I query for nextFireTime, I do get 1510794000000 which is indeed Wednesday November 15, 2017 17:00:00 (pm) in time zone America/Los Angeles (PST)
我们如何处理time_zone_id问题?
how do we handle this time_zone_id issue?
PS:我使用的是cronTrigger,它不具有由 CalendarIntervalTrigger
提供的 preserveHourOfDayAcrossDaylightSavings
的概念。
P.S: I am using cronTrigger which does not have the notion of preserveHourOfDayAcrossDaylightSavings
that is provided by CalendarIntervalTrigger
.
推荐答案
请勿使用offset表示时区。相反,您可以要求用户传递诸如 美国/洛杉矶之类的时区。然后,您可以使用 http://www.quartz-scheduler.org/api/2.2.1/org/quartz/CronScheduleBuilder.html#inTimeZone(java.util.TimeZone)创建具有适当时区的触发器。
Do not use offset to represent the timezone. Rather you can ask the user to pass in timezone like "America/Los_Angeles". Then you can use http://www.quartz-scheduler.org/api/2.2.1/org/quartz/CronScheduleBuilder.html#inTimeZone(java.util.TimeZone) to create trigger with proper timezone.
inTimeZone(TimeZone.getTimeZone("USER_ENTERED_VALUE")
最后,当您查看QRTZ_CRON_TRIGGERS表时,TIME_ZONE_ID的值为 America / Los_Angeles
Finally when you look at QRTZ_CRON_TRIGGERS table, the value for TIME_ZONE_ID will be America/Los_Angeles
这篇关于如何处理用户时区,以节省cron触发器石英的日光?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!