JobData is not persisted after each execution in Quartz.net

僤鯓⒐⒋嵵緔 提交于 2019-12-23 16:24:42

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!