springboot定时任务@Scheduled以及集群定时任务锁@SchedulerLock

springboot定时任务@Scheduled以及集群定时任务锁@SchedulerLock

代码地址: https://github.com/763977251/MyDemo/tree/main/scheduled_demo

1.引入maven依赖

引入mysql:因为分布式锁是基于mysql数据库表实现的

		
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>1.2.8version>
        dependency>

        
        <dependency>
            <groupId>net.javacrumbs.shedlockgroupId>
            <artifactId>shedlock-springartifactId>
            <version>4.30.0version>
        dependency>
        <dependency>
            <groupId>net.javacrumbs.shedlockgroupId>
            <artifactId>shedlock-provider-jdbc-templateartifactId>
            <version>4.30.0version>
        dependency>

2.数据库建表

/*
MySQL Backup
Database: scheduled_demo
Backup Time: 2021-12-16 11:13:58
*/

SET FOREIGN_KEY_CHECKS=0;
DROP TABLE IF EXISTS `shedlock`;
CREATE TABLE `shedlock` (
  `name` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  `lock_until` timestamp(3) NULL DEFAULT NULL,
  `locked_at` timestamp(3) NULL DEFAULT NULL,
  `locked_by` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
  PRIMARY KEY (`name`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC COMMENT='shedlock分布式定时任务表';
BEGIN;
LOCK TABLES `shedlock` WRITE;
DELETE FROM `shedlock`;
UNLOCK TABLES;
COMMIT;

3.项目配置文件

配置数据库连接

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/scheduled_demo?useUnicode=true&characterEncoding=utf-8&useSSL=false&&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=111111

4.分布式锁配置信息

这里使用mysql数据库实现分布式锁

@Component
public class ShedLockConfig {
    @Bean
    public LockProvider lockProvider(DataSource dataSource) {
        return new JdbcTemplateLockProvider(
                JdbcTemplateLockProvider.Configuration.builder()
                        .withJdbcTemplate(new JdbcTemplate(dataSource))
                        .withTimeZone(TimeZone.getTimeZone("Asia/Shanghai"))
                        .build()
        );
    }
}

5.编写定时任务

@Component
public class TaskDemo {

    @Scheduled(cron="0 */1 * * * *") //每分钟执行一次
    @SchedulerLock(name = "test", lockAtMostFor = "50s", lockAtLeastFor = "40s") // 锁最多保持50秒,最少保持40秒
    public void test(){
        Date now = new Date();
        System.out.println("定时任务开始执行,时间:" + now);

    }
}

6.主启动类上加入注解

@SpringBootApplication
@EnableScheduling //定时任务注解
@EnableSchedulerLock(defaultLockAtMostFor = "PT30S")//定时任务锁,默认时间30S
public class ScheduledDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(ScheduledDemoApplication.class, args);
    }

}

你可能感兴趣的:(数据库,java,spring,boot)