java 如何进行“顺序”作业调度(Quartz?)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13486607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do “sequential” Job Scheduling (Quartz?)
提问by Sanchit
I'm making use of Quartz Scheduling and there are 2 jobs. First Job is performing the tasks for around 2 minutes and the Second one is to be setup for Cleaning Operations of Temporary Files. So, I need to setup the Schedule to work in a way that after the first job is executed/finished performing tasks I need to do the cleaning operations with the help of Second Job.
Considering the Example 9 - Job Listeners under Quartz 2.1.x which states that we can define a method named jobWasExecuted( _, _ ); in the Job Listener and it executes when the 1st job is executed/or comes in running state.
Are we able to setup the schedule which can listen for the first job finishing then executes second? or,
Are we able to define the join() method like in Java Multithreading which can execute on the completion of first job?
我正在使用 Quartz Scheduling 并且有 2 个工作。第一个作业执行大约 2 分钟的任务,第二个作业是设置临时文件的清理操作。因此,我需要设置计划以在第一个作业执行/完成执行任务后我需要在第二个作业的帮助下进行清洁操作的方式工作。
考虑示例 9 - Quartz 2.1.x 下的作业侦听器,其中声明我们可以定义一个名为 jobWasExecuted( _, _ ) 的方法;在作业侦听器中,它在第一个作业执行/或进入运行状态时执行。
我们是否能够设置可以侦听第一个作业完成然后执行第二个的时间表?或者,
我们是否能够像在 Java 多线程中那样定义可以在第一个作业完成时执行的 join() 方法?
采纳答案by Vishal
There currently is no "direct" or "free" way to chain triggers with Quartz. However there are several ways you can accomplish it without much effort. Below is an outline of a couple approaches:
One way is to use a listener (i.e. a TriggerListener, JobListener or SchedulerListener) that can notice the completion of a job/trigger and then immediately schedule a new trigger to fire. This approach can get a bit involved, since you'll have to inform the listener which job follows which - and you may need to worry about persistence of this information.
Another way is to build a Job that contains within its JobDataMap the name of the next job to fire, and as the job completes (the last step in its Execute() method) have the job schedule the next job. Several people are doing this and have had good luck. Most have made a base (abstract) class that is a Job that knows how to get the job name and group out of the JobDataMap using special keys (constants) and contains code to schedule the identified job. Then they simply make extensions of this class that included the additional work the job should do.
目前没有“直接”或“免费”的方式来用 Quartz 链接触发器。但是,您可以通过多种方式轻松完成它。以下是几种方法的概述:
一种方法是使用侦听器(即 TriggerListener、JobListener 或 SchedulerListener),它可以注意到作业/触发器的完成,然后立即安排新的触发器触发。这种方法可能会涉及一些问题,因为您必须通知侦听器遵循哪个作业 - 您可能需要担心此信息的持久性。
另一种方法是构建一个 Job,在其 JobDataMap 中包含要触发的下一个作业的名称,并在作业完成时(其 Execute() 方法的最后一步)让作业调度下一个作业。有几个人正在这样做,并且有好运。大多数人已经创建了一个基本(抽象)类,它是一个 Job,它知道如何使用特殊键(常量)从 JobDataMap 中获取作业名称和组,并包含用于安排已识别作业的代码。然后他们简单地扩展这个类,包括工作应该做的额外工作。
参考:http: //www.quartz-scheduler.net/documentation/faq.html#how-do-i-chain-job-execution? -or,-how-do-i-create-a- workflow?
回答by Jan Moravec
I know this is an old question, but nevertheless there are 2 more options available to chain the execution of your jobs which people can find useful:
我知道这是一个老问题,但是还有另外 2 个选项可用于链接您的工作的执行,人们会发现它们很有用:
1) Use the JobChainingJobListenerthat is included in the standard Quartz distribution since very early releases. This listener allows you to programmatically define simple job chains using its addJobChainLinkmethod.
1) 使用标准 Quartz 发行版中包含的JobChainingJobListener,从很早的版本开始。此侦听器允许您使用其addJobChainLink方法以编程方式定义简单的作业链。
2) Use a commercial solution such as QuartzDeskthat I am the principal developer of. QuartzDesk contains a robust job chaining engine that allows you to externalize the definition of your job chains from the application code and enables you to update your job chains at runtime through a GUI without modifying, redeploying and restarting your application. A job chain can be associated with a particular job, trigger or it can be a global job chain that is executed whenever any of your jobs execute (useful for global job execution failure handlers etc.).
2) 使用商业解决方案,例如我是主要开发人员的QuartzDesk。QuartzDesk 包含一个强大的作业链引擎,允许您从应用程序代码外部化您的作业链的定义,并使您能够在运行时通过 GUI 更新您的作业链,而无需修改、重新部署和重新启动您的应用程序。作业链可以与特定作业、触发器相关联,也可以是在任何作业执行时执行的全局作业链(对于全局作业执行失败处理程序等很有用)。
回答by Angelo Quaglia
How do I keep a Job from firing concurrently?
Quartz.NET 2.x
Implement IJob and also decorate your job class with [DisallowConcurrentExecution] attribute. Read the API documentation for DisallowConcurrentExecutionAttribute for more information.
如何防止作业同时触发?
石英.NET 2.x
实现 IJob 并使用 [DisallowConcurrentExecution] 属性装饰您的作业类。阅读 DisallowConcurrentExecutionAttribute 的 API 文档以了解更多信息。
The annotation is available in the Java implementation.
该注解在 Java 实现中可用。