如何获取Quartz Job的JobKey/JobDetail [英] How to get JobKey / JobDetail of Quartz Job

查看:46
本文介绍了如何获取Quartz Job的JobKey/JobDetail的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解如何使用Quartz 2.3.2版获得工作的详细信息.

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

到目前为止,我们使用Quartz v1.0.x进行作业,我必须将其升级到最新版本.

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

在版本2.3.2中,方法 GetJobDetail()不再具有使用2个参数的构造函数...而是使用一个 JobKey 参数.

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

不幸的是,我找不到获取单个JobKey的方法.我尝试的是以下内容:

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

  • 这是实现/获取jobKey的正确方法吗?(在 var jobkey = jobkey.Single(...)行上总是只有一个Jobkey吗?
  • 是否真的没有办法先获得所有所有工作密钥?
  • 这是Quartz希望我们获得JobDetail的方式吗?还是有更好/更简单的方式?
    • 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?
    • 预先感谢

      推荐答案

      您可以只创建一个新的作业密钥(这只是一个存储作业名称和组名称的存储空间)

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

      请注意,triggerKey也是如此

      Note that it is also true for the triggerKey

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

      这篇关于如何获取Quartz Job的JobKey/JobDetail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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