Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scheduler - detect scheduled methods of the same name on a class #31551

Merged
merged 1 commit into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,8 @@ public boolean isNonBlocking() {
return nonBlocking;
}

public String getMethodDescription() {
return method.declaringClass().name() + "#" + method.name() + "()";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.function.Function;
Expand Down Expand Up @@ -192,17 +194,23 @@ void validateScheduledBusinessMethods(SchedulerConfig config, List<ScheduledBusi
ValidationPhaseBuildItem validationPhase, BuildProducer<ValidationErrorBuildItem> validationErrors) {
List<Throwable> errors = new ArrayList<>();
Map<String, AnnotationInstance> encounteredIdentities = new HashMap<>();
Set<String> methodDescriptions = new HashSet<>();

for (ScheduledBusinessMethodItem scheduledMethod : scheduledMethods) {
if (!methodDescriptions.add(scheduledMethod.getMethodDescription())) {
errors.add(new IllegalStateException("Multiple @Scheduled methods of the same name declared on the same class: "
+ scheduledMethod.getMethodDescription()));
continue;
}
MethodInfo method = scheduledMethod.getMethod();
if (Modifier.isAbstract(method.flags())) {
errors.add(new IllegalStateException("@Scheduled method must not be abstract: "
+ method.declaringClass().name() + "#" + method.name() + "()"));
+ scheduledMethod.getMethodDescription()));
continue;
}
if (Modifier.isPrivate(method.flags())) {
errors.add(new IllegalStateException("@Scheduled method must not be private: "
+ method.declaringClass().name() + "#" + method.name() + "()"));
+ scheduledMethod.getMethodDescription()));
continue;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.quarkus.scheduler.test;

import jakarta.enterprise.inject.spi.DeploymentException;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.scheduler.Scheduled;
import io.quarkus.scheduler.ScheduledExecution;
import io.quarkus.test.QuarkusUnitTest;

public class MultipleScheduledMethodsWithTheSameNameTest {

@RegisterExtension
static final QuarkusUnitTest test = new QuarkusUnitTest()
.setExpectedException(DeploymentException.class)
.withApplicationRoot((jar) -> jar
.addClasses(BeanWithInvalidScheduledMethods.class));

@Test
public void test() throws InterruptedException {
}

static class BeanWithInvalidScheduledMethods {

@Scheduled(cron = "0/1 * * * * ?")
void foo() {
}

@Scheduled(every = "10s")
void foo(ScheduledExecution execution) {
}

}

}