Is there a possibility of leak with #Scheduled backlog? - spring

I am currently reviewing options to develop looped method execution and one suggestion was to make use of simple Spring #Scheduled annotation. So if I use this approach and use #Scheduled(fixedRate = 1000) in which task scheduled to run each second, If in case each task execution is taking more than second and if there is a huge backlog of scheduled tasks in millions, can it cause some sort of leak?

Related

Spring Boot: How to schedule and execute tasks concurrently?

What I want to is as follows:
In a Spring Boot application,
Schedule tasks (functions, or a method of a class) with cron expressions (cron expressions can be different for each task).
When it's time to run task, run it, concurrently with other task if neccessary (start time is the same, running periods overlaps etc.) - and without any limitation of the concurrency.
The tasks can take several minutes.
The number of tasks (and its options) and cron expressions cannot be determined at development time. It is end user configurable.
The scheduler must satisfy the following requirements.
It must not have a wait queue. if a scheduled time arrives, the task must be executed immediately (Don't worry about the number of the threads).
When the tasks are not running, the number of idle threads should be minimal - or the number should be controllable.
I've looked at ThreadPoolTaskScheduler, but it seems that it fails to satisfy the above requirements.
Thank you in advance.

Problem with Quartz and Tasks Simultaneous

I have some problems with a project that includes Spring Framework and Quartz tasks. I have some tasks that starts each one minute and a have a loop with 3 companies (object Company) inside this loop.
Each company must starts one particular task simultaneously, but generally, these 3 taks have duration longer than one minute.
Then a need these 3 tasks continue executing simultaneously (3 threads at the same time), but each one must wait their similar tasks (new tasks after one minute) finish and then it starts again.
I'm using Spring and I'm using taskExecutor to start de new thread. I dont know if its the best way to call the job class.
I hope I was clear.

Spring #Scheduled - run concurrently

I have a Spring boot 2.1.6.RELEASE application in which I have a method annotated with
#Scheduled(cron = "*/10 * * * * *}
I want it to run with that cron, EVEN IF another execution is already in progress.
I tried increasing the executor thread number using the application.properties file:
spring.task.scheduling.pool.size=10
But it didn't seem to work as it is still waiting for an execution to finish before starting the next one.
What is the proper way to do parallel executions using a cron in the #Scheduled annotation?
It is true that the default pool size for the task scheduler is 1 but increasing this pool size is only making more threads available for other #Scheduled methods. The intended behaviour is not for methods to run in parallel as otherwise threads could become exhausted.
If you wish to change this behaviour to allow the same method to run in parallel you need to use #EnableAsync and #Async annotations. You might also want to change the pool size of the task executor. That being said, keep in mind that you may still exhaust your threads so be very careful with changing this intended behaviour.

Anyway to use quarkus-scheduler #Scheduled with rate under the second?

I am trying to schedule something than need to be ran every 200ms:
#Scheduled(every = "0.2s")
What's the recommended way to do it?
Is there a good reason why #Scheduled doesn't support rate under the second?
Unfortunately, this is not supported. The idea is that #Scheduled is only used for cron-like tasks with an accuracy of seconds (note that the Cron standard is designed to work with an accuracy of minutes).
However, you can inject a managed Vertx instance and make use of Vertx.setPeriodic(). But keep in mind that the handler is executed on an event loop thread and so the code should not block. If you really need to execute some blocking code then look at Vertx.executeBlocking().
The plan is to add an injectable managed ScheduledExecutorService for similar use cases.

Spring Boot, Cron job synchronization

In my Spring Boot application, based on the Cron job(runs every 5 minutes) I need to process 2000 products in my database.
Right now the process time of these 2000 products takes more than 5 minutes. I ran into the issue where the second Cron job runs when the first one is not completed yet.
Is there in Spring/Cron out of the box functionality that will allow to synchronize these jobs and wait for the previous job completion before starting the next one?
Please advise how to properly implement such kind of system. Anyway, the following technologies are also available Neo4j, MongoDB, Kafka. Please advise how to properly design/implement this functionality using the Spring/Cron separately or even together with the mentioned technologies.
1) You may try to use #Scheduled(fixedDelay = 5*60*1000). It will guarantee that next invocation will happen strictly in 5 minutes after previous one is finished. But this may break your scheduling requirements
2) You can limit the underlying ThreadExecutor's pool size to 1 thread, so next invocation will have to wait until previous is finished, but this, again, can break the logic, since it would affect all periodic tasks invoked by #Scheduled
3) You can use Quartz instead of spring's native #Scheduled. It's more complicated to configure, but allows to achieve the desired behaviour via #DisallowConcurrentExecution annotation or via setting JobDetail::isConcurrentExectionDisallowed in your job details

Resources