2012-08-04 63 views
1

我有一個關於Quartz.net的大項目。我已經創造了一些工作來完成一些任務。所以我已經做了一個總結項目來了解我。我有工作。我想解析一些工作屬性的值。同時執行時間,工作需要這些屬性。但我不能那樣做。請不要說「爲什麼不使用'JobDetail.JobDataMap'?」我需要以下結構:如何使用Quartz.Net中的NameValueCollection訪問任何作業屬性?

enter image description here

看JobBase:(!這是我的工作)

public abstract class JobBase : MarshalByRefObject, IStatefulJob 
{ 
    void IJob.Execute(JobExecutionContext context) 
    { 
     this.Execute(); 
    } 

    protected abstract void Execute(); 
} 

也test.cs中

test.cs中:

public class Test : JobBase 
{ 

    public string FileName { get; set; } 

    public string Ip { get; set; } 


    protected override void Execute() 
    { 
     Ping ping = new Ping(); 
     PingReply pingReply = ping.Send(Ip); 
     using (System.IO.StreamWriter file = new System.IO.StreamWriter(FileName, true)) 
     { 
      file.WriteLine(pingReply.Address); 
     } 
    } 
} 

我的項目開始時間表:

private void btnProperties_Click(object sender, EventArgs e) 
    { 
     ISchedulerFactory schedfabrikayeni; 
     IScheduler schedyeni; 
     JobDetail job; 
     CronTrigger trigeryeni; 
     NameValueCollection properties = new NameValueCollection(); 
     properties["FileName"] = @"C:\temp\pingresult.txt"; 
     properties["Ip"] = "192.168.16.14"; 


     schedfabrikayeni = new StdSchedulerFactory(properties); 
     schedyeni = schedfabrikayeni.GetScheduler(); 
     job = new JobDetail("myJob", null, typeof(Test)); 

     JobDataMap map = new JobDataMap(); 
     map.Put("msg", "Your remotely added job has executed!"); 
     job.JobDataMap = map; 
     string cronExpressiontxt = string.Empty; 
     //0 0 12 1 4 ? * 
     cronExpressiontxt = "0 0/1 * 1/1 * ? *"; 
     trigeryeni = new CronTrigger("triger1", null, "myJob", null, cronExpressiontxt); 

     schedyeni.ScheduleJob(job, trigeryeni); 
     schedyeni.Start(); 
    } 

但我的IP爲空我的FileName爲空。如何使用下面的用法設置屬性:

NameValueCollection properties = new NameValueCollection(); 
     properties["FileName"] = @"C:\temp\pingresult.txt"; 
     properties["Ip"] = "192.168.16.14"; 


     schedfabrikayeni = new StdSchedulerFactory(properties); 

回答

1

我必須同意@jvilalta和@sgmoore,您需要使用JobDataMap來存儲有狀態數據。以下是我如何使用JobDataMap屬性的示例:

protected override void ExecuteWorker(IJobExecutionContext context) 
    { 
     try 
     { 
      SomeProcessor someProcessor; 

      // If the JobDataMap doesn't contain the initialized key yet, then this job hasn't been run before. 
      // Initialize state data if this is the case; otherwise, get state data from the JobDataMap. 
      if (!context.JobDetail.JobDataMap.Contains(QUARTZ_KEY_PROCESSOR)) 
      { 
       someProcessor = someProcessorFactory.Create(); 

       if (someProcessor == null) 
        return; 

       context.JobDetail.JobDataMap[QUARTZ_KEY_PROCESSOR] = someProcessor; 
      } 
      else 
      { 
       someProcessor = (someProcessor)context.JobDetail.JobDataMap[QUARTZ_KEY_PROCESSOR]; 
      } 

      ExecuteETL(someProcessor, DateTime.MaxValue); 
     } 
     catch (Exception e) 
     { 
      m_log.Fatal("Scheduled job execution failed.", e); 
      // This method can only throw a JobExecutionException. 
      // http://quartznet.sourceforge.net/tutorial/lesson_3.html 
      throw new JobExecutionException(e); 
     } 
    } 
0

你從哪裏得到這樣的想法?我沒有看到任何這樣的例子。

我可能是錯的,但據我瞭解,您傳遞給調度程序的NameValueCollection屬性是用於配置調度程序本身如何工作的設置。例如,如果您希望調度程序使用sql數據庫,則可以將quartz.jobStore.type的值設置爲「Quartz.Impl.AdoJobStore.JobStoreTX,Quartz」(除其他外)。

我認爲如果您傳遞了調度程序不知道的任何額外屬性,他們會簡單地被忽略。

特別是,似乎沒有任何方法可以訪問調度程序中的屬性,這將再次表明只需要/用於初始化調度程序。

請不要說「爲什麼不使用'JobDetail.JobDataMap'?」

JobDataMap似乎是專門爲此目的而設計的。如果出於某種原因,它不適合你,你最好解釋爲什麼它不起作用。

0

您可以通過實現自定義調度程序工廠和/或自定義作業工廠來實現。默認調度程序和作業工廠不支持像您所描述的那樣設置作業屬性。 @sgmoore指出,JobDataMap是將參數傳遞給作業的正確方法。

FWIW,你所擁有的代碼將不起作用。調度程序本身需要作爲一個單例進行管理,只要調度程序需要啓動並運行,就需要維護調度程序。否則它將被垃圾收集。

這裏有一些posts I wrote,如果你決定實施一個工作的工廠,可能會感興趣。

相關問題