1

我有一个 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" 

正如我所料。我究竟做错了什么?

4

2 回答 2

2

触发器和作业都有jobData。

LineTaskScheduler.Scheduler.TriggerJob(new Quartz.JobKey("JobName", "JobGroup"), jobData); 将 jobData 分配给触发器。您可以在 context.Trigger.JobDataMap["SomeKey"] 中看到“SomeValue”

于 2013-02-07T18:29:33.647 回答
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";
}
于 2013-07-31T15:56:34.653 回答