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 - scheduled methods are not inherited #24270

Merged
merged 1 commit into from
Mar 11, 2022
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
20 changes: 19 additions & 1 deletion docs/src/main/asciidoc/scheduler-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,26 @@ Furthermore, the annotated method must return `void` and either declare no param

TIP: The annotation is repeatable so a single method could be scheduled multiple times.

TIP: A CDI event of type `io.quarkus.scheduler.SuccessfulExecution` is fired synchronously and asynchronously when an execution of a scheduled method is successful. A CDI event of type `io.quarkus.scheduler.FailedExecution` is fired synchronously and asynchronously when an execution of a scheduled method throws an exception.
[WARNING]
====
Subclasses never inherit the metadata of a `@Scheduled` method declared on a superclass. In the following example, the `everySecond()` method is only invoked upon the instance of `Jobs`.
[source,java]
----
class Jobs {

@Scheduled(every = "1s")
void everySecond() {
// ..do something
}
}

@Singleton
class MyJobs extends Jobs {
}
----
====

A CDI event of type `io.quarkus.scheduler.SuccessfulExecution` is fired synchronously and asynchronously when an execution of a scheduled method is successful. A CDI event of type `io.quarkus.scheduler.FailedExecution` is fired synchronously and asynchronously when an execution of a scheduled method throws an exception.

=== Triggers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,6 @@ private void collectScheduledMethods(IndexView index, TransformedAnnotationsBuil
LOGGER.debugf("Found scheduled business method %s declared on %s", method, bean);
}
}

DotName superClassName = beanClass.superName();
if (superClassName != null) {
ClassInfo superClass = index.getClassByName(superClassName);
if (superClass != null) {
collectScheduledMethods(index, transformedAnnotations, bean, superClass, scheduledBusinessMethods);
}
}
}

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

import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

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

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

public class ScheduledMethodNotInheritedTest {
machi1990 marked this conversation as resolved.
Show resolved Hide resolved

@RegisterExtension
static final QuarkusUnitTest test = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(Jobs.class, MyJobs.class));

@Test
public void testExecution() throws InterruptedException {
assertTrue(Jobs.LATCH.await(5, TimeUnit.SECONDS));
assertTrue(Jobs.JOB_CLASSES.stream().allMatch(s -> s.equals(Jobs.class.getName())));
}

@Singleton
static class MyJobs extends Jobs {

}

static class Jobs {

static final CountDownLatch LATCH = new CountDownLatch(2);
static final List<String> JOB_CLASSES = new CopyOnWriteArrayList<>();

@Scheduled(every = "1s", identity = "foo")
void everySecond() {
JOB_CLASSES.add(getClass().getName());
LATCH.countDown();
}
}
}