在 Quartz.net 中每次执行后 JobData 不会持久化 [英] JobData is not persisted after each execution in Quartz.net

查看:69
本文介绍了在 Quartz.net 中每次执行后 JobData 不会持久化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一份工作,我想跟踪最近的 50 次运行.出于某种原因,状态似乎没有存储在我的简单原型中:

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);
     }
}

我尝试使用 jobDataMap.Put(JobRunsKey, runs) 存储新列表,但是下次我触发作业时,JobDataMap 中缺少键.有什么建议吗?

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?

推荐答案

您可能没有将 JobRun 类标记为 Serializable.

You probably don't have your JobRun class marked as Serializable.

这应该有效

[Serializable]
public class JobRun
{
    public string Message       ;
    public string Result        ;
    public DateTime TimeForRun  ;
}

这篇关于在 Quartz.net 中每次执行后 JobData 不会持久化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆