Reputation: 15756
There are a couple SO articles about this, but only one directly addresses this issue. However, the solution doesn't make sense for me. I'm using a straight string.
UPDATE:
This:
[PersistJobDataAfterExecution]
public class BackgroundTaskTester : IJob
{
public void Execute(IJobExecutionContext context)
{
Debug.WriteLine("Value Is: " + context.Trigger.JobDataMap["field1"] as string);
context.Trigger.JobDataMap["field1"] = DateTimeOffset.Now.ToString();
}
}
Outputs This:
Value Is:
Value Is:
Value Is:
Value Is:
Value Is:
Value Is:
Value Is:
But this:
[PersistJobDataAfterExecution]
public class BackgroundTaskTester : IJob
{
public void Execute(IJobExecutionContext context)
{
Debug.WriteLine("Value Is: " + context.JobDetail.JobDataMap["field1"] as string);
context.JobDetail.JobDataMap["field1"] = DateTimeOffset.Now.ToString();
}
}
Outputs This:
Value Is: 10/6/2014 9:26:23 AM -05:00
Value Is: 10/6/2014 9:26:28 AM -05:00
Value Is: 10/6/2014 9:26:33 AM -05:00
However, I want to store things in the Trigger. How do I get the Trigger to persist?
ORIGINAL QUESTION:
I have a class:
[PersistJobDataAfterExecution]
public class BackgroundTaskNotification : IJob
{
public void Execute(IJobExecutionContext context)
{
<see below>
}
}
The following code doesn't function as expected:
public void Execute(IJobExecutionContext context)
{
string x = context.MergedJobDataMap["field1"];
context.Put("field1", "test string");
string y = context.MergedJobDataMap["field1"];
// PROBLEM: x != y
}
I've tried context.JobDetail.JobDataMap.Put()
and also context.Trigger.JobDataMap.Put()
None of them update MergedJobDataMap.
Maybe that's OK though. There is a JobDataMap on the JobDetail object and the Trigger. What I'm trying to do is this:
public void Execute(IJobExecutionContext context)
{
string x = context.MergedJobDataMap["field1"]; //get last x
<do something with x>
context.Put("field1", x); //save updated x
}
I'm trying to do something with x and have x persist between runs.
I'm not sure if it's relevant, but I'll add that when I create the job, I actually put field1
into the trigger's JobDataMap
. This is because I have a single Job, and multiple triggers. I want the data to be stored at the tigger level.
Upvotes: 4
Views: 7959
Reputation: 1332
MergedDataMap is a combination of the TriggerDataMap and the JobDataMap (with trigger entries overriding job entries). Updating this will do nothing, since it doesn't propagate changes back to the original JobDataMap or TriggerDataMap, and it's only JobDataMap that is re-persisted.
You want to set context.JobDetail.JobDataMap["field1"]
in order for it to be persisted.
If you want to save to the Trigger datamap, you do have to do a little more work.
If you look in the IJobExecutionContext
you are given in Execute()
, you have an instance of the scheduler that started the job, and an instance of the trigger that started the job. Combine with info here:
to update the trigger as part of the job execution. Note that this updates the trigger immediately, not after the job run (as when Quartz manages your job data for you).
This can be adapted to work for the job's data map as well, and have the changes persisted immediately vs. automatically at the end of the job execution.
Upvotes: 4