From 2b0f9ef186fc0bf62259c8820c12217b3b322ddc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20B=C3=B6ckelmann?= Date: Thu, 14 Mar 2024 08:08:04 +0100 Subject: [PATCH] Implement a small example dynamic schedule --- .../src/main/java/core/DemoApplication.java | 21 +++++++------------ .../java/core/services/ntfy/NtfyTask.java | 8 +++++++ .../services/ntfy/TaskSchedulingService.java | 21 +++++++++++++++++++ .../src/main/resources/application.properties | 1 + 4 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 backend/src/main/java/core/services/ntfy/NtfyTask.java create mode 100644 backend/src/main/java/core/services/ntfy/TaskSchedulingService.java diff --git a/backend/src/main/java/core/DemoApplication.java b/backend/src/main/java/core/DemoApplication.java index ab593cb..0dbf798 100644 --- a/backend/src/main/java/core/DemoApplication.java +++ b/backend/src/main/java/core/DemoApplication.java @@ -5,12 +5,16 @@ import core.entities.UserRole; import core.repositories.RoleRepository; import core.repositories.UserRepository; import core.services.PropertyService; +import core.services.TaskScheduleService; +import core.services.ntfy.TaskSchedulingService; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; +import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication +@EnableScheduling public class DemoApplication{ public static void main(String[] args) { @@ -18,19 +22,10 @@ public class DemoApplication{ } - /*@Bean - public CommandLineRunner init(RoleRepository roleRepository, UserRepository userRepository, PropertyService propertyService) { + @Bean + public CommandLineRunner init() { return args -> { - for (UserRole userRole : UserRole.values()) { - if(!roleRepository.existsByName(userRole)) { - roleRepository.save(new RoleEntity(userRole)); - } - } - - propertyService.init(); - - FirstUserObserver observer = new FirstUserObserver(userRepository); - observer.start(); + TaskSchedulingService.scheduleTask(); }; - }*/ + } } diff --git a/backend/src/main/java/core/services/ntfy/NtfyTask.java b/backend/src/main/java/core/services/ntfy/NtfyTask.java new file mode 100644 index 0000000..603f9e7 --- /dev/null +++ b/backend/src/main/java/core/services/ntfy/NtfyTask.java @@ -0,0 +1,8 @@ +package core.services.ntfy; + +public class NtfyTask implements Runnable{ + @Override + public void run() { + System.out.println("A little Test"); + } +} diff --git a/backend/src/main/java/core/services/ntfy/TaskSchedulingService.java b/backend/src/main/java/core/services/ntfy/TaskSchedulingService.java new file mode 100644 index 0000000..c0c0f06 --- /dev/null +++ b/backend/src/main/java/core/services/ntfy/TaskSchedulingService.java @@ -0,0 +1,21 @@ +package core.services.ntfy; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.scheduling.TaskScheduler; +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; +import org.springframework.scheduling.support.CronTrigger; + +import java.util.HashMap; +import java.util.Map; +import java.util.TimeZone; +import java.util.concurrent.ScheduledFuture; + +public class TaskSchedulingService { + + + public static void scheduleTask() { + ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler(); + threadPoolTaskScheduler.initialize(); + threadPoolTaskScheduler.schedule(new NtfyTask(), new CronTrigger("0 0/1 * 1/1 * *")); + } +} diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index d42b602..c43317d 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -27,6 +27,7 @@ spring.jpa.open-in-view=false + # Spring Data Rest Setup spring.data.rest.base-path=/api