From d0763adb80c8f3db07a1bc54087b8d406f737266 Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Mon, 19 Feb 2024 16:04:06 +0000 Subject: [PATCH] feat : adds sample job scheduler --- .vscode/launch.json | 10 ++++++++++ .../learning/shedlock/config/Initializer.java | 17 ++++++++--------- .../db/changelog/migration/01-init.sql | 2 +- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 63009f58c..939849c22 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -103,6 +103,16 @@ "projectName": "boot-graphql-webflux", "args": "", "envFile": "${workspaceFolder}/.env" + }, + { + "type": "java", + "name": "Spring Boot-Application", + "request": "launch", + "cwd": "${workspaceFolder}", + "mainClass": "com.learning.shedlock.Application", + "projectName": "boot-shedlock-sample", + "args": "--spring.profiles.active=local", + "envFile": "${workspaceFolder}/.env" } ] } \ No newline at end of file diff --git a/scheduler/boot-shedlock-sample/src/main/java/com/learning/shedlock/config/Initializer.java b/scheduler/boot-shedlock-sample/src/main/java/com/learning/shedlock/config/Initializer.java index 169d80cb7..1481698c8 100644 --- a/scheduler/boot-shedlock-sample/src/main/java/com/learning/shedlock/config/Initializer.java +++ b/scheduler/boot-shedlock-sample/src/main/java/com/learning/shedlock/config/Initializer.java @@ -1,19 +1,18 @@ package com.learning.shedlock.config; -import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.springframework.boot.CommandLineRunner; +import net.javacrumbs.shedlock.spring.annotation.SchedulerLock; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component -@RequiredArgsConstructor @Slf4j -public class Initializer implements CommandLineRunner { +public class Initializer { - private final ApplicationProperties properties; - - @Override - public void run(String... args) { - log.info("Running Initializer....."); + @Scheduled(cron = "0 */1 * * * *") + @SchedulerLock(name = "lockedTask", lockAtMostFor = "PT5S", lockAtLeastFor = "PT2S") + public void lockedTask() { + // code for task to be performed + log.info("Scheduler Job triggered"); } } diff --git a/scheduler/boot-shedlock-sample/src/main/resources/db/changelog/migration/01-init.sql b/scheduler/boot-shedlock-sample/src/main/resources/db/changelog/migration/01-init.sql index 8a9404073..6d6bf7c5c 100644 --- a/scheduler/boot-shedlock-sample/src/main/resources/db/changelog/migration/01-init.sql +++ b/scheduler/boot-shedlock-sample/src/main/resources/db/changelog/migration/01-init.sql @@ -1,4 +1,4 @@ -CREATE TABLE lockProvider ( +CREATE TABLE shedlock ( name VARCHAR(64), lock_until TIMESTAMP(3) NULL, locked_at TIMESTAMP(3) NULL,