issue-86 #102
@ -21,6 +21,11 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.quartz-scheduler</groupId>
|
||||
<artifactId>quartz</artifactId>
|
||||
<version>2.3.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-security</artifactId>
|
||||
|
@ -1,8 +1,29 @@
|
||||
package core.services.ntfy;
|
||||
|
||||
public class NtfyTask implements Runnable{
|
||||
import org.quartz.Job;
|
||||
import org.quartz.JobExecutionContext;
|
||||
import org.quartz.JobExecutionException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.*;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
|
||||
public class NtfyTask implements Job {
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("A little Test");
|
||||
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
|
||||
HttpClient httpClient = HttpClient.newHttpClient();
|
||||
|
||||
try {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(new URI("http://localhost:4280/Test"))
|
||||
.POST(HttpRequest.BodyPublishers.ofString("A simple testmessage"))
|
||||
.build();
|
||||
|
||||
httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString());
|
||||
} catch (URISyntaxException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,21 +1,38 @@
|
||||
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;
|
||||
import org.quartz.*;
|
||||
import org.quartz.impl.StdSchedulerFactory;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Date;
|
||||
|
||||
public class TaskSchedulingService {
|
||||
|
||||
|
||||
public static void scheduleTask() {
|
||||
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
|
||||
threadPoolTaskScheduler.initialize();
|
||||
threadPoolTaskScheduler.schedule(new NtfyTask(), new CronTrigger("0 0/1 * 1/1 * *"));
|
||||
public static void scheduleTask() throws SchedulerException {
|
||||
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
|
||||
scheduler.start();
|
||||
|
||||
JobDetail job = JobBuilder.newJob(NtfyTask.class).withIdentity("Job 1", "Group 1").build();
|
||||
JobDetail job2 = JobBuilder.newJob(NtfyTask.class).withIdentity("Job 2", "Group 1").build();
|
||||
|
||||
LocalDateTime executionTime = LocalDateTime.of(2024, 3, 14, 9, 18); // Example: March 15, 2024, 10:00 AM
|
||||
|
||||
Trigger trigger = TriggerBuilder.newTrigger()
|
||||
.withIdentity("trigger1", "Group 1")
|
||||
.startAt(calculateDelayInMillis(executionTime)).build();
|
||||
Trigger trigger2 = TriggerBuilder.newTrigger()
|
||||
.withIdentity("trigger2", "Group 1")
|
||||
.startAt(calculateDelayInMillis(executionTime.plusMinutes(1))).build();
|
||||
|
||||
Trigger immediatly = TriggerBuilder.newTrigger().withIdentity("Immediately", "Group 1").startNow().build();
|
||||
|
||||
scheduler.scheduleJob(job, immediatly);
|
||||
}
|
||||
|
||||
private static Date calculateDelayInMillis(LocalDateTime executionTime) {
|
||||
return Date.from(executionTime.atZone(ZoneId.systemDefault()).toInstant());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user