Scheduled SchedulerLock 任务调度 (防止多节点任务重复跑)

1.引入jar
<dependency>
   <groupId>net.javacrumbs.shedlock</groupId>
   <artifactId>shedlock-spring</artifactId>
   <version>2.1.0</version>
</dependency>
<dependency>
   <groupId>net.javacrumbs.shedlock</groupId>
   <artifactId>shedlock-provider-jdbc-template</artifactId>
   <version>2.2.0</version>
</dependency>

 

2.@SchedulerLock(name = "xxxTask", lockAtMostFor = 1000 * 60 * 10 , lockAtLeastFor = 1000 * 60 * 2 )  ;

       name  锁名字 ;

       lockAtMostFor  最长锁表时间:(防止节点奔溃,不释放锁)

       lockAtLeastFor 最短锁表时间      防止任务重复跑

@Scheduled(cron = "0 */10 * * * ?")
@SchedulerLock(name = "xxxTask", lockAtMostFor = 1000 * 60 * 10 , lockAtLeastFor = 1000 * 60 * 2 )
@ApiOperation("自动发起")
public void xxx(){

}

 

3.锁提供者(这里是指jdbc ; 数据库方式)

 

@Component
public class lockProvider {


    /**
     * @description:  锁提供者
     * @Author yhy
     * @date 2020/11/10 11:23
     */
    @Bean(name = {"configure2","configure1"})
    public LockProvider lockProvider(DataSource dataSource) {
        return new JdbcTemplateLockProvider(dataSource);
    }
}

 

 

上一篇:SpringBoot中使用@Scheduled创建定时任务


下一篇:@Scheduled