Skip to content

Commit e1f819d

Browse files
committed
Scheduler - detect scheduled methods of the same name on a class
- fix #31547
1 parent 9207c6b commit e1f819d

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,8 @@ public boolean isNonBlocking() {
4848
return nonBlocking;
4949
}
5050

51+
public String getMethodDescription() {
52+
return method.declaringClass().name() + "#" + method.name() + "()";
53+
}
54+
5155
}

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
import java.time.ZoneId;
1313
import java.util.ArrayList;
1414
import java.util.HashMap;
15+
import java.util.HashSet;
1516
import java.util.List;
1617
import java.util.Map;
1718
import java.util.Optional;
19+
import java.util.Set;
1820
import java.util.concurrent.CompletableFuture;
1921
import java.util.concurrent.CompletionStage;
2022
import java.util.function.Function;
@@ -192,17 +194,23 @@ void validateScheduledBusinessMethods(SchedulerConfig config, List<ScheduledBusi
192194
ValidationPhaseBuildItem validationPhase, BuildProducer<ValidationErrorBuildItem> validationErrors) {
193195
List<Throwable> errors = new ArrayList<>();
194196
Map<String, AnnotationInstance> encounteredIdentities = new HashMap<>();
197+
Set<String> methodDescriptions = new HashSet<>();
195198

196199
for (ScheduledBusinessMethodItem scheduledMethod : scheduledMethods) {
200+
if (!methodDescriptions.add(scheduledMethod.getMethodDescription())) {
201+
errors.add(new IllegalStateException("Multiple @Scheduled methods of the same name declared on the same class: "
202+
+ scheduledMethod.getMethodDescription()));
203+
continue;
204+
}
197205
MethodInfo method = scheduledMethod.getMethod();
198206
if (Modifier.isAbstract(method.flags())) {
199207
errors.add(new IllegalStateException("@Scheduled method must not be abstract: "
200-
+ method.declaringClass().name() + "#" + method.name() + "()"));
208+
+ scheduledMethod.getMethodDescription()));
201209
continue;
202210
}
203211
if (Modifier.isPrivate(method.flags())) {
204212
errors.add(new IllegalStateException("@Scheduled method must not be private: "
205-
+ method.declaringClass().name() + "#" + method.name() + "()"));
213+
+ scheduledMethod.getMethodDescription()));
206214
continue;
207215
}
208216

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.quarkus.scheduler.test;
2+
3+
import jakarta.enterprise.inject.spi.DeploymentException;
4+
5+
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.api.extension.RegisterExtension;
7+
8+
import io.quarkus.scheduler.Scheduled;
9+
import io.quarkus.scheduler.ScheduledExecution;
10+
import io.quarkus.test.QuarkusUnitTest;
11+
12+
public class MultipleScheduledMethodsWithTheSameNameTest {
13+
14+
@RegisterExtension
15+
static final QuarkusUnitTest test = new QuarkusUnitTest()
16+
.setExpectedException(DeploymentException.class)
17+
.withApplicationRoot((jar) -> jar
18+
.addClasses(BeanWithInvalidScheduledMethods.class));
19+
20+
@Test
21+
public void test() throws InterruptedException {
22+
}
23+
24+
static class BeanWithInvalidScheduledMethods {
25+
26+
@Scheduled(cron = "0/1 * * * * ?")
27+
void foo() {
28+
}
29+
30+
@Scheduled(every = "10s")
31+
void foo(ScheduledExecution execution) {
32+
}
33+
34+
}
35+
36+
}

0 commit comments

Comments
 (0)