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 Reactive methods #1470

Open
Airidas36 opened this issue Jul 3, 2023 · 3 comments
Open

Feature: Support Reactive methods #1470

Airidas36 opened this issue Jul 3, 2023 · 3 comments

Comments

@Airidas36
Copy link

Is your feature request related to a problem? Please describe.
With the new Spring 6.1.0-M1 version, methods annotated with @Scheduled can now return Publisher types e.g (Mono, Flux, etc.) spring-projects/spring-framework#29924

Currently Shedlock does not support this and the application startup fails with the following message:
Caused by: java.lang.IllegalStateException: Could not create recurring task for @Scheduled method 'reportPendingReports': Cannot obtain a Publisher-convertible value from the @Scheduled reactive method.

You can trick Spring by configuring Shedlock interceptMode to EnableSchedulerLock.InterceptMode.PROXY_SCHEDULER, but then Shedlock autoconfiguration silently fails because SpringLockConfigurationExtractor only supports tasks of type ScheduledMethodRunnable and the task type is org.springframework.scheduling.annotation.ScheduledAnnotationReactiveSupportSubscribingRunnable. Therefore lock configuration is not acquired and DefaultLockManager executes the task without a lock.

@lukas-krecan
Copy link
Owner

Hi, I will look into that. In the meantime, can you please explain what's the use-case for reactive scheduled method. Thanks

@Airidas36
Copy link
Author

Hi, I will look into that. In the meantime, can you please explain what's the use-case for reactive scheduled method. Thanks

Hi,

Previously, if code-base was reactive, it was up to the developer to actually bridge the reactive piece of code with the Spring Scheduler. You would need to explicitly subscribe to / block the reactive pipeline to start a scheduled task. On top of that, fixedDelay was never working correctly since Spring was not actually aware of when the subscribed publisher emmited onComplete/onError and would schedule a new task even if the old one was still running.

This new feature also supports different types of Publishers - and not necessarily the ones from Project Reactor. RxJava, Reactor, Kotlin suspended functions - return types that that can be adapted to Publisher using ReactiveRegistryAdapter (CompletableFuture, Mono, Flux, Flow, Flowable, Completable) are now supported and handled by the ScheduledAnnotationBeanProcessor.

The developer does not need to worry about bridging the piece of reactive code with Springs' Scheduler, it is handled by the framework itself, including subscribing to the provided Publisher, tracking/cancelling/disposing of the subscription.

@lukas-krecan
Copy link
Owner

lukas-krecan commented Jul 4, 2023

Thanks, I looked into that and it's going to be tricky.

  1. Ideally, we should reuse the ScheduledAnnotationReactiveSupport class, but as it will only part of Spring 6.1, ShedLock would have to require Spring 6.1. Morover, the methods are package-protected.
  2. It introduces unnecessary complexity. I try to stay clear of reactive code as it usually brings more problems then it solves.
  3. It will become obsolete as soon as Loom will get out.

So, unless somebody will be able to contribute the code, this is not going to happen.

For those thinking about contributing the code:

  1. Please make sure it's well tested for all cases from here.
  2. Make sure the code is small, well isolated and understandable
  3. Make sure it works will all currently supported versions of Spring

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

No branches or pull requests

2 participants