失败时石英重试
假设我有一个这样配置的触发器:
<bean id="updateInsBBTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="updateInsBBJobDetail"/>
<!-- run every morning at 5 AM -->
<property name="cronExpression" value="0 0 5 * * ?"/>
</bean>
触发器必须与另一个应用程序连接,如果出现任何问题(例如连接失败),它应该每 10 分钟重试该任务最多五次或直到成功。有什么方法可以配置触发器像这样工作吗?
Let's say I have a trigger configured this way:
<bean id="updateInsBBTrigger"
class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="updateInsBBJobDetail"/>
<!-- run every morning at 5 AM -->
<property name="cronExpression" value="0 0 5 * * ?"/>
</bean>
The trigger have to connect with another application and if there is any problem (like a connection failure) it should to retry the task up to five times every 10 minutes or until success. There is any way to configure the trigger to work like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会推荐这样的实现来在失败后恢复作业:
为什么?
I would recommend an implementation like this one to recover the job after a fail:
Why?
来源:自动重试失败的作业在 Quartz 中
如果你想让一个作业不断地一遍又一遍地尝试直到成功,你所要做的就是抛出一个带有标志的 JobExecutionException 来告诉调度程序在失败时再次触发它。以下代码显示了具体操作方法:
如果您想重试一定次数,情况会变得更加复杂。您必须使用 StatefulJob 并在其 JobDataMap 中保存一个 retryCounter,如果作业失败,您将增加该重试计数器。如果计数器超过最大重试次数,您可以根据需要禁用该作业。
Source: Automatically Retry Failed Jobs in Quartz
If you want to have a job which keeps trying over and over again until it succeeds, all you have to do is throw a JobExecutionException with a flag to tell the scheduler to fire it again when it fails. The following code shows how:
It gets a bit more complicated if you want to retry a certain number of times. You have to use a StatefulJob and hold a retryCounter in its JobDataMap, which you increment if the job fails. If the counter exceeds the maximum number of retries, then you can disable the job if you wish.
我建议提供更大的灵活性和可配置性,以便更好地在数据库中存储两个偏移量:repeatOffset,它将告诉您
作业应该重试多久后以及 TrialPeriodOffset 将保留作业所在时间窗口的信息
允许重新安排。然后你可以像这样检索这两个参数(我假设你正在使用 Spring):
然后,它不需要记住计数器,而是需要记住 initalAttempt:
并执行类似以下检查的操作:
这是一个好主意为尝试的结果创建一个枚举,该结果将返回到您的核心工作流程
像上面的应用程序。
然后构造重新安排时间:
I would suggest for more flexibility and configurability to better store in your DB two offsets: the repeatOffset which will tell you
after how long the job should be retried and the trialPeriodOffset which will keep the information of the time window that the job is
allowed to be rescheduled. Then you can retrieve these two parameters like (I assume you are using Spring):
Then instead of the job to remember the counter it will need to remember the initalAttempt:
and perform the something like the following check:
It would be a good idea to create an enum for the result of the attempt that is being returned back to the core workflow of your
application like above.
Then construct the rescheduling time:
我希望这些信息对您有用(这是我在此线程中的答案的副本)
下面是一个示例启动 cron 作业的多实例 Spring Boot 应用程序。
该作业必须仅在其中一个实例上运行。
每个实例的配置必须相同。
如果作业崩溃,它应该尝试重新启动 3 次,延迟 5 分钟 * 重新启动尝试次数。
如果作业在 3 次重新启动后仍然崩溃,则应设置作业触发器的默认 cron。
我们将在集群模式下使用 Quartz:
Deps:
首先,如答案
Out job:
这是 Quartz 配置(更多信息此处):
向调度程序添加一个侦听器:
现在最重要的是 - 使用侦听器处理作业执行的逻辑:
最后但并非最不重要的一点:application.yaml
这里数据库的官方脚本(使用 liquibase 或 Flyway)
更多信息:
关于石英
Spring Boot在集群模式下使用quartz< br>
还有一篇文章
集群有效的quartz
I hope this information will be useful for you (this is a copy of my answer in this thread)
Below is an example of a multi-instance Spring Boot application that launches a cron job.
The Job must be running on only one of the instances.
The configuration of each instance must be the same.
If a job crashes, it should try to restart 3 times with a delay of 5 minutes * number of restart attempts.
If the job still crashes after 3 restarts, the default cron for our job trigger should be set.
We will use Quartz in cluster mode:
Deps:
At first, it is a bad idea to use Thread.sleep(600000) as said in this answer
Out job:
Here is the Quartz configuration (more information here):
Add a listener to the scheduler:
And now the most important - the logic of processing the execution of our job with listener:
Last but not least: application.yaml
Here official scripts for database (use liquibase or flyway)
More information:
About quartz
spring boot using quartz in cluster mode
One more article
Cluster effectively quartz