如何获取正在执行的作业的名称?

发布于 2024-10-12 12:27:43 字数 733 浏览 6 评论 0原文

在我的应用程序中,我有一个静态类(单例),需要使用在我的各层中使用的一些环境变量进行初始化,我将其称为我的 applicationContext。这反过来又有客户和用户的背景。

当每个作业运行时,它会根据情况修改这些客户和用户上下文。 我遇到的问题是,当两个作业同时触发时,它们可能会覆盖彼此的上下文,因此我需要为每个正在运行的作业保持多个用户和客户上下文处于活动状态,并且我需要能够通过某种方式看到正确的上下文目前的工作是什么。

是否有可能以某种方式获取有关当前正在执行的quartz.net 作业的信息?

我正在设想这样的事情,其中​​“currentQuartzJob.Name”是由组成的,并且是我所缺少的部分:

public class MyContextImpl : IApplicationContext {
private Dictionary<string,subContexts> allCustomerContexts;
public string CurrentContext
{
    get { return allCustomerContexts[currentQuartzJob.Name] };
}

}

编辑:

我认为不可能做我想做的事,即能够获得执行不了解 Quartz.Net 的类中的作业名称。

我真正需要的是一种为每项工作保留不同背景的方法。我设法通过查看执行线程的 ID 来做到这一点,因为它们对于每个正在运行的作业似乎都不同。

In my application I have a static class (singleton) that needs to be initialized with some environmental variables that's used through out my layers, I'm calling it my applicationContext. That in turn has customer and user contexts.

As each job runs it modifies these customer and user contexts depending on the situation.
The problem I have is that when 2 jobs fires concurrently they might overwrite each others contexts, therefor I need to keep multiple user and customer contexts alive for each running job and I need to be able to pick the right context by somehow being able to see what the current job is.

Is it possible to somehow get information about the current executing quartz.net job?

I'm envisioning something like this where "currentQuartzJob.Name" is made up and is the part I'm missing:

public class MyContextImpl : IApplicationContext {
private Dictionary<string,subContexts> allCustomerContexts;
public string CurrentContext
{
    get { return allCustomerContexts[currentQuartzJob.Name] };
}

}

edit:

I don't think it's possible to do what I wanted, that is to be able to get the executing job's name in a class that doesn't know about Quartz.Net.

What I really needed was a way to keep a different context for each job. I managed to do that by looking at the executing thread's ID as they seem to differ for each running job.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼前雾蒙蒙 2024-10-19 12:27:43

试试这个:

public void Execute(IJobExecutionContext context)
{
    var yourJobName = context.JobDetail.Key.Name;
}

Try this:

public void Execute(IJobExecutionContext context)
{
    var yourJobName = context.JobDetail.Key.Name;
}
毁虫ゝ 2024-10-19 12:27:43

鉴于您上面的陈述:“我遇到的问题是,当 2 个作业同时触发时,它们可能会覆盖彼此的上下文”,您可能希望通过确保作业不会同时触发来降低应用程序的复杂性。这可以通过实现 IStatefulJob 接口而不是通常的 IJob 接口来实现: http://quartznet.sourceforge .net/tutorial/lesson_3.html

或者,如果这不是一个选项,您可以通过 Ischeduler.GetCurrentlyExecutingJobs() 方法查询当前正在执行的作业的 Scheduler 对象。此方法返回这些作业的 IList,但在使用该方法(从版本 1.0.3 API 开始)时请注意以下备注:

  • 此方法不支持集群。也就是说,它只会返回当前在该 Scheduler 实例中执行的作业,而不是在整个集群中返回。
  • 请注意,返回的列表是“瞬时”快照,一旦返回,执行作业的真实列表可能会有所不同。另请阅读与 JobExecutionContext 相关的文档 - 特别是如果您使用远程处理。

Given your statement above: "The problem I have is that when 2 jobs fires concurrently they might overwrite each others contexts", you may want to reduce the complexity of your application by making sure that jobs do not fire concurrently. This can be achieved by implementing the IStatefulJob interface instead of the usual IJob interface: http://quartznet.sourceforge.net/tutorial/lesson_3.html

Alternatively if that is not an option you can query the Scheduler object for the currently executing jobs via the Ischeduler.GetCurrentlyExecutingJobs() method. This method returns an IList of those jobs but note the following remarks when using that method (from version 1.0.3 API):

  • This method is not cluster aware. That is, it will only return Jobs currently executing in this Scheduler instance, not across the entire cluster.
  • Note that the list returned is an 'instantaneous' snap-shot, and that as soon as it's returned, the true list of executing jobs may be different. Also please read the doc associated with JobExecutionContext- especially if you're using remoting.
墨小墨 2024-10-19 12:27:43

我不完全确定您正在使用应用程序做什么,但我会说当前正在执行的作业的名称在作业执行期间肯定可用。

IJob Execute() 方法内部:

// implementation in IJob
public void Execute(JobExecutionContext context)
{
   // get name here
   string jobName = context.JobDetail.Name;

}

这是您要寻找的吗?

I'm not entirely sure of what you're doing with your application, but I will say that the name of the currently executing job is definitely available during job execution.

Inside the IJob Execute() method:

// implementation in IJob
public void Execute(JobExecutionContext context)
{
   // get name here
   string jobName = context.JobDetail.Name;

}

Is that what you're looking for?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文