Skip to content

Commit

Permalink
Merge pull request #24270 from mkouba/issue-24212
Browse files Browse the repository at this point in the history
Scheduler - scheduled methods are not inherited
  • Loading branch information
mkouba authored Mar 11, 2022
2 parents 3097830 + 8158210 commit d996c2a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
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 {

@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();
}
}
}

0 comments on commit d996c2a

Please sign in to comment.