如何在 Quartz.NET 中使用 Job 的属性而不是 JobDataMap 字典?
我正在开发一项 Windows 服务,该服务需要在调用其中一个 Web 服务时安排任务。在最坏的情况下,这种情况每秒可能发生数百次。该任务需要等待一段时间,通常是一两分钟,然后调用传递参数的方法。
我们尝试构建自己的调度程序类来执行此操作:
public void ScheduleTask<T>(TimeSpan delay, Action<T> task, T arg)
{
Thread.Sleep(delay);
threadPool.ExecuteAsync(task, arg);
}
但我们认为这不合适,因为理论上我们可能会导致数百个线程池线程都在等待。我的印象是,可用的线程池线程数量有限,这可能会锁定系统。
然后我转向 Quartz.NET 并在他们的功能页面上阅读到:
作业类实例可以由 Quartz.NET 或应用程序的框架实例化。
在教程第 3 页中,Scheduler 创建了您的 Job 类的实例(而不是您) )因此:
在作业类上定义数据成员是没有意义的,因为每次作业执行时它们的值都会被“清除”。
请随意对我大喊大叫,但是如何在执行之前获取对 Job 类实例的引用,以便我可以在其上设置属性?
- 该属性正在执行 参数所以我对它不感兴趣 作业执行后。
- 我也想尽量减少数量 实现这一目标所需的对象 保持我的代码整洁和简单。
- 最后,我非常不喜欢使用 因此字典更愿意避免使用 JobDataMap 对象。
I am working on a Windows service which needs to schedule tasks whenever one of it's web services is called. This could happen hundreds of times per second in a worst case scenario. The task needs to wait a period of time, typically a minute or two, then call a method passing a parameter.
We tried to build our own scheduler class to do this:
public void ScheduleTask<T>(TimeSpan delay, Action<T> task, T arg)
{
Thread.Sleep(delay);
threadPool.ExecuteAsync(task, arg);
}
But we figured this wouldn't be appropriate because we could theoretically end up with hundreds of Thread Pool threads all waiting. I am under the impression that there is a finite number of Thread Pool threads available and that this could potentially lock up the system.
I then turned to Quartz.NET and read on their features page that:
Job class instances can be instantiated by Quartz.NET, or by your application's framework.
and on page 3 of their tutorial that the Scheduler creates instances of your Job class (not you) and as such:
it does not make sense to have data-members defined on the job class as their values would be 'cleared' every time the job executes.
Feel free to yell at me, but how do I get a reference to my Job class instance before it executes so I can set a property on it?
- The property is doing the job of a
parameter so i have no interest in it
after the Job has executed. - I also want to minimise the number of
objects it takes to acheive this to
keep my code neat and simple. - Finally, I seriously dislike using
Dictionaries so would prefer to avoid the JobDataMap object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不明白您的用例是什么以及为什么您需要在作业上设置属性,但要回答您的问题:要在作业执行之前访问您的作业,您需要创建一个作业侦听器(实现 IJobListener) 。作业侦听器在作业执行之前被调用,因此您可以在此时设置一个属性。
一些链接:
有关作业侦听器的文档
我写了一篇博客文章,详细介绍了侦听器的创建,此处。
I don't understand exactly what your use case is and why you would need to set a property on the job, but to answer your question: to get access to your job before it executes you need to create a job listener (implement IJobListener). The job listener gets called just before the job gets executed, so you could set a property at that point.
Some links:
The documentation on job listeners
I wrote a blog post detailing the creation of listeners, here.