如何获取正在执行的作业的名称?
在我的应用程序中,我有一个静态类(单例),需要使用在我的各层中使用的一些环境变量进行初始化,我将其称为我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
Try this:
鉴于您上面的陈述:“我遇到的问题是,当 2 个作业同时触发时,它们可能会覆盖彼此的上下文”,您可能希望通过确保作业不会同时触发来降低应用程序的复杂性。这可以通过实现 IStatefulJob 接口而不是通常的 IJob 接口来实现: http://quartznet.sourceforge .net/tutorial/lesson_3.html
或者,如果这不是一个选项,您可以通过 Ischeduler.GetCurrentlyExecutingJobs() 方法查询当前正在执行的作业的 Scheduler 对象。此方法返回这些作业的 IList,但在使用该方法(从版本 1.0.3 API 开始)时请注意以下备注:
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):
我不完全确定您正在使用应用程序做什么,但我会说当前正在执行的作业的名称在作业执行期间肯定可用。
IJob Execute() 方法内部:
这是您要寻找的吗?
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:
Is that what you're looking for?