简体   繁体   English

有没有办法在Quartz中模拟触发器失火? (JAVA)

[英]Is there a way to simulate a trigger misfire in Quartz? (Java)

I'm looking for a way to simulate or force a trigger misfire programatically. 我正在寻找一种以编程方式模拟或强制触发失火的方法。 Here's the scenario: 这是场景:

I have a job set to trigger but the job requires some underlying resource that may be unavailable at times. 我有一个工作集要触发,但该工作需要一些可能有时不可用的底层资源。 If the resource is unavailable, I would like Quartz to re-fire the trigger later based on the misfire policy. 如果资源不可用,我希望Quartz稍后根据失火策略重新触发触发器。

I've explored two options that are similar but not quite what I'm looking for: 我已经探索了两个相似但不完全正在寻找的选项:

  1. Throwing a JobExecutionException with refireImmediately set to true: Works, but doesn't delay execution based on misfire policy; 抛出JobExecutionException并将refireImmediately设置为true:Works,但不会延迟基于失火策略的执行; this would hammer the resource availability check. 这会破坏资源可用性检查。
  2. Scheduling a second trigger at some fixed interval of time in the future: Also works, but doesn't take into account misfire policy. 在将来的某个固定时间间隔安排第二个触发器:也可以工作,但不考虑失火策略。 This means a job could wind up with a bunch of retries queued up stemming from different failed runs. 这意味着一项工作最终可能会因为不同的失败运行而排队等待一系列重试。

Any ideas or anything I'm missing? 我缺少任何想法或任何东西? Thanks! 谢谢!

If I got it right, you don't need to force or simulate a misfire because the resource availability is "something your Job can handle" . 如果我做对了,你不需要强制或模拟失火,因为资源可用性是“你的工作可以处理的东西”

Misfires exists and are managed by Quartz for situations like server shutdown or other "unexpected problems" that prevent Job execution. 失效存在并由Quartz管理,用于服务器关闭或其他阻止作业执行的“意外问题”

You have two options to follow to implement a simple fault-tolerance retry logic; 您可以通过两个选项来实现简单的容错重试逻辑; your Job can execute it's logic only when the underlying resource is available, so you can: 只有在底层资源可用时,您的Job才能执行它的逻辑,因此您可以:

  1. Wait for the resource to become availabile: 等待资源变为可用:
    • in this case your job waits and repeat the check for the resource availability, eventually after a short timeout, the job can give up and end. 在这种情况下,您的作业会等待并重复检查资源可用性,最终在短暂超时后,作业可以放弃和结束。

  2. Just do nothing if the resource is not available: 如果资源不可用,则不执行任何操作:
    • in this case the job ends without doing anything and the it will fire normally and retry according the Trigger definition. 在这种情况下,作业结束而不做任何事情,它将正常触发并根据触发器定义重试。

In both cases, if the resource is avaliable, the Job can execute it's inner logic and use the underlying resource. 在这两种情况下,如果资源可用,Job可以执行它的内部逻辑并使用底层资源。 (No misfires, because the Job was actually executed) (没有失火,因为工作实际上已被执行)

This can be done using a Trigger, setting the misfire policy you need in case the Job cannot be executed at all. 这可以使用Trigger来完成,设置您需要的失火策略,以防作业根本无法执行。 See This great and detailed article on Quartz misfires. 请参阅这篇关于Quartz失火的精彩而详细的文章


In your situation, If you want to execute a job one time every day: 在您的情况下,如果您想每天执行一次作业:

  1. Define a Cron trigger that fires multiple times in a day in a given span of time, for exemple, every 15 minutes from 8:00 AM to 12:00 AM: 定义一个Cron触发器,该触发器在给定的时间范围内一天多次触发,例如,从上午8:00到凌晨12:00每15分钟触发一次:

     0 0/15 8-12 * * ? 
  2. Buld a Job that use one of the two approaches described before 使用前面描述的两种方法之一来创建作业

  3. The first time your Job inner logic executes (that is, the resource is available) your job will save a "job executed flag" with the day of execution somewhere on DB. 第一次执行Job内部逻辑(即资源可用)时,您的作业将在DB上的某个位置保存“作业执行标志”。

On the following trigger executions, the Job will check the flag and will not execute it's inner logic again. 在下面的触发器执行中,Job将检查该标志,并且不会再次执行它的内部逻辑。


Also, if your job will take long time to finish, you may want to prevent concurrent execution of the same job using the following annotation on the job implementation: 此外,如果您的工作需要很长时间才能完成,您可能希望使用作业实现上的以下注释来防止同时执行同一作业:

@DisableConcurrentExecution

See Quartz tutorials for more informations on job execution. 有关作业执行的更多信息,请参阅Quartz 教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM