Skip to content

Commit 74a2144

Browse files
bclozelwilkinsona
authored andcommitted
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
1 parent 305bfb1 commit 74a2144

File tree

2 files changed

+8
-16
lines changed

2 files changed

+8
-16
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/scheduling/ScheduledTasksEndpoint.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.boot.actuate.scheduling;
1818

19-
import java.lang.reflect.Method;
2019
import java.time.Duration;
2120
import java.util.Collection;
2221
import java.util.Collections;
@@ -46,7 +45,6 @@
4645
import org.springframework.scheduling.config.TriggerTask;
4746
import org.springframework.scheduling.support.CronTrigger;
4847
import org.springframework.scheduling.support.PeriodicTrigger;
49-
import org.springframework.scheduling.support.ScheduledMethodRunnable;
5048

5149
/**
5250
* {@link Endpoint @Endpoint} to expose information about an application's scheduled
@@ -284,13 +282,7 @@ public static final class RunnableDescriptor {
284282
private final String target;
285283

286284
private RunnableDescriptor(Runnable runnable) {
287-
if (runnable instanceof ScheduledMethodRunnable scheduledMethodRunnable) {
288-
Method method = scheduledMethodRunnable.getMethod();
289-
this.target = method.getDeclaringClass().getName() + "." + method.getName();
290-
}
291-
else {
292-
this.target = runnable.getClass().getName();
293-
}
285+
this.target = runnable.toString();
294286
}
295287

296288
public String getTarget() {

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/scheduling/ScheduledTasksEndpointTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void cronTriggerIsReported() {
8080
assertThat(tasks.getCron()).hasSize(1);
8181
CronTaskDescriptor description = (CronTaskDescriptor) tasks.getCron().get(0);
8282
assertThat(description.getExpression()).isEqualTo("0 0 0/6 1/1 * ?");
83-
assertThat(description.getRunnable().getTarget()).isEqualTo(CronTriggerRunnable.class.getName());
83+
assertThat(description.getRunnable().getTarget()).contains(CronTriggerRunnable.class.getName());
8484
});
8585
}
8686

@@ -109,7 +109,7 @@ void fixedDelayTriggerIsReported() {
109109
FixedDelayTaskDescriptor description = (FixedDelayTaskDescriptor) tasks.getFixedDelay().get(0);
110110
assertThat(description.getInitialDelay()).isEqualTo(2000);
111111
assertThat(description.getInterval()).isEqualTo(1000);
112-
assertThat(description.getRunnable().getTarget()).isEqualTo(FixedDelayTriggerRunnable.class.getName());
112+
assertThat(description.getRunnable().getTarget()).contains(FixedDelayTriggerRunnable.class.getName());
113113
});
114114
}
115115

@@ -123,7 +123,7 @@ void noInitialDelayFixedDelayTriggerIsReported() {
123123
FixedDelayTaskDescriptor description = (FixedDelayTaskDescriptor) tasks.getFixedDelay().get(0);
124124
assertThat(description.getInitialDelay()).isEqualTo(0);
125125
assertThat(description.getInterval()).isEqualTo(1000);
126-
assertThat(description.getRunnable().getTarget()).isEqualTo(FixedDelayTriggerRunnable.class.getName());
126+
assertThat(description.getRunnable().getTarget()).contains(FixedDelayTriggerRunnable.class.getName());
127127
});
128128
}
129129

@@ -152,7 +152,7 @@ void fixedRateTriggerIsReported() {
152152
FixedRateTaskDescriptor description = (FixedRateTaskDescriptor) tasks.getFixedRate().get(0);
153153
assertThat(description.getInitialDelay()).isEqualTo(3000);
154154
assertThat(description.getInterval()).isEqualTo(2000);
155-
assertThat(description.getRunnable().getTarget()).isEqualTo(FixedRateTriggerRunnable.class.getName());
155+
assertThat(description.getRunnable().getTarget()).contains(FixedRateTriggerRunnable.class.getName());
156156
});
157157
}
158158

@@ -166,7 +166,7 @@ void noInitialDelayFixedRateTriggerIsReported() {
166166
FixedRateTaskDescriptor description = (FixedRateTaskDescriptor) tasks.getFixedRate().get(0);
167167
assertThat(description.getInitialDelay()).isEqualTo(0);
168168
assertThat(description.getInterval()).isEqualTo(2000);
169-
assertThat(description.getRunnable().getTarget()).isEqualTo(FixedRateTriggerRunnable.class.getName());
169+
assertThat(description.getRunnable().getTarget()).contains(FixedRateTriggerRunnable.class.getName());
170170
});
171171
}
172172

@@ -178,7 +178,7 @@ void taskWithCustomTriggerIsReported() {
178178
assertThat(tasks.getFixedRate()).isEmpty();
179179
assertThat(tasks.getCustom()).hasSize(1);
180180
CustomTriggerTaskDescriptor description = (CustomTriggerTaskDescriptor) tasks.getCustom().get(0);
181-
assertThat(description.getRunnable().getTarget()).isEqualTo(CustomTriggerRunnable.class.getName());
181+
assertThat(description.getRunnable().getTarget()).contains(CustomTriggerRunnable.class.getName());
182182
assertThat(description.getTrigger()).isEqualTo(CustomTriggerTask.trigger.toString());
183183
});
184184
}

0 commit comments

Comments
 (0)