使用 Quartz 和 Spring
在我的应用程序中,需要能够根据传入的请求类型(动态)创建计划作业。
我还可以使用 Spring 创建和触发作业吗? 如果是,怎么办?
任何帮助都会有用的。
In my application there is a requirement to be able to create Scheduled Job(s) depending on the type of Request that comes in (Dynamically).
Can I still use Spring to create and trigger Jobs? If Yes, how?
Any help would be useful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
鉴于 SchedulerFactoryBean 公开了本机 Quartz Scheduler 对象,您可以将其直接连接到控制器类中,然后使用 Scheduler 对象动态创建和注册触发器和作业。
Spring 本身不能用于动态创建作业的调度,因为 Spring 的 bean 支持将用于静态配置的作业,但本机 Quartz Scheduler API 足够合理,可以单独使用(勉强)。 至于工作的触发,是 Quartz 的工作,而不是 Spring 的工作。
编辑:要么我误解了原来的问题,要么其他人都是。 其他答案都详细介绍了如何使用 Spring 静态连接一系列石英作业,但问题是如何在收到请求时动态调度作业。
Given that the SchedulerFactoryBean exposes a native Quartz Scheduler object, you can wire that directly into your controller class, and then dynamically create and register triggers and jobs with the Scheduler object.
Spring itself can't be used for the scheduling of the dynamically created jobs, since Spring's bean support will be used for statically configured jobs, but the native Quartz Scheduler API is reasonable enough to use on its own (barely). As fr triggering of the jobs, that Quartz's job, not Spring's.
edit: either I'm mis-understanding the original question, or everyone else is. The other answers all detail how to statically wire up a series of quartz jobs using Spring, but the question was how to dynamically schedule jobs as requests come in.
看看 CronTriggerBean 和 JobDetailBean。 下面模拟的“MyJob”类是 QuartzJobBean。 cron 表达式正是您所期望的,但以秒为第一个值。
Look at CronTriggerBean and JobDetailBean. The 'MyJob' class mocked up below is an instance of QuartzJobBean. The cron expression is what you'd expect, but with seconds as its first value.
这方面似乎没有太多完整的信息。 这就是我动态安排工作的方式。 当然,您可以用其他触发器替换简单触发器。
Spring beans:
要立即运行作业,请获取对调度程序和作业的引用,附加一个简单的触发器并将其放入调度程序中,如下所示:
There does not seem to be much complete information on this. This is how I schedule jobs dynamically. Of course you could replace the simple trigger with some other trigger.
Spring beans:
To run the job immediately get a reference to the scheduler and the job, attach a simple trigger and put it into the scheduler, like this:
您还可以使用
org.springframework.scheduling包中的
MethodInvokingJobDetailFactoryBean
让Spring使用Quartz触发您的bean上的方法(即您根本不需要创建任何特定于Quartz的类) .quartzYou can also get Spring to trigger methods on your beans using Quartz (i.e. youdon't need to create any Quartz-specific classes at all) using the
MethodInvokingJobDetailFactoryBean
in the packageorg.springframework.scheduling.quartz
您可以从此链接下载示例源代码
You can download sample source code from this link
一年后,我发现自己不得不做一些非常相似的事情。 谷歌搜索,我发现这个链接 它描述了通过 JobExecutionContext 从计划作业中访问应用程序上下文。 我想我将创建一个抽象类型的作业,它可以完成一些实际的作业创建,并在作业需要运行时使用原型来实际注入所需的服务。
A year later and I find myself having to something very similar. Googling around, I found this link which describes getting access to the application context from within a scheduled job through the JobExecutionContext. I think I will be creating an abstract type job that can do some of the actual job creation and use a prototype to actual inject required services when the job needs to run.
Spring 3(撰写本文时的最新版本)几乎完全支持使用注释来设置作业。
请参阅: Spring 调度参考
Spring 3 (latest version at time of writing) supports setting up jobs almost completely with annotations.
See: Spring reference on scheduling