Skip to content

Commit f30d9e0

Browse files
committed
Scheduler: register static Scheduled methods correctly
- fixes #47738
1 parent d26c932 commit f30d9e0

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

extensions/scheduler/deployment/src/main/java/io/quarkus/scheduler/deployment/SchedulerProcessor.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.HashSet;
1616
import java.util.List;
1717
import java.util.Map;
18+
import java.util.Map.Entry;
1819
import java.util.Optional;
1920
import java.util.Set;
2021
import java.util.concurrent.CompletableFuture;
@@ -176,6 +177,7 @@ void collectScheduledMethods(BeanArchiveIndexBuildItem beanArchives, BeanDiscove
176177
BuildProducer<ScheduledBusinessMethodItem> scheduledBusinessMethods) {
177178

178179
// First collect static scheduled methods
180+
Map<MethodInfo, List<AnnotationInstance>> staticScheduledMethods = new HashMap<>();
179181
List<AnnotationInstance> schedules = new ArrayList<>(
180182
beanArchives.getIndex().getAnnotations(SchedulerDotNames.SCHEDULED_NAME));
181183
for (AnnotationInstance annotationInstance : beanArchives.getIndex().getAnnotations(SchedulerDotNames.SCHEDULES_NAME)) {
@@ -198,13 +200,23 @@ void collectScheduledMethods(BeanArchiveIndexBuildItem beanArchives, BeanDiscove
198200
method.name(), declaringClass.name()));
199201
}
200202
if (Modifier.isStatic(method.flags()) && !KotlinUtil.isSuspendMethod(method)) {
201-
scheduledBusinessMethods.produce(new ScheduledBusinessMethodItem(null, method, schedules,
202-
transformedAnnotations.hasAnnotation(method, SchedulerDotNames.NON_BLOCKING),
203-
transformedAnnotations.hasAnnotation(method, SchedulerDotNames.RUN_ON_VIRTUAL_THREAD)));
204-
LOGGER.debugf("Found scheduled static method %s declared on %s", method, declaringClass.name());
203+
List<AnnotationInstance> methodSchedules = staticScheduledMethods.get(method);
204+
if (methodSchedules == null) {
205+
methodSchedules = new ArrayList<>();
206+
staticScheduledMethods.put(method, methodSchedules);
207+
}
208+
methodSchedules.add(annotationInstance);
205209
}
206210
}
207211

212+
for (Entry<MethodInfo, List<AnnotationInstance>> e : staticScheduledMethods.entrySet()) {
213+
MethodInfo method = e.getKey();
214+
scheduledBusinessMethods.produce(new ScheduledBusinessMethodItem(null, method, e.getValue(),
215+
transformedAnnotations.hasAnnotation(method, SchedulerDotNames.NON_BLOCKING),
216+
transformedAnnotations.hasAnnotation(method, SchedulerDotNames.RUN_ON_VIRTUAL_THREAD)));
217+
LOGGER.debugf("Found scheduled static method %s declared on %s", method, method.declaringClass().name());
218+
}
219+
208220
// Then collect all business methods annotated with @Scheduled
209221
for (BeanInfo bean : beanDiscovery.beanStream().classBeans()) {
210222
collectScheduledMethods(beanArchives.getIndex(), transformedAnnotations, bean,

extensions/scheduler/deployment/src/test/java/io/quarkus/scheduler/test/staticmethod/ScheduledStaticMethodTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package io.quarkus.scheduler.test.staticmethod;
22

3+
import static org.junit.jupiter.api.Assertions.assertEquals;
34
import static org.junit.jupiter.api.Assertions.assertTrue;
45

56
import java.util.concurrent.CountDownLatch;
67
import java.util.concurrent.TimeUnit;
78

9+
import jakarta.inject.Inject;
10+
811
import org.junit.jupiter.api.Test;
912
import org.junit.jupiter.api.extension.RegisterExtension;
1013

1114
import io.quarkus.scheduler.Scheduled;
15+
import io.quarkus.scheduler.Scheduler;
1216
import io.quarkus.test.QuarkusUnitTest;
1317

1418
public class ScheduledStaticMethodTest {
@@ -18,8 +22,12 @@ public class ScheduledStaticMethodTest {
1822
.withApplicationRoot((jar) -> jar
1923
.addClasses(Jobs.class, AbstractJobs.class, InterfaceJobs.class));
2024

25+
@Inject
26+
Scheduler scheduler;
27+
2128
@Test
2229
public void testSimpleScheduledJobs() throws InterruptedException {
30+
assertEquals(3, scheduler.getScheduledJobs().size());
2331
assertTrue(Jobs.LATCH.await(5, TimeUnit.SECONDS));
2432
assertTrue(AbstractJobs.LATCH.await(5, TimeUnit.SECONDS));
2533
assertTrue(InterfaceJobs.LATCH.await(5, TimeUnit.SECONDS));

0 commit comments

Comments
 (0)