How to get JobKey / JobDetail of Quartz Job

蹲街弑〆低调 提交于 2019-12-01 07:10:44

问题


I'm having trouble to understand how I can get the details of a job with Quartz version 2.3.2.

Until now, we used Quartz v1.0.x for jobs and I have to upgrade it to the latest version.

This is how we used to get the details of a job:

JobDetail job = scheduler.GetJobDetail(task.Name, groupName);

With version 2.3.2, the method GetJobDetail() doesn't have a constructor that takes 2 parameter anymore... instead, it takes a JobKey parameter.

Unfortunately I couldn't find a way to get a single JobKey. What I tried is the following:

string groupName = !string.IsNullOrEmpty(task.GroupNameExtension) ? task.GroupNameExtension : task.GroupName;
var jobkeys = quartzScheduler.GetJobKeys(GroupMatcher<JobKey>.GroupContains(groupName));
var jobkey = jobkeys.Single(x => x.Name == task.Name);
var jobDetail = quartzScheduler.GetJobDetail(jobkey);
  • Is this the correct way to implement it / get the jobKey? (will there always be only one jobkey on the line var jobkey = jobkey.Single(...)?
  • Is there really no way to get a JobDetail without getting all the JobKeys first?
  • Is this the way Quartz wants us to get the JobDetail or is there a better / simpler way?

Thanks in advance


回答1:


You can just create a new job key (which is just a fancy storage for a job name and a group name)

new JobKey("jobName", "jobGroupName");

As long as your job name and job group name is the same with which you created your job, you will be able to get your job detail.

var jobDetail = quartzScheduler.GetJobDetail(new JobKey("jobName", "jobGroupName"));

personnally, i implement a static method in my job class to centralise the job key creation so i don't have many litterals all over the place :

public static JobKey GetJobKey(EntityServer server)
{
    return new JobKey("AutoRestart" + server.Id, "AutoRestart");
}

Note that it is also true for the triggerKey

public static TriggerKey GetTriggerKey(EntityServer server)
{
    return new TriggerKey("AutoRestart" + server.Id, "AutoRestart");
}


来源:https://stackoverflow.com/questions/31161939/how-to-get-jobkey-jobdetail-of-quartz-job

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