2011-07-20 47 views
5

我需要一些幫助。我想弄清楚如何在Quartz.Net中安排工作。 Quartz中的作業對應於我的Web應用程序中的任務,這些任務在我的Web應用程序中分別是Job。我希望用戶能夠按需啓動作業(Web應用程序上下文),並讓它立即運行或在將來安排作業,並可能在給定時間間隔內重複。我知道所有這些項目是如何在Quartz中單獨完成的,但我很難將它們放在一起。在Quartz.Net中調度依賴作業

例如,在我的web應用程序中,我可能會按照特定的順序執行幾項任務。我希望能夠在石英中安排這些任務,以便它們以我的Web應用程序中確定的相同順序執行。有沒有人知道如何做到這一點?我已經閱讀了Quartz文檔,說在JobDataMap中存儲下一個Job,只是掙扎着。

我目前正在等待創建石英作業,直到用戶請求安排作業或運行它。你認爲我應該創建這個工作,並觸發在Web應用中創建任務,然後從任務對象中提取這些信息以便在Quartz中進行調度?

回答

4

關於第二段:如果我正確地理解了你,你有一組作業,用戶可以從中選擇並按特定順序執行。我會通過創建用戶選擇的每種作業類型的作業實例來處理它。爲了保持作業的順序,您可以將下一個作業的組名和作業名存儲在上一作業的JobDataMap中。然後,您可以擁有一個通用的JobListener,它已註冊到所有作業。偵聽器將在執行作業時收到通知,並將作業參數傳遞給作業包。 JobListener可以枚舉剛剛執行的作業的JobDataMap。如果它找到一個鍵值爲「nextjobname」的鍵值對,JobListener將查詢該作業的調度器。對工作包中也可用的調度程序的引用。如果調度程序爲給定名稱返回一個JobDetail實例,則JobListener將執行它,在完成時會收到通知,等等,直到它到達JobDataMap中的「nextjobname」作業。
另外,如果你不想有Job Listeners,你可以有一個基類的Job類來實現這個功能。所有的工作都會從這個類中派生出來,並且會覆蓋它的虛擬Execute方法。您可以在重寫實現返回之前調用base.Execute(context)。

public class Basejob : IJob 
{ 
    public virtual void Execute(JobExecutionContext context) 
    { 
     string nextJob = context.MergedJobDataMap["nextjobname"]; 
     JobDetail nextjob = context.Scheduler.GetJobDetail(context.MergedJobDataMap["nextjobname"], 
              context.MergedJobDataMap["nextjobgroupname"]); 
     if(nextjob != null) 
     { 
      context.Scheduler.ScheduleJob(nextjob, new SimpleTrigger(nextjob.Name + "trigger")); // this will fire the job immediately 
     } 
    } 
} 

public class MyJob : BaseJob 
{ 
    public override void Execute(JobExecutionContext context) 
    { 
     //do work 
     base.Execute(context); 
    } 
} 
+0

這是有道理的。你有任何JobListener或基礎Job類的例子嗎? – M4V3R1CK

+0

另一個簡單的問題是:如何將觸發器分配給通過偵聽器執行的作業?根據我的理解,爲了安排工作,您必須傳遞一個觸發器(與其相關的工作)或觸發器和工作。 – M4V3R1CK

+0

如果有人想知道,_scheduler.AddJob(JobDetail作業,布爾替換)允許您添加作業到數據存儲沒有關聯的觸發器...... DUHHHHHHHHHHHHHHHHHHH – M4V3R1CK

10

你需要的是JobChainingJobListener類,它是有幫助你在你的願望特定的順序爲您創造就業機會執行鏈..

using System; 
using System.Text; 
using Quartz; 
using Quartz.Impl; 
using Quartz.Impl.Calendar; 
using Quartz.Listener; 
using Quartz.Impl.Matchers; 
using System.Threading; 

namespace QuartzNET.Samples 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      // Create RAMJobStore instance 
      DirectSchedulerFactory.Instance.CreateVolatileScheduler(5); 
      ISchedulerFactory factory = DirectSchedulerFactory.Instance; 

      // Get scheduler and add object 
      IScheduler scheduler = factory.GetScheduler();   

      StringBuilder history = new StringBuilder("Runtime History: "); 
      scheduler.Context.Add("History", history); 

      JobKey firstJobKey = JobKey.Create("FirstJob", "Pipeline"); 
      JobKey secondJobKey = JobKey.Create("SecondJob", "Pipeline"); 
      JobKey thirdJobKey = JobKey.Create("ThirdJob", "Pipeline"); 

      // Create job and trigger 
      IJobDetail firstJob = JobBuilder.Create<FirstJob>() 
             .WithIdentity(firstJobKey) 
             //.StoreDurably(true) 
             .Build(); 

      IJobDetail secondJob = JobBuilder.Create<SecondJob>() 
             .WithIdentity(secondJobKey)          
             .StoreDurably(true)          
             .Build(); 

      IJobDetail thirdJob = JobBuilder.Create<ThirdJob>() 
             .WithIdentity(thirdJobKey) 
             .StoreDurably(true) 
             .Build(); 

      ITrigger firstJobTrigger = TriggerBuilder.Create() 
              .WithIdentity("Trigger", "Pipeline") 
              .WithSimpleSchedule(x => x 
               .WithMisfireHandlingInstructionFireNow() 
               .WithIntervalInSeconds(5)  
               .RepeatForever()) 
              .Build(); 

      JobChainingJobListener listener = new JobChainingJobListener("Pipeline Chain"); 
      listener.AddJobChainLink(firstJobKey, secondJobKey); 
      listener.AddJobChainLink(secondJobKey, thirdJobKey);    

      scheduler.ListenerManager.AddJobListener(listener, GroupMatcher<JobKey>.GroupEquals("Pipeline")); 

      // Run it all in chain 
      scheduler.Start(); 
      scheduler.ScheduleJob(firstJob, firstJobTrigger); 
      scheduler.AddJob(secondJob, false, true); 
      scheduler.AddJob(thirdJob, false, true); 

      Console.ReadLine(); 
      scheduler.Shutdown(); 
      Console.WriteLine("Scheduler shutdown."); 
      Console.WriteLine(history); 
      Console.ReadLine(); 
     } 
    } 

    class FirstJob : IJob 
    { 
     public void Execute(IJobExecutionContext context) 
     { 
      var history = context.Scheduler.Context["History"] as StringBuilder; 
      history.AppendLine(); 
      history.AppendFormat("First {0}", DateTime.Now);    
      Console.WriteLine("First {0}", DateTime.Now); 
     } 
    } 

    class SecondJob : IJob 
    { 
     public void Execute(IJobExecutionContext context) 
     { 
      var history = context.Scheduler.Context["History"] as StringBuilder; 
      history.AppendLine(); 
      history.AppendFormat("Second {0}", DateTime.Now); 
      Console.WriteLine("Second {0}", DateTime.Now);    
     } 
    } 

    class ThirdJob : IJob 
    { 
     public void Execute(IJobExecutionContext context) 
     { 
      var history = context.Scheduler.Context["History"] as StringBuilder; 
      history.AppendLine(); 
      history.AppendFormat("Third {0}", DateTime.Now); 
      Console.WriteLine("Third {0}", DateTime.Now); 
      Console.WriteLine(); 
     } 
    } 
}