Skip to content

Commit

Permalink
Adapt to Task and ScheduledTask changes in Framework
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bclozel authored and snicoll committed Jun 19, 2024
1 parent e44b521 commit 1493bef
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -46,7 +45,6 @@
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
Expand Down Expand Up @@ -284,13 +282,7 @@ public static final class RunnableDescriptor {
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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void cronTriggerIsReported() {
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());
});
}

Expand Down Expand Up @@ -109,7 +109,7 @@ void fixedDelayTriggerIsReported() {
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());
});
}

Expand All @@ -123,7 +123,7 @@ void noInitialDelayFixedDelayTriggerIsReported() {
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());
});
}

Expand Down Expand Up @@ -152,7 +152,7 @@ void fixedRateTriggerIsReported() {
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());
});
}

Expand All @@ -166,7 +166,7 @@ void noInitialDelayFixedRateTriggerIsReported() {
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());
});
}

Expand All @@ -178,7 +178,7 @@ void taskWithCustomTriggerIsReported() {
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());
});
}
Expand Down

0 comments on commit 1493bef

Please sign in to comment.