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

@Scheduled not found when in parent classes #38781

Closed
edeandrea opened this issue Feb 14, 2024 · 8 comments · Fixed by #38791
Closed

@Scheduled not found when in parent classes #38781

edeandrea opened this issue Feb 14, 2024 · 8 comments · Fixed by #38791
Labels
area/scheduler kind/bug Something isn't working
Milestone

Comments

@edeandrea
Copy link
Contributor

edeandrea commented Feb 14, 2024

Describe the bug

When using the @Scheduled annotation on a method, it is not found/picked up when the scheduled method is on a parent class of a CDI bean.

Expected behavior

I would expect any @Scheduled method in the class hierarchy of a CDI bean to be found and executed.

Actual behavior

Only @Scheduled methods within the top-level class of a CDI bean are found/executed

How to Reproduce?

  1. Download and unzip the reproducer: scheduled.zip
  2. Examine the project.
    • There is a class called BeanWithScheduled which is simple CDI bean with an @Scheduled method.
    • There is a class called ChildBean which has an @Scheduled method and also extends a base class, SubclassWithScheduled.
    • SubclassWithScheduled also has an @Scheduled method.
  3. Run ./mvnw clean quarkus:dev
  4. You'll notice the following output, indicating that only the @Scheduled methods in the BeanWithScheduled and ChildBean classes were executed. The @Scheduled method in the SubclassWithScheduled base class was not executed.
__  ____  __  _____   ___  __ ____  ______ 
 --/ __ \/ / / / _ | / _ \/ //_/ / / / __/ 
 -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \   
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/   
2024-02-14 09:39:21,697 INFO  [io.quarkus] (Quarkus Main Thread) scheduled 1.0-SNAPSHOT on JVM (powered by Quarkus 3.7.2) started in 0.563s. 

2024-02-14 09:39:21,698 INFO  [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
2024-02-14 09:39:21,698 INFO  [io.quarkus] (Quarkus Main Thread) Installed features: [cdi, scheduler, smallrye-context-propagation, vertx]
2024-02-14 09:39:22,022 INFO  [BeanWithScheduled] (vert.x-worker-thread-1) Running scheduled task from main bean without a parent class
2024-02-14 09:39:22,022 INFO  [ChildBean] (vert.x-worker-thread-2) Running scheduled task from main bean with a parent class
2024-02-14 09:39:27,010 INFO  [ChildBean] (vert.x-worker-thread-2) Running scheduled task from main bean with a parent class
2024-02-14 09:39:27,010 INFO  [BeanWithScheduled] (vert.x-worker-thread-1) Running scheduled task from main bean without a parent class

Output of uname -a or ver

Darwin edeandrea-m1pro 23.3.0 Darwin Kernel Version 23.3.0: Wed Dec 20 21:30:44 PST 2023; root:xnu-10002.81.5~7/RELEASE_ARM64_T6000 arm64

Output of java -version

openjdk version "17.0.10" 2024-01-16
OpenJDK Runtime Environment Temurin-17.0.10+7 (build 17.0.10+7)
OpenJDK 64-Bit Server VM Temurin-17.0.10+7 (build 17.0.10+7, mixed mode)

Quarkus version or git rev

3.7.2

Build tool (ie. output of mvnw --version or gradlew --version)

Apache Maven 3.9.6 (bc0240f3c744dd6b6ec2920b3cd08dcc295161ae)
Maven home: /Users/edeandre/.m2/wrapper/dists/apache-maven-3.9.6-bin/3311e1d4/apache-maven-3.9.6
Java version: 17.0.10, vendor: Eclipse Adoptium, runtime: /Users/edeandre/.sdkman/candidates/java/17.0.10-tem
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "14.3", arch: "aarch64", family: "mac"

Additional information

No response

@edeandrea edeandrea added the kind/bug Something isn't working label Feb 14, 2024
Copy link

quarkus-bot bot commented Feb 14, 2024

/cc @mkouba (scheduler)

@mkouba
Copy link
Contributor

mkouba commented Feb 14, 2024

Well, I don't remember all the details but we initially decided not to support the inheritance of @Scheduled methods.

It would definitely make the discovery and validation more complex. Also if there are multiple subclasses inheriting a scheduled method from a superclass you wouldn't be able to declare the @Scheduled#identity() which has to be unique.

As a workaround, you can override the scheduled method in the subclass (ChildBean in your reproducer) and re-declare the @Scheduled annotation 🤷.

@edeandrea
Copy link
Contributor Author

edeandrea commented Feb 14, 2024

Yeah I had just thought of that workaround and thats most likely what I'll do. I don't really know the history as to why it was excluded, but I'm sure there are pros/cons on each side of including it or not. I was just surprised that it didn't work and spent half a day trying to figure out why my code wasn't working.

If it isn't going to be supported in parent classes, maybe that should be called out in the docs?

@mkouba
Copy link
Contributor

mkouba commented Feb 15, 2024

If it isn't going to be supported in parent classes, maybe that should be called out in the docs?

That's a good idea. Actually, we can also detect the case where a @Scheduled method is declared on an abstract class (your reproducer)/interface and fail the build. I'll send a PR.

@mkouba
Copy link
Contributor

mkouba commented Feb 15, 2024

Hm, actually it is documented in the reference guide - there is a warning that starts with text "Subclasses never inherit the metadata of a @Scheduled method declared on a superclass.". It got added in #24270 which a fix for #24212 and removes the inheritance for the reasons described in the comments.

So I will:

  1. Turn the warning into a subsection with title Inheritance of metadata,
  2. Mention this in the javadoc of io.quarkus.scheduler.Scheduled as well,
  3. Implement a validation rule to fail the build if @Scheduled is declared on a method of an abstract class/interface

mkouba added a commit to mkouba/quarkus that referenced this issue Feb 15, 2024
- turn the warning in the docs into a subsection
- mention the inheritance rules in the javadoc of the Scheduled
annotation
- fail the build if an abstract class/interface declares a method
annotated with Scheduled
- add support for static interface scheduled methods (so far only
static methods declared on a class were supported)
- fixes quarkusio#38781
@quarkus-bot quarkus-bot bot added this to the 3.9 - main milestone Feb 15, 2024
@edeandrea
Copy link
Contributor Author

Is it just when declared on a method of an abstract class? Or anywhere in the parent class hierarchy?

@mkouba
Copy link
Contributor

mkouba commented Feb 15, 2024

Is it just when declared on a method of an abstract class? Or anywhere in the parent class hierarchy?

An abstract class or an interface. A superclass that is not abstract might be a legal use case.

@edeandrea
Copy link
Contributor Author

Thanks very much @mkouba for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/scheduler kind/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants