Skip to content

Commit

Permalink
feat: handle db using liquibase and expose endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Nov 4, 2023
1 parent 0ee9919 commit 93f39f2
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.scheduler.quartz.job;

import com.scheduler.quartz.service.OddEvenService;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Component;

@Component
@DisallowConcurrentExecution
public class SampleJob implements Job {

private final OddEvenService oddEvenService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.quartz.DateBuilder.futureDate;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

import org.quartz.*;
Expand All @@ -14,18 +15,23 @@ public class SchedulerRegistrationService {
@Bean
JobDetail registerOddJob() {
return newJob(SampleJob.class)
.withIdentity("sample-job")
.withIdentity("sample-job", "sample-group")
.usingJobData("jobName", "odd")
.storeDurably()
.requestRecovery()
.build();
}

@Bean
Trigger triggerOddJob(JobDetail registerOddJob) {
return newTrigger()
.withIdentity("trigger-sample-job")
.withIdentity("trigger-sample-job", "sample-group")
.forJob(registerOddJob.getKey())
.startAt(futureDate(5, DateBuilder.IntervalUnit.SECOND))
.startAt(futureDate(10, DateBuilder.IntervalUnit.SECOND))
.withSchedule(simpleSchedule()
.withIntervalInSeconds(5) // Run every 2 seconds
.repeatForever()
.withMisfireHandlingInstructionFireNow())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.scheduler.quartz.model.response;

public record JobStatus(String jobName, boolean completed) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.scheduler.quartz.service;

import com.scheduler.quartz.model.response.JobStatus;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import org.quartz.*;
import org.quartz.impl.matchers.GroupMatcher;
import org.quartz.utils.Key;
import org.springframework.stereotype.Service;

@Service
public class JobsService {

private final Scheduler scheduler;
private final String groupName = "sample-group";

public JobsService(Scheduler scheduler) {
this.scheduler = scheduler;
}

public boolean deleteJob(String id) throws SchedulerException {
JobKey jobKey = new JobKey(id, groupName);
return scheduler.deleteJob(jobKey);
}

public List<String> getJobs() throws SchedulerException {
return scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName)).stream()
.map(Key::getName)
.sorted(Comparator.naturalOrder())
.collect(Collectors.toList());
}

public List<JobStatus> getJobsStatuses() throws SchedulerException {
LinkedList<JobStatus> list = new LinkedList<>();
for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) {
JobDetail jobDetail = scheduler.getJobDetail(jobKey);
List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobDetail.getKey());
for (Trigger trigger : triggers) {
Trigger.TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
if (Trigger.TriggerState.COMPLETE.equals(triggerState)) {
list.add(new JobStatus(jobKey.getName(), true));
} else {
list.add(new JobStatus(jobKey.getName(), false));
}
}
}
list.sort(Comparator.comparing(JobStatus::jobName));
return list;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.scheduler.quartz.web.controller;

import com.scheduler.quartz.model.response.JobStatus;
import com.scheduler.quartz.service.JobsService;
import java.util.List;
import org.quartz.SchedulerException;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/jobs")
public class JobsController {

private final JobsService jobsService;

public JobsController(JobsService jobsService) {
this.jobsService = jobsService;
}

@GetMapping
List<String> getJobs() throws SchedulerException {
return jobsService.getJobs();
}

@GetMapping("/statuses")
List<JobStatus> getJobsStatuses() throws SchedulerException {
return jobsService.getJobsStatuses();
}

@DeleteMapping("/{id}")
boolean deleteJob(@PathVariable String id) throws SchedulerException {
return jobsService.deleteJob(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ spring.jpa.properties.hibernate.connection.provider_disables_autocommit=true
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

spring.quartz.job-store-type=jdbc
spring.quartz.jdbc.initialize-schema=always
#spring.quartz.jdbc.initialize-schema=always
spring.quartz.properties.org.quartz.jobStore.isClustered=true
spring.quartz.properties.org.quartz.jobStore.useProperties=true
spring.quartz.properties.org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,16 @@ databaseChangeLog:
- includeAll:
path: migration/
errorIfMissingOrEmpty: true
relativeToChangelogFile: true
relativeToChangelogFile: true

- changeSet:
dbms: postgresql
id: create-spring-quartz-metadata
author: raja
changes:
- sqlFile:
encoding: UTF-8
path: classpath:/org/quartz/impl/jdbcjobstore/tables_postgres.sql
relativeToChangelogFile: false
splitStatements: true
stripComments: true

0 comments on commit 93f39f2

Please sign in to comment.