Skip to content

Commit d990449

Browse files
committed
Improve toString for reactive ScheduledTask
Prior to this commit, the reactive Scheduled tasks would be wrapped as a `SubscribingRunnable` which does not implement a custom `toString`. This would result in task metadata using the default Java `toString` representation for those. This commit ensures that the bean class name and method name are used for this `toString`. Closes gh-34010
1 parent 15dcc44 commit d990449

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

Diff for: spring-context/src/main/java/org/springframework/scheduling/annotation/ScheduledAnnotationReactiveSupport.java

+13-4
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ public static Runnable createSubscriptionRunnable(Method method, Object targetBe
123123
Publisher<?> publisher = getPublisherFor(method, targetBean);
124124
Supplier<ScheduledTaskObservationContext> contextSupplier =
125125
() -> new ScheduledTaskObservationContext(targetBean, method);
126+
String displayName = targetBean.getClass().getName() + "." + method.getName();
126127
return new SubscribingRunnable(publisher, shouldBlock, scheduled.scheduler(),
127-
subscriptionTrackerRegistry, observationRegistrySupplier, contextSupplier);
128+
subscriptionTrackerRegistry, displayName, observationRegistrySupplier, contextSupplier);
128129
}
129130

130131
/**
@@ -192,6 +193,8 @@ static final class SubscribingRunnable implements SchedulingAwareRunnable {
192193

193194
final boolean shouldBlock;
194195

196+
final String displayName;
197+
195198
@Nullable
196199
private final String qualifier;
197200

@@ -202,12 +205,13 @@ static final class SubscribingRunnable implements SchedulingAwareRunnable {
202205
final Supplier<ScheduledTaskObservationContext> contextSupplier;
203206

204207
SubscribingRunnable(Publisher<?> publisher, boolean shouldBlock,
205-
@Nullable String qualifier, List<Runnable> subscriptionTrackerRegistry,
206-
Supplier<ObservationRegistry> observationRegistrySupplier,
207-
Supplier<ScheduledTaskObservationContext> contextSupplier) {
208+
@Nullable String qualifier, List<Runnable> subscriptionTrackerRegistry,
209+
String displayName, Supplier<ObservationRegistry> observationRegistrySupplier,
210+
Supplier<ScheduledTaskObservationContext> contextSupplier) {
208211

209212
this.publisher = publisher;
210213
this.shouldBlock = shouldBlock;
214+
this.displayName = displayName;
211215
this.qualifier = qualifier;
212216
this.subscriptionTrackerRegistry = subscriptionTrackerRegistry;
213217
this.observationRegistrySupplier = observationRegistrySupplier;
@@ -253,6 +257,11 @@ private void subscribe(TrackingSubscriber subscriber, Observation observation) {
253257
this.publisher.subscribe(subscriber);
254258
}
255259
}
260+
261+
@Override
262+
public String toString() {
263+
return this.displayName;
264+
}
256265
}
257266

258267

Diff for: spring-context/src/test/java/org/springframework/scheduling/annotation/ScheduledAnnotationReactiveSupportTests.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-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.
@@ -173,6 +173,17 @@ void hasCheckpointToString() {
173173
assertThat(p).hasToString("checkpoint(\"@Scheduled 'mono()' in 'org.springframework.scheduling.annotation.ScheduledAnnotationReactiveSupportTests$ReactiveMethods'\")");
174174
}
175175

176+
@Test
177+
void shouldProvideToString() {
178+
ReactiveMethods target = new ReactiveMethods();
179+
Method m = ReflectionUtils.findMethod(ReactiveMethods.class, "mono");
180+
Scheduled cron = AnnotationUtils.synthesizeAnnotation(Map.of("cron", "-"), Scheduled.class, null);
181+
List<Runnable> tracker = new ArrayList<>();
182+
183+
assertThat(createSubscriptionRunnable(m, target, cron, () -> ObservationRegistry.NOOP, tracker))
184+
.hasToString("org.springframework.scheduling.annotation.ScheduledAnnotationReactiveSupportTests$ReactiveMethods.mono");
185+
}
186+
176187

177188
static class ReactiveMethods {
178189

0 commit comments

Comments
 (0)