mirror of
https://github.com/fabioformosa/quartz-manager.git
synced 2026-05-14 22:00:30 +09:00
fixed sonar issues
This commit is contained in:
@@ -115,6 +115,13 @@ public class JobService {
|
|||||||
scheduler.deleteJob(jobKey);
|
scheduler.deleteJob(jobKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Class<? extends Job> getEligibleJobClass(String jobClassName) throws ClassNotFoundException {
|
||||||
|
return jobClasses.stream()
|
||||||
|
.filter(jobClass -> jobClass.getName().equals(jobClassName))
|
||||||
|
.findFirst()
|
||||||
|
.orElseThrow(() -> new ClassNotFoundException("Job class " + jobClassName + " is not eligible"));
|
||||||
|
}
|
||||||
|
|
||||||
private JobKey requireJob(String group, String name) throws SchedulerException, JobNotFoundException {
|
private JobKey requireJob(String group, String name) throws SchedulerException, JobNotFoundException {
|
||||||
JobKey jobKey = JobKey.jobKey(name, group);
|
JobKey jobKey = JobKey.jobKey(name, group);
|
||||||
if (!scheduler.checkExists(jobKey))
|
if (!scheduler.checkExists(jobKey))
|
||||||
@@ -123,7 +130,7 @@ public class JobService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private JobDetail buildJobDetail(JobKey jobKey, ScheduledJobInputDTO scheduledJobInputDTO) throws ClassNotFoundException {
|
private JobDetail buildJobDetail(JobKey jobKey, ScheduledJobInputDTO scheduledJobInputDTO) throws ClassNotFoundException {
|
||||||
Class<? extends Job> jobClass = Class.forName(scheduledJobInputDTO.getJobClass()).asSubclass(Job.class);
|
Class<? extends Job> jobClass = getEligibleJobClass(scheduledJobInputDTO.getJobClass());
|
||||||
JobBuilder jobBuilder = JobBuilder.newJob(jobClass)
|
JobBuilder jobBuilder = JobBuilder.newJob(jobClass)
|
||||||
.withIdentity(jobKey)
|
.withIdentity(jobKey)
|
||||||
.storeDurably(scheduledJobInputDTO.isDurable())
|
.storeDurably(scheduledJobInputDTO.isDurable())
|
||||||
|
|||||||
@@ -13,8 +13,11 @@ import org.springframework.stereotype.Service;
|
|||||||
@Service
|
@Service
|
||||||
public class SimpleTriggerService extends AbstractSchedulerService {
|
public class SimpleTriggerService extends AbstractSchedulerService {
|
||||||
|
|
||||||
public SimpleTriggerService(@Qualifier("quartzManagerScheduler") Scheduler scheduler, ConversionService conversionService) {
|
private final JobService jobService;
|
||||||
|
|
||||||
|
public SimpleTriggerService(@Qualifier("quartzManagerScheduler") Scheduler scheduler, ConversionService conversionService, JobService jobService) {
|
||||||
super(scheduler, conversionService);
|
super(scheduler, conversionService);
|
||||||
|
this.jobService = jobService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SimpleTriggerDTO getSimpleTriggerByName(String name) throws SchedulerException, TriggerNotFoundException {
|
public SimpleTriggerDTO getSimpleTriggerByName(String name) throws SchedulerException, TriggerNotFoundException {
|
||||||
@@ -49,7 +52,7 @@ public class SimpleTriggerService extends AbstractSchedulerService {
|
|||||||
scheduler.scheduleJob(newSimpleTrigger);
|
scheduler.scheduleJob(newSimpleTrigger);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Class<? extends Job> jobClass = Class.forName(simpleTriggerCommandDTO.getSimpleTriggerInputDTO().getJobClass()).asSubclass(Job.class);
|
Class<? extends Job> jobClass = jobService.getEligibleJobClass(simpleTriggerCommandDTO.getSimpleTriggerInputDTO().getJobClass());
|
||||||
JobDetail jobDetail = JobBuilder.newJob()
|
JobDetail jobDetail = JobBuilder.newJob()
|
||||||
.ofType(jobClass)
|
.ofType(jobClass)
|
||||||
.storeDurably(false)
|
.storeDurably(false)
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ import org.quartz.CronTrigger;
|
|||||||
import org.quartz.DailyTimeIntervalScheduleBuilder;
|
import org.quartz.DailyTimeIntervalScheduleBuilder;
|
||||||
import org.quartz.DailyTimeIntervalTrigger;
|
import org.quartz.DailyTimeIntervalTrigger;
|
||||||
import org.quartz.DateBuilder;
|
import org.quartz.DateBuilder;
|
||||||
import org.quartz.Job;
|
|
||||||
import org.quartz.JobBuilder;
|
import org.quartz.JobBuilder;
|
||||||
import org.quartz.JobDataMap;
|
import org.quartz.JobDataMap;
|
||||||
import org.quartz.JobDetail;
|
import org.quartz.JobDetail;
|
||||||
@@ -43,13 +42,17 @@ import java.util.TimeZone;
|
|||||||
public class TriggerService {
|
public class TriggerService {
|
||||||
|
|
||||||
private static final int DEFAULT_PRIORITY = Trigger.DEFAULT_PRIORITY;
|
private static final int DEFAULT_PRIORITY = Trigger.DEFAULT_PRIORITY;
|
||||||
|
private static final String MISFIRE_DO_NOTHING = "DO_NOTHING";
|
||||||
|
private static final String MISFIRE_IGNORE_MISFIRES = "IGNORE_MISFIRES";
|
||||||
|
|
||||||
private final Scheduler scheduler;
|
private final Scheduler scheduler;
|
||||||
private final ConversionService conversionService;
|
private final ConversionService conversionService;
|
||||||
|
private final JobService jobService;
|
||||||
|
|
||||||
public TriggerService(@Qualifier("quartzManagerScheduler") Scheduler scheduler, ConversionService conversionService) {
|
public TriggerService(@Qualifier("quartzManagerScheduler") Scheduler scheduler, ConversionService conversionService, JobService jobService) {
|
||||||
this.scheduler = scheduler;
|
this.scheduler = scheduler;
|
||||||
this.conversionService = conversionService;
|
this.conversionService = conversionService;
|
||||||
|
this.jobService = jobService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<TriggerKeyDTO> fetchTriggers() throws SchedulerException {
|
public List<TriggerKeyDTO> fetchTriggers() throws SchedulerException {
|
||||||
@@ -83,7 +86,7 @@ public class TriggerService {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
JobDetail jobDetail = JobBuilder.newJob()
|
JobDetail jobDetail = JobBuilder.newJob()
|
||||||
.ofType(Class.forName(triggerInputDTO.getJobClass()).asSubclass(Job.class))
|
.ofType(jobService.getEligibleJobClass(triggerInputDTO.getJobClass()))
|
||||||
.storeDurably(false)
|
.storeDurably(false)
|
||||||
.build();
|
.build();
|
||||||
scheduler.scheduleJob(jobDetail, newTrigger);
|
scheduler.scheduleJob(jobDetail, newTrigger);
|
||||||
@@ -262,24 +265,24 @@ public class TriggerService {
|
|||||||
|
|
||||||
private CronScheduleBuilder applyCronMisfireInstruction(CronScheduleBuilder scheduleBuilder, String misfireInstruction) {
|
private CronScheduleBuilder applyCronMisfireInstruction(CronScheduleBuilder scheduleBuilder, String misfireInstruction) {
|
||||||
return switch (normalizeMisfireInstruction(misfireInstruction)) {
|
return switch (normalizeMisfireInstruction(misfireInstruction)) {
|
||||||
case "DO_NOTHING" -> scheduleBuilder.withMisfireHandlingInstructionDoNothing();
|
case MISFIRE_DO_NOTHING -> scheduleBuilder.withMisfireHandlingInstructionDoNothing();
|
||||||
case "IGNORE_MISFIRES" -> scheduleBuilder.withMisfireHandlingInstructionIgnoreMisfires();
|
case MISFIRE_IGNORE_MISFIRES -> scheduleBuilder.withMisfireHandlingInstructionIgnoreMisfires();
|
||||||
default -> scheduleBuilder.withMisfireHandlingInstructionFireAndProceed();
|
default -> scheduleBuilder.withMisfireHandlingInstructionFireAndProceed();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private DailyTimeIntervalScheduleBuilder applyDailyMisfireInstruction(DailyTimeIntervalScheduleBuilder scheduleBuilder, String misfireInstruction) {
|
private DailyTimeIntervalScheduleBuilder applyDailyMisfireInstruction(DailyTimeIntervalScheduleBuilder scheduleBuilder, String misfireInstruction) {
|
||||||
return switch (normalizeMisfireInstruction(misfireInstruction)) {
|
return switch (normalizeMisfireInstruction(misfireInstruction)) {
|
||||||
case "DO_NOTHING" -> scheduleBuilder.withMisfireHandlingInstructionDoNothing();
|
case MISFIRE_DO_NOTHING -> scheduleBuilder.withMisfireHandlingInstructionDoNothing();
|
||||||
case "IGNORE_MISFIRES" -> scheduleBuilder.withMisfireHandlingInstructionIgnoreMisfires();
|
case MISFIRE_IGNORE_MISFIRES -> scheduleBuilder.withMisfireHandlingInstructionIgnoreMisfires();
|
||||||
default -> scheduleBuilder.withMisfireHandlingInstructionFireAndProceed();
|
default -> scheduleBuilder.withMisfireHandlingInstructionFireAndProceed();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private CalendarIntervalScheduleBuilder applyCalendarIntervalMisfireInstruction(CalendarIntervalScheduleBuilder scheduleBuilder, String misfireInstruction) {
|
private CalendarIntervalScheduleBuilder applyCalendarIntervalMisfireInstruction(CalendarIntervalScheduleBuilder scheduleBuilder, String misfireInstruction) {
|
||||||
return switch (normalizeMisfireInstruction(misfireInstruction)) {
|
return switch (normalizeMisfireInstruction(misfireInstruction)) {
|
||||||
case "DO_NOTHING" -> scheduleBuilder.withMisfireHandlingInstructionDoNothing();
|
case MISFIRE_DO_NOTHING -> scheduleBuilder.withMisfireHandlingInstructionDoNothing();
|
||||||
case "IGNORE_MISFIRES" -> scheduleBuilder.withMisfireHandlingInstructionIgnoreMisfires();
|
case MISFIRE_IGNORE_MISFIRES -> scheduleBuilder.withMisfireHandlingInstructionIgnoreMisfires();
|
||||||
default -> scheduleBuilder.withMisfireHandlingInstructionFireAndProceed();
|
default -> scheduleBuilder.withMisfireHandlingInstructionFireAndProceed();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ import org.quartz.TriggerBuilder;
|
|||||||
import org.quartz.TriggerKey;
|
import org.quartz.TriggerKey;
|
||||||
import org.quartz.impl.calendar.AnnualCalendar;
|
import org.quartz.impl.calendar.AnnualCalendar;
|
||||||
import org.quartz.impl.calendar.CronCalendar;
|
import org.quartz.impl.calendar.CronCalendar;
|
||||||
import org.quartz.impl.calendar.DailyCalendar;
|
|
||||||
import org.quartz.impl.calendar.HolidayCalendar;
|
import org.quartz.impl.calendar.HolidayCalendar;
|
||||||
import org.quartz.impl.calendar.MonthlyCalendar;
|
import org.quartz.impl.calendar.MonthlyCalendar;
|
||||||
import org.quartz.impl.calendar.WeeklyCalendar;
|
import org.quartz.impl.calendar.WeeklyCalendar;
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ class JobServiceTest {
|
|||||||
void setUp() {
|
void setUp() {
|
||||||
MockitoAnnotations.openMocks(this);
|
MockitoAnnotations.openMocks(this);
|
||||||
schedulerBackedJobService = new JobService("", scheduler, conversionService);
|
schedulerBackedJobService = new JobService("", scheduler, conversionService);
|
||||||
|
schedulerBackedJobService.getJobClasses().add(SampleJob.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -106,7 +107,7 @@ class JobServiceTest {
|
|||||||
Assertions.assertThat(scheduledJobs.get(0).getDescription()).isEqualTo("sample");
|
Assertions.assertThat(scheduledJobs.get(0).getDescription()).isEqualTo("sample");
|
||||||
Assertions.assertThat(scheduledJobs.get(0).isDurable()).isTrue();
|
Assertions.assertThat(scheduledJobs.get(0).isDurable()).isTrue();
|
||||||
Assertions.assertThat(scheduledJobs.get(0).isRequestsRecovery()).isTrue();
|
Assertions.assertThat(scheduledJobs.get(0).isRequestsRecovery()).isTrue();
|
||||||
Assertions.assertThat(scheduledJobs.get(0).getJobDataMap().get("key")).isEqualTo("value");
|
Assertions.assertThat((Map<String, Object>) scheduledJobs.get(0).getJobDataMap()).containsEntry("key", "value");
|
||||||
Assertions.assertThat(scheduledJobs.get(0).getTriggerKeys()).hasSize(1);
|
Assertions.assertThat(scheduledJobs.get(0).getTriggerKeys()).hasSize(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package it.fabioformosa.quartzmanager.api.services;
|
|||||||
import it.fabioformosa.quartzmanager.api.common.utils.DateUtils;
|
import it.fabioformosa.quartzmanager.api.common.utils.DateUtils;
|
||||||
import it.fabioformosa.quartzmanager.api.dto.*;
|
import it.fabioformosa.quartzmanager.api.dto.*;
|
||||||
import it.fabioformosa.quartzmanager.api.exceptions.TriggerNotFoundException;
|
import it.fabioformosa.quartzmanager.api.exceptions.TriggerNotFoundException;
|
||||||
|
import it.fabioformosa.quartzmanager.api.jobs.SampleJob;
|
||||||
import org.assertj.core.api.Assertions;
|
import org.assertj.core.api.Assertions;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
@@ -29,9 +30,13 @@ class SimpleTriggerServiceTest {
|
|||||||
@Mock
|
@Mock
|
||||||
private ConversionService conversionService;
|
private ConversionService conversionService;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private JobService jobService;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() {
|
void setUp() throws ClassNotFoundException {
|
||||||
openMocks(this);
|
openMocks(this);
|
||||||
|
Mockito.doReturn(SampleJob.class).when(jobService).getEligibleJobClass(SampleJob.class.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -62,7 +67,7 @@ class SimpleTriggerServiceTest {
|
|||||||
@Test
|
@Test
|
||||||
void givenASimpleTriggerCommandDTO_whenASimpleTriggerIsScheduled_thenATriggerDTOIsReturned() throws SchedulerException, ClassNotFoundException {
|
void givenASimpleTriggerCommandDTO_whenASimpleTriggerIsScheduled_thenATriggerDTOIsReturned() throws SchedulerException, ClassNotFoundException {
|
||||||
SimpleTriggerInputDTO triggerInputDTO = SimpleTriggerInputDTO.builder()
|
SimpleTriggerInputDTO triggerInputDTO = SimpleTriggerInputDTO.builder()
|
||||||
.jobClass("it.fabioformosa.quartzmanager.api.jobs.SampleJob")
|
.jobClass(SampleJob.class.getName())
|
||||||
.startDate(new Date())
|
.startDate(new Date())
|
||||||
.repeatInterval(5000L).repeatCount(5)
|
.repeatInterval(5000L).repeatCount(5)
|
||||||
.endDate(DateUtils.addHoursToNow(1))
|
.endDate(DateUtils.addHoursToNow(1))
|
||||||
@@ -98,7 +103,7 @@ class SimpleTriggerServiceTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenASimpleTriggerCommandDTO_whenASimpleTriggerIsRecheduled_thenATriggerDTOIsReturned() throws SchedulerException, ClassNotFoundException, TriggerNotFoundException {
|
void givenASimpleTriggerCommandDTO_whenASimpleTriggerIsRecheduled_thenATriggerDTOIsReturned() throws SchedulerException, TriggerNotFoundException {
|
||||||
SimpleTriggerInputDTO triggerInputDTO = SimpleTriggerInputDTO.builder()
|
SimpleTriggerInputDTO triggerInputDTO = SimpleTriggerInputDTO.builder()
|
||||||
.jobClass("it.fabioformosa.quartzmanager.api.jobs.SampleJob")
|
.jobClass("it.fabioformosa.quartzmanager.api.jobs.SampleJob")
|
||||||
.startDate(new Date())
|
.startDate(new Date())
|
||||||
|
|||||||
@@ -50,9 +50,13 @@ class TriggerServiceTest {
|
|||||||
@Mock
|
@Mock
|
||||||
private ConversionService conversionService;
|
private ConversionService conversionService;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private JobService jobService;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp(){
|
void setUp() throws ClassNotFoundException {
|
||||||
MockitoAnnotations.openMocks(this);
|
MockitoAnnotations.openMocks(this);
|
||||||
|
Mockito.doReturn(SampleJob.class).when(jobService).getEligibleJobClass(SampleJob.class.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ quartz:
|
|||||||
enabled: true
|
enabled: true
|
||||||
|
|
||||||
quartz-manager:
|
quartz-manager:
|
||||||
jobClassPackages: it.fabioformosa.quartzmanager.api.jobs
|
jobClassPackages: it.fabioformosa.quartzmanager.api.jobs, it.fabioformosa.samplepackage
|
||||||
|
|
||||||
logging:
|
logging:
|
||||||
level:
|
level:
|
||||||
|
|||||||
Reference in New Issue
Block a user