如何使用特定位置的时区来计划作业,而不是UTC

时间:2016-08-24 18:43:26

标签: quartz-scheduler quartz.net

我第一次使用Quartz.net调度程序。我尝试根据位置时区为不同位置运行特定流程。我想动态地将时间表和位置详细信息传递给调度程序。我怎样才能做到这一点。这里的任何帮助非常感谢。以下是我现在的代码。我现在的问题是,如果执行以下所有代码的for循环,我一次只能处理一条记录,这将达到性能,或者如果我只是在for循环中包含数据映射逻辑,那么最后一条记录将是调度程序实际启动时发送,我不知道如何将时间传递给触发器,因为它已经不在循环中。

public ISchedulerFactory objSchedulerFact;
        private IScheduler objScheduler;

protected override void OnStart(string[] args)
        {


            objScheduler =  objSchedulerFact.GetScheduler();

          IJobDetail objEslJobDet = JobBuilder.Create<ESLPriceAuditProcessor>()
                .WithIdentity("ESLJob", "ESL")
                .Build();

            objEslJobDet.JobDataMap.Put("Location", 100); // Pass the value here dynamically by taking the data from a table

            IJobDetail objSignJobDet = JobBuilder.Create<SignPriceAuditProcessor>()
                .WithIdentity("SignJob", "Sign")
                .Build();
            objSignJobDet.JobDataMap.Put("Location", 200);

            ITrigger objESLJobTrigger = TriggerBuilder.Create()
                .WithIdentity("ESLTrigger", "ESL")
                .StartAt(DateTime.Today.AddHours(2)) //Pass the time hours dynamically. I wanted execute the process for the given location based on the location time, say 4 AM at their respective timezone. Example, if i'm running the code on the server in CST time zone and I want to run this process for a EST location at current time CST Time + 2 hours and so on. I can configure the number of hours difference in the database based on the location, but how can I pass that dynamically.   
                .EndAt(DateTime.Today.AddHours(22))
                .WithSchedule(SimpleScheduleBuilder.Create().WithIntervalInHours(4))
                .Build();

            ITrigger objSignJobTrigger = TriggerBuilder.Create()
                .WithIdentity("SignTrigger", "SIGN")
                .StartAt(DateTime.Today.AddHours(4)) 
                .EndAt(DateTime.Today.AddHours(22))
                .WithSchedule(SimpleScheduleBuilder.Create().WithIntervalInHours(4))
                .Build();


            objScheduler.ScheduleJob(objEslJobDet, objESLJobTrigger);
            objScheduler.ScheduleJob(objSignJobDet, objSignJobTrigger);
            objScheduler.Start();
        }

1 个答案:

答案 0 :(得分:0)

您应该阅读this SO回答。要点是

  

您真的永远不应该依赖运行代码的服务器的时区。服务器代码应该是时区中立的。您应该参与DateTime.Now或TimeZoneInfo.Local或任何类似功能的唯一地方是桌面或移动应用程序。服务器代码应仅依赖于UTC。

由于时区偏移不是常数,许多时区会为夏令时(a.k.a。“夏令时”)切换偏移。