使用 Quartz 仅运行一次作业
有没有一种方法可以让我在 Java 中使用 Quartz 只运行一次作业?我知道在这种情况下使用 Quartz 是没有意义的。但是,问题是,我有多个工作,并且它们运行多次。所以,我正在使用石英。
这可能吗?
Is there a way I could run a job only once using Quartz in Java? I understand it does not make sense to use Quartz in this case. But, the thing is, I have multiple jobs and they are run multiple times. So, I am using Quartz.
Is this even possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您应该使用在特定时间触发且不重复的 SimpleTrigger。 TriggerUtils 有许多方便的方法来创建此类东西。
You should use SimpleTrigger that fires at specific time and without repeating. TriggerUtils has many handy methods for creating these kind of things.
是的,这是可能的!
Yes, it's possible!
在石英> 2.0 中,您可以让调度程序在工作完成后取消调度任何作业:
其中,triggerKey 是仅运行一次的作业的 ID。此后,该工作将不再被调用。
In quartz > 2.0, you can get the scheduler to unschedule any job after work is done:
where triggerKey is the ID of the job to run only once. After this, the job wouldn't be called anymore.
以下是如何使用 Quartz 2.x 立即运行
TestJob
类的示例:另请参阅 Quartz Enterprise Job Scheduler 教程 → 简单触发器
Here is an example of how to run a
TestJob
class immediately with Quartz 2.x:see also Quartz Enterprise Job Scheduler Tutorials → SimpleTriggers
我不确定 Mono 和 Java 中的 Quartz 有多少相似之处,但这似乎在 .Net 中有效
I'm not sure how much similar is Quartz in Mono and Java but this seems working in .Net
我不得不问自己,尝试配置一个作业并添加检查是否已经按照 Marko Lahma 的答案中的建议运行是否有意义(因为安排一个作业运行一次会导致它运行一次,每次我们启动该作业时)应用程序)。我发现 CommandLineRunner 应用程序的示例不太适合我,主要是因为我们已经有一个 ApplicationRunner,它用于其他使用 Quartz 调度/cron 的作业。我对 Quartz 使用 SimpleTrigger 初始化这项工作并不满意,所以我必须寻找其他东西。
使用以下文章中的一些想法:
我能够拼凑一起实现一个工作实现,它允许我执行以下操作:
我想出了以下 CommandLineRunner 类:
在我的一个配置类中,我添加了一个 CommandLineRunner bean,它调用我创建的自定义 CommandLineRunner:
稍后,我能够通过 CLI 启动这些作业,而不会影响我当前的 Quartz 计划作业,只要没有人通过 CLI 多次运行该命令,它就不会再次运行。因为我接受 ApplicationArguments,所以我必须对类型进行一些处理,然后将它们转换为 String[]。
最后,我可以这样调用它:
结果是,该作业仅在我调用它时才初始化,并且它被排除在我用于其他作业的 CronTriggerFactoryBean 触发器之外。
这里做了几个假设,所以我尝试总结一下:
scheduler.setJobDetails(...)
),scheduler.setTriggers(...)
调用I had to ask myself if it made sense to try to configure a job and add checks if it had been run already as suggested in Marko Lahma's answer (since scheduling a job to run once results in it being run once, every time we start the app). I found examples of CommandLineRunner apps which didn't quite work for me, mostly because we already had an ApplicationRunner which was used for other jobs which use Quartz scheduling / cron. I wasn't happy with having Quartz initialize this job using a SimpleTrigger, so I had to find something else.
Using some ideas from the following articles:
I was able to piece together a working implementation which allows me to do the following:
I came up with the following CommandLineRunner class:
In one of my configuration classes I added a CommandLineRunner bean which calls the custom CommandLineRunner I created:
Later, I am able to initiate these jobs via the CLI without affecting my current Quartz scheduled jobs, and as long as no one runs the command via CLI multiple times, it will never be run again. I have to do some juggling of types since I accept ApplicationArguments, and then convert them into String[].
Finally, I am able to call it like this:
The result is that the job is initialized only when I call it, and it is excluded from my CronTriggerFactoryBean triggers which I used for my other jobs.
There are several assumptions being made here, so I'll try to summarize:
scheduler.setJobDetails(...)
)scheduler.setTriggers(...)
call另一种解决方案:SimpleSchedulerBuilder中有一个方法
.withRepeatCount(0)
:Another solution: There is a method
.withRepeatCount(0)
in SimpleSchedulerBuilder: