跨多个实例仅运行一次计划作业 [英] Running Only Once A Schedule Job Across Multiple Instances
问题描述
我有一个 schedule作业
,该作业在每个月末运行.运行后,它将一些数据保存到数据库.
当我扩展应用程序(例如,具有2个实例)时,两个实例都运行计划作业并保存数据,并且一天结束时我的数据库具有相同的数据.
因此,我希望计划作业仅运行一次,而不管云中的实例数如何.
您可以在方法中使用@SnapLock批注,以确保任务仅运行一次.请参阅此版本库中的文档 https://github.com/luismpcosta/snap-scheduler >
示例:导入Maven依赖项
< dependency>< groupId> io.opensw.scheduler</groupId>< artifactId>快照调度程序核心</artifactId>< version> 0.3.0</version></dependency>
导入maven依赖项后,您需要创建所需的表表.
最后,在下面看到如何注释方法,该方法保证仅使用@SnapLock注释运行一次:
import io.opensw.scheduler.core.annotations.SnapLock;...@SnapLock(键="UNIQUE_TASK_KEY",时间= 60)@Scheduled(fixedRate = 30000)公共无效reportCurrentTime(){...}
通过这种方法,您还可以保证对任务执行情况进行审核.
I have a schedule job
that run every end of the month. After running it saves some data to database.
When i scale the app(for example with 2 instances) both instances run the schedule job and both save the data and at the end of day my database has the same data.
So i want the schedule job only run one time regardless of instances numbers at cloud.
You can use @SnapLock annotation in your method which guarantees that task only runs once. See documentation in this repo https://github.com/luismpcosta/snap-scheduler
Example: Import maven dependency
<dependency>
<groupId>io.opensw.scheduler</groupId>
<artifactId>snap-scheduler-core</artifactId>
<version>0.3.0</version>
</dependency>
After importing maven dependency, you'll need to create the required tables tables.
Finally, see bellow how to annotate methods which guarantees that only runs once with @SnapLock annotation:
import io.opensw.scheduler.core.annotations.SnapLock;
...
@SnapLock(key = "UNIQUE_TASK_KEY", time = 60)
@Scheduled(fixedRate = 30000)
public void reportCurrentTime() {
...
}
With this approach you also guarantee audit of the tasks execution.
这篇关于跨多个实例仅运行一次计划作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!