如何使用自定义jobDataMap调用TriggerJob?

时间:2013-02-07 17:02:53

标签: quartz-scheduler quartz.net

我有一个Quartz.NET工作,我按如下方式设置:

var jobKey = new JobKey("JobName", "JobGroup");
var triggerKey = new TriggerKey("JobName", "JobGroup");
var jobData = new JobDataMap();
jobData.Add("SomeKey", "OriginalValue");
var jobDetail = JobBuilder.Create<JobClass>()
                    .WithIdentity(jobKey)
                    .StoreDurably()
                    .UsingJobData(jobData)
                    .Build();
Scheduler.AddJob(jobDetail, true);
var triggerDetail = TriggerBuilder.Create()
                        .WithIdentity(triggerKey)
                        .StartNow()
                        .WithDailyTimeIntervalSchedule(x => x.OnEveryDay()
                            .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(04, 07))
                            .EndingDailyAt(TimeOfDay.HourAndMinuteOfDay(06, 07))
                            .WithMisfireHandlingInstructionFireAndProceed())
                        .ForJob(jobKey)
                        .Build();
Scheduler.ScheduleJob(triggerDetail);

我正在尝试使用以下代码手动触发该作业:

var jobData = new JobDataMap();
jobData.Add("SomeKey", "SomeValue");
TaskScheduler.Scheduler.TriggerJob(new Quartz.JobKey("JobName", "JobGroup"), jobData);

当我运行手动触发这段代码时,

中的值
context.JobDetail.JobDataMap["SomeKey"] 

"OriginalValue"

而不是

"SomeValue" 
正如我所料,

我做错了什么?

2 个答案:

答案 0 :(得分:2)

触发器和作业都有jobData。

TaskScheduler.Scheduler.TriggerJob(new Quartz.JobKey("JobName", "JobGroup"), jobData); 分配jobData来触发。你可以在context.Trigger.JobDataMap [“SomeKey”]

中看到'SomeValue'

答案 1 :(得分:0)

使用引用类型:

//A simple class used here only for storing a string value:
public class SimpleDTO
{
    public string Value { get; set; }
}

void Work() {
    var dto = new SimpleDTO();
    dto.Value = "OriginalValue";
    JobDataMap data = new JobDataMap();
    data["Key"] = dto;

    TaskScheduler.Scheduler.TriggerJob(new Quartz.JobKey("JobName", "JobGroup"), jobData);

    //read modified new value:
    var resultDto = (SimpleDTO)data["Key"];
    Assert.AreEqual("NewValue", resultDto.Value);
}

public void Execute(IJobExecutionContext context)
{
    var simpleDTO = (SimpleDTO)context.MergedJobDataMap["SomeKey"];

    //set a new value:

    simpleDTO.Value = "NewValue";
}