[英]how to access any job properties using NameValueCollection in Quartz.Net?
我有一個關於Quartz.net的大項目。 我被創造了一些工作來完成一些任務。 所以我做了一個總結項目,以了解我。 我有工作。 我想解析一些值到作業的屬性。 在執行時間上,作業需要這些屬性。 但是我做不到。 請不要說“為什么不使用'JobDetail.JobDataMap'?” 我需要下面的結構:
看一下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);
我必須同意@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);
}
}
您從哪里得知這可行? 我還沒有看到這樣的例子。
我可能是錯的,但據我了解,傳遞給調度程序的NameValueCollection屬性是用於配置調度程序本身工作方式的設置。 例如,如果您希望調度程序使用sql數據庫,則可以(除其他事項外)將quartz.jobStore.type的值設置為“ Quartz.Impl.AdoJobStore.JobStoreTX,Quartz”。
我假設如果您傳遞了調度程序不知道的任何其他屬性,它們將被簡單地忽略。
特別是,似乎沒有任何方法可以訪問調度程序上的屬性,這再次表明僅需要/用於初始化調度程序。
請不要說“為什么不使用'JobDetail.JobDataMap'?”
JobDataMap似乎正是為此目的而設計的。 如果出於某種原因它對您不起作用,則最好解釋一下為什么它不起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.