石英、Unity 和。网
是否可以注册一个quartz作业来始终使用由Unity DI容器注入的相同的IJob
实例?我有一个来自 Unity DI 的类 Monitor
的单个实例“监视器”,我将其注册为:
container.RegisterType<IMonitor, Monitor>(new ContainerControlledLifetimeManager())
并且我的 IJob
实现期望将该监视器实例注入其中:
class MyJob : IJob {
...
[Dependency] IMonitor monitor {get; set;}
...
void Execute()
...
}
但是当quartz 事件触发时,会在注入依赖项之前调用 IJob.Execute() 实现。我应该如何让它发挥作用?我应该考虑其他 DI 容器或调度程序吗?
谢谢
Is it possible to register a quartz job to always use the same IJob
instance injected by DI container Unity? I have a single instance "monitor" of a class Monitor
coming from Unity DI, wich I registered as:
container.RegisterType<IMonitor, Monitor>(new ContainerControlledLifetimeManager())
and my IJob
implementation expects to have that monitor instance injected into it:
class MyJob : IJob {
...
[Dependency] IMonitor monitor {get; set;}
...
void Execute()
...
}
but when quartz events fire, the IJob.Execute()
implementation is called before the dependency gets injected. How should I get this working? Should I consider other DI containers or Schedulers instead ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Quartz 将在每次火灾事件时重新实例化作业接口实现。如果您想保留状态,建议使用
IStatefulJob
作业执行之间:来自 Quartz 教程:
您的另一个选择是实现您自己的 JobFactory:
Quartz will reinstantiate job interface implementation on every fire event. It is recommended to use
IStatefulJob
if you want to preserve state between job executions:From Quartz tutorial:
Another option for you is to implement your own JobFactory:
看看 Quartz.Unity。
https://www.nuget.org/packages/Quartz.Unity/1.0.1
文档非常稀疏,但看起来您需要做的就是将 nuget 包和以下行添加到容器配置中。
Have a look at Quartz.Unity.
https://www.nuget.org/packages/Quartz.Unity/1.0.1
Doc is very sparse, but it appears that all you need to do is add the nuget package and the following line to your container configuration.
您可以通过实现自己的 JobFactory 来做到这一点。您必须实现 IJobFactory 接口:
然后,将调度程序的quartz.scheduler.jobFactory.type 属性设置为作业工厂的类型。
作为参考,这是quartz.net 使用的默认作业工厂:
有趣的行是:
You can do this by implementing your own JobFactory. You'll have to implement the IJobFactory interface:
Then, set the scheduler's quartz.scheduler.jobFactory.type property to your job factory's type.
For reference, here's the default job factory that quartz.net uses:
The interesting line is:
创建一个覆盖 SimpleJobFactory 的 CustomJobfactory 并使用 spring 实例化作业类。
Create a CustomJobfactory that overrides SimpleJobFactory and use spring to instantiate the job classes.