我有两个偶尔运行的Quartz.NET作业,我需要在它们之间传递信息。我该怎么做?
以下是设置数据的示例类:
[PersistJobDataAfterExecution]
[DisallowConcurrentExecution]
public class PushingJob : IJob
{
public void Execute(IJobExecutionContext context)
{
var keys = context.Scheduler.GetJobKeys(GroupMatcher<JobKey>.AnyGroup());
foreach (var key in keys)
{
var detail = context.Scheduler.GetJobDetail(key);
if (detail.JobType == typeof(PullingJob))
{
detail.JobDataMap.Put("Foo", "Bar");
}
}
}
}
以下是获取数据的示例:
[PersistJobDataAfterExecution]
[DisallowConcurrentExecution]
public class PullingJob : IJob
{
public void Execute(IJobExecutionContext context)
{
Console.WriteLine("Foo property = " + context.MergedJobDataMap.GetString("Foo"));
}
}
答案 0 :(得分:1)
如果您使用的是DI引擎,则可以在这两个作业实例之间共享一个实例对象,并在其中包含并发字典。
否则,即使没有DI引擎,你总是可以在它们之间共享静态并发字典(不太优雅,但它有效):)
例如,注射:
[PersistJobDataAfterExecution]
[DisallowConcurrentExecution]
public class PushingJob : IJob
{
DataObject _data;
// with injection
public PushingJob(DataObject data)
{
_data= data;
}
public void Execute(IJobExecutionContext context)
{
var keys = context.Scheduler.GetJobKeys(GroupMatcher<JobKey>.AnyGroup());
foreach (var key in keys)
{
var detail = context.Scheduler.GetJobDetail(key);
if (detail.JobType == typeof(PullingJob))
{
data.MyDict.AddOrUpdate("Foo", "Bar");
}
}
}
}
[PersistJobDataAfterExecution]
[DisallowConcurrentExecution]
public class PullingJob : IJob
{
DataObject _data;
// with injection
public PushingJob(DataObject data)
{
_data= data;
}
public void Execute(IJobExecutionContext context)
{
Console.WriteLine("Foo property = " + _data.MyDict.GetOrAdd("Foo"));
}
}