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
This commit is contained in:
Brian Clozel 2024-06-19 16:39:22 +02:00 committed by Andy Wilkinson
parent 305bfb1641
commit 74a2144a37
2 changed files with 8 additions and 16 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,7 +16,6 @@
package org.springframework.boot.actuate.scheduling; package org.springframework.boot.actuate.scheduling;
import java.lang.reflect.Method;
import java.time.Duration; import java.time.Duration;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
@ -46,7 +45,6 @@ import org.springframework.scheduling.config.Task;
import org.springframework.scheduling.config.TriggerTask; import org.springframework.scheduling.config.TriggerTask;
import org.springframework.scheduling.support.CronTrigger; import org.springframework.scheduling.support.CronTrigger;
import org.springframework.scheduling.support.PeriodicTrigger; import org.springframework.scheduling.support.PeriodicTrigger;
import org.springframework.scheduling.support.ScheduledMethodRunnable;
/** /**
* {@link Endpoint @Endpoint} to expose information about an application's scheduled * {@link Endpoint @Endpoint} to expose information about an application's scheduled
@ -284,13 +282,7 @@ public class ScheduledTasksEndpoint {
private final String target; private final String target;
private RunnableDescriptor(Runnable runnable) { private RunnableDescriptor(Runnable runnable) {
if (runnable instanceof ScheduledMethodRunnable scheduledMethodRunnable) { this.target = runnable.toString();
Method method = scheduledMethodRunnable.getMethod();
this.target = method.getDeclaringClass().getName() + "." + method.getName();
}
else {
this.target = runnable.getClass().getName();
}
} }
public String getTarget() { public String getTarget() {

View File

@ -80,7 +80,7 @@ class ScheduledTasksEndpointTests {
assertThat(tasks.getCron()).hasSize(1); assertThat(tasks.getCron()).hasSize(1);
CronTaskDescriptor description = (CronTaskDescriptor) tasks.getCron().get(0); CronTaskDescriptor description = (CronTaskDescriptor) tasks.getCron().get(0);
assertThat(description.getExpression()).isEqualTo("0 0 0/6 1/1 * ?"); 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); FixedDelayTaskDescriptor description = (FixedDelayTaskDescriptor) tasks.getFixedDelay().get(0);
assertThat(description.getInitialDelay()).isEqualTo(2000); assertThat(description.getInitialDelay()).isEqualTo(2000);
assertThat(description.getInterval()).isEqualTo(1000); 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); FixedDelayTaskDescriptor description = (FixedDelayTaskDescriptor) tasks.getFixedDelay().get(0);
assertThat(description.getInitialDelay()).isEqualTo(0); assertThat(description.getInitialDelay()).isEqualTo(0);
assertThat(description.getInterval()).isEqualTo(1000); 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); FixedRateTaskDescriptor description = (FixedRateTaskDescriptor) tasks.getFixedRate().get(0);
assertThat(description.getInitialDelay()).isEqualTo(3000); assertThat(description.getInitialDelay()).isEqualTo(3000);
assertThat(description.getInterval()).isEqualTo(2000); 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); FixedRateTaskDescriptor description = (FixedRateTaskDescriptor) tasks.getFixedRate().get(0);
assertThat(description.getInitialDelay()).isEqualTo(0); assertThat(description.getInitialDelay()).isEqualTo(0);
assertThat(description.getInterval()).isEqualTo(2000); 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.getFixedRate()).isEmpty();
assertThat(tasks.getCustom()).hasSize(1); assertThat(tasks.getCustom()).hasSize(1);
CustomTriggerTaskDescriptor description = (CustomTriggerTaskDescriptor) tasks.getCustom().get(0); 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()); assertThat(description.getTrigger()).isEqualTo(CustomTriggerTask.trigger.toString());
}); });
} }