如何在 Quartz.Net 作业中创建对实例化对象的引用?
我有一个嵌入 Quartz.Net 的 Windows 服务,但似乎找不到一种方法来创建对 Quartz.Net 作业中实例化对象的引用...
当 Windows 服务启动时,它会实例化一些用于日志记录、数据库的对象访问和其他目的,所以我希望我的 Quartz.Net 作业使用这些已经实例化的对象,而不是创建这些对象自己的实例。然而,Quartz.Net 作业是由调度程序使用无参数构造函数实例化的,因此无法使用构造函数传递引用。
我是否必须创建自己的 JobFactory 实现,这是实现此目的的唯一方法吗?
I have a windows service with embedded Quartz.Net but can't seem to find a way to create a reference to an instantiated object within a Quartz.Net job...
When the windows service is started it instantiates some objects for logging, database access and other purposes so I'd like my Quartz.Net jobs to use these already instantiated objects instead of creating its own instances of these objects. However, Quartz.Net jobs are instantiated by the scheduler using the no-argument constructor and hence there is no way to pass a reference using the constructor.
Do I have to create my own implementation of the JobFactory and is that the only way to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为适合这种情况的方法是使用 作业监听器。
您可以创建一个不执行任何操作的“虚拟”作业,以及一个检测作业何时运行的侦听器。您可以使用对任何依赖项的引用来实例化侦听器,只要它们在您设置作业调度时可用即可。
I think the approach that works for this situation is to use a job listener.
You can create a "dummy" job which does nothing, and a listener which detects when the job has been run. You can instantiate the listener with references to any dependencies, as long as they are available at the time you set up the job scheduling.
您可以在
jobDetail.JobDataMap
中添加对象的键值对,并从 (JobExecutionContext) context.JobDetail.JobDataMap
检索它们。You can add key-value pairs of objects in
jobDetail.JobDataMap
and retrieve themfrom(JobExecutionContext) context.JobDetail.JobDataMap
.不同的上下文(Linux/JAVA),但创建您自己的继承自 Quartz 的工厂。重写方法“createScheduler”。调用 super 方法,将实例保存在静态(同步)哈希映射中。编写静态方法以按名称获取实例。
Different context (Linux/JAVA), but create your own Factory that inherits from the Quartz one. Override the method "createScheduler". Call the super method, save instance in a static (synchronized) hash map. Write static method to get instance by name.