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

Feature: Support Spring @Scheduled annotation as a way of instantiating Recurring tasks #509

Open
kagkarlsson opened this issue Jul 1, 2024 · 6 comments

Comments

@kagkarlsson
Copy link
Owner

Support Spring @Scheduled annotation as a way of instantiating Recurring tasks. Makes for a convenient and compact way to define recurring tasks

@iluwa
Copy link
Contributor

iluwa commented Jul 5, 2024

I like the idea! Potentially it'd reduce a big portion of boilerplate.
I'd like to work on the feature

@kagkarlsson
Copy link
Owner Author

Feel free to do so! The help is appreciated!

I haven't looked at how the integration works, or if there are any potential pitfalls for db-scheduler, so that is part of the job 😄

@kagkarlsson
Copy link
Owner Author

If it turns out that it is not possible to hook into Spring's @Scheduled annotation, then we can consider creating our own custom annotation, e.g @RecurringTask(name="my-task", cron="...")

@iluwa
Copy link
Contributor

iluwa commented Jul 8, 2024

Thank you for looking into this. I'm curious, is it because that the db-scheduler can't implement "fixed-interval" stuff from this annotation (like fixedRate, fixedDelay)?

Initially I was planning to put the feature into spring-boot-module. But since it's not limited by spring anymore, the core package looks like a more suitable place.

@kagkarlsson
Copy link
Owner Author

fixedRate might be difficult. It is possible to create a Schedule supporting it but may have quirks (when execution-duration > fixedRate).

It may be that we need to get a class-instance containing the annotated method from something like the spring context, since it may have dependencies and such. In that case it will have to reside in the spring-boot-module. Not sure exactly how it would look if put in core.. 🤔

@kamko
Copy link

kamko commented Aug 29, 2024

I was exploring how to implement an annotation to serve as a more or less drop-in replacement for Spring's Scheduled cron jobs.

fixedRate might be difficult. It is possible to create a schedule supporting it, but it may have quirks (e.g., when execution duration exceeds fixedRate).

I also think the polling strategy might pose some challenges. The biggest issue, however, would be that having db-scheduler hook onto the Scheduled annotation would require disabling Spring's annotation-driven scheduling. If we cannot achieve a 100% drop-in replacement, it might be better to create our own annotation.

I'm not sure how the annotation could work without Spring. Where would we get the actual class instances to run the methods? I don't think that having annotation without any actual way to use it is sufficient.

I have experimented with this, and by using BeanDefinitionRegistryPostProcessor, you can scan for beans with annotated methods and dynamically create recurring tasks, which would be injected with Spring dependencies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants