Skip to content

Commit b73acaa

Browse files
mkoubagsmet
authored andcommitted
Scheduler - detect scheduled methods of the same name on a class
- fix quarkusio#31547 (cherry picked from commit e1f819d)
1 parent 6beba4b commit b73acaa

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
@@ -11,9 +11,11 @@
1111
import java.time.Duration;
1212
import java.util.ArrayList;
1313
import java.util.HashMap;
14+
import java.util.HashSet;
1415
import java.util.List;
1516
import java.util.Map;
1617
import java.util.Optional;
18+
import java.util.Set;
1719
import java.util.concurrent.CompletableFuture;
1820
import java.util.concurrent.CompletionStage;
1921
import java.util.function.Function;
@@ -191,17 +193,23 @@ void validateScheduledBusinessMethods(SchedulerConfig config, List<ScheduledBusi
191193
ValidationPhaseBuildItem validationPhase, BuildProducer<ValidationErrorBuildItem> validationErrors) {
192194
List<Throwable> errors = new ArrayList<>();
193195
Map<String, AnnotationInstance> encounteredIdentities = new HashMap<>();
196+
Set<String> methodDescriptions = new HashSet<>();
194197

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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package io.quarkus.scheduler.test;
2+
3+
import javax.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)