From 74a2144a3737b0602a5d3365639001756c617254 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Wed, 19 Jun 2024 16:39:22 +0200 Subject: [PATCH] Adapt to Task and ScheduledTask changes in Framework Spring Framework wraps `Task` and `ScheduledTask` runnables to collect and share metadata about task execution and scheduling. The `ScheduledTasksEndpoint` descriptors were relying on the fact that tasks would never be wrapped. Spring Framework already wrapped runnables in various cases, for methods returning `Callable` or reactive types. This commit makes use of the `toString()` method to describe the runnable. Runnable implementations can override this method for displaying purposes on the actuator endpoint. See spring-projects/spring-framework#24560 See gh-41177 --- .../actuate/scheduling/ScheduledTasksEndpoint.java | 12 ++---------- .../scheduling/ScheduledTasksEndpointTests.java | 12 ++++++------ 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/scheduling/ScheduledTasksEndpoint.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/scheduling/ScheduledTasksEndpoint.java index 7b666c8b05c..98eff14a5d4 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/scheduling/ScheduledTasksEndpoint.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/scheduling/ScheduledTasksEndpoint.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,7 +16,6 @@ package org.springframework.boot.actuate.scheduling; -import java.lang.reflect.Method; import java.time.Duration; import java.util.Collection; import java.util.Collections; @@ -46,7 +45,6 @@ import org.springframework.scheduling.config.Task; import org.springframework.scheduling.config.TriggerTask; import org.springframework.scheduling.support.CronTrigger; import org.springframework.scheduling.support.PeriodicTrigger; -import org.springframework.scheduling.support.ScheduledMethodRunnable; /** * {@link Endpoint @Endpoint} to expose information about an application's scheduled @@ -284,13 +282,7 @@ public class ScheduledTasksEndpoint { private final String target; private RunnableDescriptor(Runnable runnable) { - if (runnable instanceof ScheduledMethodRunnable scheduledMethodRunnable) { - Method method = scheduledMethodRunnable.getMethod(); - this.target = method.getDeclaringClass().getName() + "." + method.getName(); - } - else { - this.target = runnable.getClass().getName(); - } + this.target = runnable.toString(); } public String getTarget() { diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/scheduling/ScheduledTasksEndpointTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/scheduling/ScheduledTasksEndpointTests.java index 8befd959fcd..6456d266da6 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/scheduling/ScheduledTasksEndpointTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/scheduling/ScheduledTasksEndpointTests.java @@ -80,7 +80,7 @@ class ScheduledTasksEndpointTests { assertThat(tasks.getCron()).hasSize(1); CronTaskDescriptor description = (CronTaskDescriptor) tasks.getCron().get(0); assertThat(description.getExpression()).isEqualTo("0 0 0/6 1/1 * ?"); - assertThat(description.getRunnable().getTarget()).isEqualTo(CronTriggerRunnable.class.getName()); + assertThat(description.getRunnable().getTarget()).contains(CronTriggerRunnable.class.getName()); }); } @@ -109,7 +109,7 @@ class ScheduledTasksEndpointTests { FixedDelayTaskDescriptor description = (FixedDelayTaskDescriptor) tasks.getFixedDelay().get(0); assertThat(description.getInitialDelay()).isEqualTo(2000); assertThat(description.getInterval()).isEqualTo(1000); - assertThat(description.getRunnable().getTarget()).isEqualTo(FixedDelayTriggerRunnable.class.getName()); + assertThat(description.getRunnable().getTarget()).contains(FixedDelayTriggerRunnable.class.getName()); }); } @@ -123,7 +123,7 @@ class ScheduledTasksEndpointTests { FixedDelayTaskDescriptor description = (FixedDelayTaskDescriptor) tasks.getFixedDelay().get(0); assertThat(description.getInitialDelay()).isEqualTo(0); assertThat(description.getInterval()).isEqualTo(1000); - assertThat(description.getRunnable().getTarget()).isEqualTo(FixedDelayTriggerRunnable.class.getName()); + assertThat(description.getRunnable().getTarget()).contains(FixedDelayTriggerRunnable.class.getName()); }); } @@ -152,7 +152,7 @@ class ScheduledTasksEndpointTests { FixedRateTaskDescriptor description = (FixedRateTaskDescriptor) tasks.getFixedRate().get(0); assertThat(description.getInitialDelay()).isEqualTo(3000); assertThat(description.getInterval()).isEqualTo(2000); - assertThat(description.getRunnable().getTarget()).isEqualTo(FixedRateTriggerRunnable.class.getName()); + assertThat(description.getRunnable().getTarget()).contains(FixedRateTriggerRunnable.class.getName()); }); } @@ -166,7 +166,7 @@ class ScheduledTasksEndpointTests { FixedRateTaskDescriptor description = (FixedRateTaskDescriptor) tasks.getFixedRate().get(0); assertThat(description.getInitialDelay()).isEqualTo(0); assertThat(description.getInterval()).isEqualTo(2000); - assertThat(description.getRunnable().getTarget()).isEqualTo(FixedRateTriggerRunnable.class.getName()); + assertThat(description.getRunnable().getTarget()).contains(FixedRateTriggerRunnable.class.getName()); }); } @@ -178,7 +178,7 @@ class ScheduledTasksEndpointTests { assertThat(tasks.getFixedRate()).isEmpty(); assertThat(tasks.getCustom()).hasSize(1); CustomTriggerTaskDescriptor description = (CustomTriggerTaskDescriptor) tasks.getCustom().get(0); - assertThat(description.getRunnable().getTarget()).isEqualTo(CustomTriggerRunnable.class.getName()); + assertThat(description.getRunnable().getTarget()).contains(CustomTriggerRunnable.class.getName()); assertThat(description.getTrigger()).isEqualTo(CustomTriggerTask.trigger.toString()); }); }