Define Starting Reminder Notification
All checks were successful
Java CI with Maven / test (push) Successful in 39s
Java CI with Maven / build-and-push-frontend (push) Successful in 8s
Java CI with Maven / build-and-push-backend (push) Successful in 8s

This commit is contained in:
Sebastian Böckelmann 2024-03-14 10:04:58 +01:00
parent 248717cb45
commit 871751b5d4
4 changed files with 50 additions and 4 deletions

View File

@ -25,7 +25,7 @@ public class DemoApplication{
@Bean @Bean
public CommandLineRunner init() { public CommandLineRunner init() {
return args -> { return args -> {
TaskSchedulingService.scheduleTask(); TaskSchedulingService.scheduleStartingTask("Finishing ConceptCreator");
}; };
} }
} }

View File

@ -18,7 +18,7 @@ public class NtfyTask implements Job {
try { try {
HttpRequest request = HttpRequest.newBuilder() HttpRequest request = HttpRequest.newBuilder()
.uri(new URI("http://localhost:4280/Test")) .uri(new URI("http://localhost:4280/Test"))
.POST(HttpRequest.BodyPublishers.ofString("A simple testmessage")) .POST(HttpRequest.BodyPublishers.ofString(jobExecutionContext.getMergedJobDataMap().getString("data")))
.build(); .build();
httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString()); httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString());

View File

@ -0,0 +1,34 @@
package core.services.ntfy;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class NtfyTaskStartNotification implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
HttpClient httpClient = HttpClient.newHttpClient();
String msg = "Task " + jobExecutionContext.getMergedJobDataMap().getString("task") + " should have been started by now!";
try {
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI("http://localhost:4280/Test"))
.POST(HttpRequest.BodyPublishers.ofString(msg))
.header("Tags", "warning")
.header("Title", "Task Starting Reminder")
.header("Actions", "view, Open TimeScheduler, https://time.fawkes100.de/, clear=true")
.build();
httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
}

View File

@ -10,13 +10,25 @@ import java.util.Date;
public class TaskSchedulingService { public class TaskSchedulingService {
public static void scheduleStartingTask(String taskName) throws SchedulerException {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.start();
JobDetail job = JobBuilder.newJob(NtfyTaskStartNotification.class).build();
job.getJobDataMap().put("task", taskName);
Trigger immediatly = TriggerBuilder.newTrigger().startNow().build();
scheduler.scheduleJob(job, immediatly);
}
public static void scheduleTask() throws SchedulerException { public static void scheduleTask() throws SchedulerException {
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.start(); scheduler.start();
JobDetail job = JobBuilder.newJob(NtfyTask.class).withIdentity("Job 1", "Group 1").build(); JobDetail job = JobBuilder.newJob(NtfyTask.class).build();
JobDetail job2 = JobBuilder.newJob(NtfyTask.class).withIdentity("Job 2", "Group 1").build();
job.getJobDataMap().put("data", "A simple Job 1 Message");
LocalDateTime executionTime = LocalDateTime.of(2024, 3, 14, 9, 18); // Example: March 15, 2024, 10:00 AM LocalDateTime executionTime = LocalDateTime.of(2024, 3, 14, 9, 18); // Example: March 15, 2024, 10:00 AM