问题
I have a job where I want to keep track of the 50 latest runs. For some reason it doesn't seem like the state is stored in my simple prototyp:
[PersistJobDataAfterExecution]
public class ApiJob : IJob
{
private const string JobRunsKey = "jobRuns";
private const int HistoryToKeep = 50;
private const string UrlKey = "Url";
public void Execute(IJobExecutionContext context)
{
var jobDataMap = context.JobDetail.JobDataMap;
var url = context.JobDetail.JobDataMap.GetString(UrlKey);
var client = new RestClient(url);
var request = new RestRequest(Method.POST);
var response = client.Execute(request);
var runs = new List<JobRun>();
if (jobDataMap.ContainsKey(JobRunsKey))
{
runs = (List<JobRun>) jobDataMap[JobRunsKey];
}
Console.WriteLine("I'm running so fast!");
runs.Insert(0, new JobRun(){Message = "Hello", Result = JobResult.Ok, TimeForRun = DateTime.UtcNow});
while (runs.Count > HistoryToKeep)
{
runs.RemoveAt(HistoryToKeep);
}
jobDataMap.Put(JobRunsKey, runs);
}
}
I try to store the new list with jobDataMap.Put(JobRunsKey, runs)
but the next time I trigger the job the key is missing from the JobDataMap
. Any suggestions?
回答1:
You probably don't have your JobRun class marked as Serializable.
This should work
[Serializable]
public class JobRun
{
public string Message ;
public string Result ;
public DateTime TimeForRun ;
}
来源:https://stackoverflow.com/questions/15836211/jobdata-is-not-persisted-after-each-execution-in-quartz-net