示例:导入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-schedulerExample: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. 这篇关于跨多个实例仅运行一次计划作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!