diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/quartz/QuartzEndpoint.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/quartz/QuartzEndpoint.java index ea74c92e55..49608ac6b0 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/quartz/QuartzEndpoint.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/quartz/QuartzEndpoint.java @@ -203,14 +203,12 @@ public class QuartzEndpoint { throws SchedulerException { JobKey jobKey = JobKey.jobKey(jobName, groupName); JobDetail jobDetail = this.scheduler.getJobDetail(jobKey); - if (jobDetail != null) { - List triggers = this.scheduler.getTriggersOfJob(jobKey); - return new QuartzJobDetailsDescriptor(jobDetail.getKey().getGroup(), jobDetail.getKey().getName(), - jobDetail.getDescription(), jobDetail.getJobClass().getName(), jobDetail.isDurable(), - jobDetail.requestsRecovery(), sanitizeJobDataMap(jobDetail.getJobDataMap(), showUnsanitized), - extractTriggersSummary(triggers)); + if (jobDetail == null) { + return null; } - return null; + List triggers = this.scheduler.getTriggersOfJob(jobKey); + return new QuartzJobDetailsDescriptor(jobDetail, sanitizeJobDataMap(jobDetail.getJobDataMap(), showUnsanitized), + extractTriggersSummary(triggers)); } /** @@ -232,8 +230,7 @@ public class QuartzEndpoint { return null; } this.scheduler.triggerJob(jobKey); - return new QuartzJobTriggerDescriptor(jobDetail.getKey().getGroup(), jobDetail.getKey().getName(), - jobDetail.getJobClass().getName(), Instant.now()); + return new QuartzJobTriggerDescriptor(jobDetail); } private static List> extractTriggersSummary(List triggers) { @@ -282,12 +279,12 @@ public class QuartzEndpoint { } private Map sanitizeJobDataMap(JobDataMap dataMap, boolean showUnsanitized) { - if (dataMap != null) { - Map map = new LinkedHashMap<>(dataMap.getWrappedMap()); - map.replaceAll((key, value) -> getSanitizedValue(showUnsanitized, key, value)); - return map; + if (dataMap == null) { + return null; } - return null; + Map map = new LinkedHashMap<>(dataMap.getWrappedMap()); + map.replaceAll((key, value) -> getSanitizedValue(showUnsanitized, key, value)); + return map; } private Object getSanitizedValue(boolean showUnsanitized, String key, Object value) { @@ -375,7 +372,7 @@ public class QuartzEndpoint { private final Map jobs; - private QuartzJobGroupSummaryDescriptor(String group, Map jobs) { + QuartzJobGroupSummaryDescriptor(String group, Map jobs) { this.group = group; this.jobs = jobs; } @@ -397,7 +394,7 @@ public class QuartzEndpoint { private final String className; - private QuartzJobSummaryDescriptor(JobDetail job) { + QuartzJobSummaryDescriptor(JobDetail job) { this.className = job.getJobClass().getName(); } @@ -424,11 +421,11 @@ public class QuartzEndpoint { private final Instant triggerTime; - private QuartzJobTriggerDescriptor(String group, String name, String className, Instant triggerTime) { - this.group = group; - this.name = name; - this.className = className; - this.triggerTime = triggerTime; + QuartzJobTriggerDescriptor(JobDetail jobDetail) { + this.group = jobDetail.getKey().getGroup(); + this.name = jobDetail.getKey().getName(); + this.className = jobDetail.getJobClass().getName(); + this.triggerTime = Instant.now(); } public String getGroup() { @@ -470,14 +467,13 @@ public class QuartzEndpoint { private final List> triggers; - QuartzJobDetailsDescriptor(String group, String name, String description, String className, boolean durable, - boolean requestRecovery, Map data, List> triggers) { - this.group = group; - this.name = name; - this.description = description; - this.className = className; - this.durable = durable; - this.requestRecovery = requestRecovery; + QuartzJobDetailsDescriptor(JobDetail jobDetail, Map data, List> triggers) { + this.group = jobDetail.getKey().getGroup(); + this.name = jobDetail.getKey().getName(); + this.description = jobDetail.getDescription(); + this.className = jobDetail.getJobClass().getName(); + this.durable = jobDetail.isDurable(); + this.requestRecovery = jobDetail.requestsRecovery(); this.data = data; this.triggers = triggers; } @@ -527,7 +523,7 @@ public class QuartzEndpoint { private final Triggers triggers; - private QuartzTriggerGroupSummaryDescriptor(String group, boolean paused, + QuartzTriggerGroupSummaryDescriptor(String group, boolean paused, Map> descriptionsByType) { this.group = group; this.paused = paused; @@ -559,7 +555,7 @@ public class QuartzEndpoint { private final Map custom; - private Triggers(Map> descriptionsByType) { + Triggers(Map> descriptionsByType) { this.cron = descriptionsByType.getOrDefault(TriggerType.CRON, Collections.emptyMap()); this.dailyTimeInterval = descriptionsByType.getOrDefault(TriggerType.DAILY_INTERVAL, Collections.emptyMap()); @@ -622,30 +618,23 @@ public class QuartzEndpoint { */ public abstract static class TriggerDescriptor { - private static final Map, Function> DESCRIBERS = new LinkedHashMap<>(); + private static final Map, Function> DESCRIBERS; static { - DESCRIBERS.put(CronTrigger.class, (trigger) -> new CronTriggerDescriptor((CronTrigger) trigger)); - DESCRIBERS.put(SimpleTrigger.class, (trigger) -> new SimpleTriggerDescriptor((SimpleTrigger) trigger)); - DESCRIBERS.put(DailyTimeIntervalTrigger.class, + Map, Function> descriptors = new LinkedHashMap<>(); + descriptors.put(CronTrigger.class, (trigger) -> new CronTriggerDescriptor((CronTrigger) trigger)); + descriptors.put(SimpleTrigger.class, (trigger) -> new SimpleTriggerDescriptor((SimpleTrigger) trigger)); + descriptors.put(DailyTimeIntervalTrigger.class, (trigger) -> new DailyTimeIntervalTriggerDescriptor((DailyTimeIntervalTrigger) trigger)); - DESCRIBERS.put(CalendarIntervalTrigger.class, + descriptors.put(CalendarIntervalTrigger.class, (trigger) -> new CalendarIntervalTriggerDescriptor((CalendarIntervalTrigger) trigger)); + DESCRIBERS = Map.copyOf(descriptors); } private final Trigger trigger; private final TriggerType type; - private static TriggerDescriptor of(Trigger trigger) { - return DESCRIBERS.entrySet() - .stream() - .filter((entry) -> entry.getKey().isInstance(trigger)) - .map((entry) -> entry.getValue().apply(trigger)) - .findFirst() - .orElse(new CustomTriggerDescriptor(trigger)); - } - protected TriggerDescriptor(Trigger trigger, TriggerType type) { this.trigger = trigger; this.type = type; @@ -723,6 +712,15 @@ public class QuartzEndpoint { return this.type; } + static TriggerDescriptor of(Trigger trigger) { + return DESCRIBERS.entrySet() + .stream() + .filter((entry) -> entry.getKey().isInstance(trigger)) + .map((entry) -> entry.getValue().apply(trigger)) + .findFirst() + .orElse(new CustomTriggerDescriptor(trigger)); + } + } /** diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/quartz/QuartzEndpointWebExtension.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/quartz/QuartzEndpointWebExtension.java index 406aed760f..c36f7c1ba1 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/quartz/QuartzEndpointWebExtension.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/quartz/QuartzEndpointWebExtension.java @@ -83,13 +83,10 @@ public class QuartzEndpointWebExtension { @WriteOperation public WebEndpointResponse triggerQuartzJob(@Selector String jobs, @Selector String group, @Selector String name, String state) throws SchedulerException { - if (!"jobs".equals(jobs)) { - return new WebEndpointResponse<>(WebEndpointResponse.STATUS_BAD_REQUEST); + if ("jobs".equals(jobs) && "running".equals(state)) { + return handleNull(this.delegate.triggerQuartzJob(group, name)); } - if (!"running".equals(state)) { - return new WebEndpointResponse<>(WebEndpointResponse.STATUS_BAD_REQUEST); - } - return handleNull(this.delegate.triggerQuartzJob(group, name)); + return new WebEndpointResponse<>(WebEndpointResponse.STATUS_BAD_REQUEST); } private WebEndpointResponse handle(String jobsOrTriggers, ResponseSupplier jobAction, @@ -104,10 +101,8 @@ public class QuartzEndpointWebExtension { } private WebEndpointResponse handleNull(T value) { - if (value != null) { - return new WebEndpointResponse<>(value); - } - return new WebEndpointResponse<>(WebEndpointResponse.STATUS_NOT_FOUND); + return (value != null) ? new WebEndpointResponse<>(value) + : new WebEndpointResponse<>(WebEndpointResponse.STATUS_NOT_FOUND); } @FunctionalInterface