2012-03-08 25 views
2

時間我對工作的代碼 - 只要登錄信息數據庫消防工作只有一個特定的日期和時間

public class Job : IJob 
    { 
     private static readonly log4net.ILog log = 
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); 
     #region IJob Members 
     public void Execute(IJobExecutionContext context) 
     { 
      // This job simply prints out its job name and the 
      // date and time that it is running 
      JobKey jobKey = context.JobDetail.Key; 
      log.InfoFormat("SimpleJob says: {0} executing at {1}", 
jobKey, DateTime.Now.ToString("r")); 
     } 
     #endregion 
    } 

我單身調度類

public class Scheduler 
    { 
     static Scheduler() 
     { 
      NameValueCollection properties = new 
NameValueCollection(); 
      properties["quartz.scheduler.instanceName"] = "myApp"; 
      properties["quartz.scheduler.instanceId"] = "MyApp"; 
      properties["quartz.threadPool.type"] = 
"Quartz.Simpl.SimpleThreadPool, Quartz"; 
      properties["quartz.threadPool.threadCount"] = "10"; 
      properties["quartz.threadPool.threadPriority"] = "Normal"; 
      properties["quartz.scheduler.instanceName"] = 
"TestScheduler"; 
      properties["quartz.scheduler.instanceId"] = 
"instance_one"; 
      properties["quartz.jobStore.type"] = 
"Quartz.Impl.AdoJobStore.JobStoreTX, Quartz"; 
      properties["quartz.jobStore.useProperties"] = "true"; 
      properties["quartz.jobStore.dataSource"] = "default"; 
      properties["quartz.jobStore.tablePrefix"] = "QRTZ_"; 
      // if running MS SQL Server we need this 
      properties["quartz.jobStore.lockHandler.type"] = 
"Quartz.Impl.AdoJobStore.UpdateLockRowSemaphore, Quartz"; 
      properties["quartz.dataSource.default.connectionString"] = 
"Server=localhost;Database=quartzr;Uid=user;Pwd=pass"; 
      properties["quartz.dataSource.default.provider"] = 
"SqlServer-20"; 
      _schedulerFactory = new StdSchedulerFactory(properties); 
      _scheduler = _schedulerFactory.GetScheduler(); 
     } 
     public static IScheduler GetScheduler() 
     { 
      return _scheduler; 
     } 
     private static readonly ISchedulerFactory _schedulerFactory; 
     private static readonly IScheduler _scheduler; 
    } 

Global.asax中開始調度

void Application_Start(object sender, EventArgs e) 
     { 
      Scheduler.GetScheduler().Start(); 
     } 

並添加作業代碼

DateTime SelectedDate = this.Calendar1.SelectedDate; 
       int hour = this.TimeSelector1.Hour; 
       int minute = this.TimeSelector1.Minute; 
       int second = this.TimeSelector1.Second; 
       // First we must get a reference to a scheduler 
       // jobs can be scheduled before sched.start() has been 
called 
       // get a "nice round" time a few seconds in the 
future... 
       DateTimeOffset startTime = DateBuilder.DateOf(hour, 
minute, second, SelectedDate.Day, SelectedDate.Month, 
SelectedDate.Year); 
       try 
       { 
        // job1 will only fire once at date/time "ts" 
        IJobDetail job = JobBuilder.Create<Job>() 
         .WithIdentity("job1", "group1") 
         .Build(); 
        ISimpleTrigger trigger = 
(ISimpleTrigger)TriggerBuilder.Create() 
                    .WithIdentity("trigger1", 
"group1") 
                    .StartAt(startTime) 
                    .Build(); 
        // schedule it to run! 
        DateTimeOffset? ft = 
Scheduler.GetScheduler().ScheduleJob(job, trigger); 
        log.Info(job.Key + 
          " will run at: " + ft); 
        this.Label1.Text = "Zdarzenie dodane"; 
       } 
       catch (Exception ex) 
       { 
        log.Error(ex.Message, ex); 
       } 

問題是,喬布斯被添加到數據庫,但在具體 時間我立刻火了,不是:/我用最新的庫Quartz.NET 2.0 beta 2版本 我是否一些錯誤的代碼?我與這個API begginer,請幫助

回答

2

Quartz.Net希望你傳遞UTC的日期和時間。嘗試改變這一行:

.StartAt(startTime) 

.StartAt(startTime.ToUniversalTime()) 

,或者確保開始時間是UTC通過其在之前

+0

這是如何與WithDailyTimeIntervalSchedule傳遞DailyTimeIntervalScheduleBuilder和StartingDailyAt? – Demodave 2016-03-09 21:27:41

1

StartAt除了DateTimeOffest
石英具有DateBuilder助手類。這

var minutesToFireDouble = (futureUTCtime - DateTime.UtcNow).TotalMinutes; 
       var minutesToFire = (int)Math.Floor(minutesToFireDouble); // negative or zero value will set fire immediately 
       var timeToFire = DateBuilder.FutureDate(minutesToFire, IntervalUnit.Minute); 
       var trigger = TriggerBuilder.Create() 
              .WithIdentity("triggerC", "groupC") 
              .StartAt(timeToFire) 
              .WithSimpleSchedule(x => x.WithMisfireHandlingInstructionFireNow()) 
              .Build(); 
0

如果你已經知道UTC時間,你需要你的任務在運行,你可以做這樣的事情:

UTC開始時間目標:2017年9月1日中午12時

設置你的Quartz「開始時間」的方法如下:在UTC時間格式

var MyTaskAbcStartTime = "2017-09-01 12:00:00Z" 
.StartAt(MyTaskAbcStartTime) 

參考微軟文檔: https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings

希望這有助於節省一些時間以備將來Quartz用戶。

相關問題