我有一份工作,我想跟踪最近的 50 次运行。由于某种原因,状态似乎没有存储在我的简单原型中:
[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);
}
}
我尝试使用存储新列表,jobDataMap.Put(JobRunsKey, runs)
但下次触发作业时,密钥从JobDataMap
. 有什么建议么?