Kamil Przybyo
Kamil Przybyo

Reputation: 115

Spring task executor and scheduler

I wasn't able to find necessary information, neither in documentation or in already existing questions here, that's why I'm creating one myself (I can't ask questions under similar posts yet).

What I need to know is relationship between Spring task executor and scheduler. My current configuration looks like this:

<task:executor
        id="executor"
        pool-size="1-2"
        queue-capacity="50"
        rejection-policy="CALLER_RUNS"
/>
<task:scheduler id="scheduler" pool-size="2"/>

<task:scheduled-tasks scheduler="scheduler">
    <task:scheduled ref="task1" method="methodInTask1" cron="0 1/5 * ? * *"/>
    <task:scheduled ref="task2" method="methodInTask2" cron="0 0/5 * ? * *"/>
</task:scheduled-tasks>

What I'm not sure is how it works. "Who" does run my tasks? Is it scheduler, as task are scheduled with him? Or scheduler is only creating them, placing in queue and excutor runs them?

If not, and the running one is scheduler, I have to create annotations above specific classes and their methods, so that they can be launched by executor?

There is no clear explanation to how they are related, in documentation, neither is for "pool-size", but it can be found in others questions at least. If it's scheduler who runs the tasks is executor in this configuration redundant?

Upvotes: 4

Views: 4351

Answers (1)

Ammar
Ammar

Reputation: 4024

Your queries are well addressed in the documentation (refer this section for complete behavioral aspects)

Answering your specific questions

"Who" does run my tasks? Is it scheduler, as task are scheduled with him? Or scheduler is only creating them, placing in queue and excutor runs them?

The scheduler namespace creates an instance of ThreadPoolTaskScheduler which is capable of handling the task execution itself (as it implements the AsyncTaskExecutor). Thus the scheduler executes the tasks itself with no help of executor.

There is no clear explanation to how they are related

There is no relation as such between scheduler and executor apart from the fact that they both implement AsyncTaskExecutor meant to execute tasks asynchronously (on side note - the executor namespace creates an instance of ThreadPoolTaskExecutor)

neither is for "pool-size"

This section will provide you with the relevant details.

If it's scheduler who runs the tasks is executor in this configuration redundant?

Redundant only if you plan to have scheduled task in your application, else it will be utilized for any task marked as @Async.

Hope this answers your queries, do let know in comments if more information is required.

Upvotes: 4

Related Questions