简体   繁体   English

Spring任务执行器和调度器

[英]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. 我需要知道的是Spring任务执行程序和调度程序之间的关系。 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? 如果由调度程序运行任务,那么在这种配置中执行程序是否多余?

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 ). scheduler名称空间创建ThreadPoolTaskScheduler的实例,该实例能够处理任务本身( 因为它实现了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 ) 调度程序和执行程序之间没有这种关系,除了它们都实现了旨在异步执行任务的AsyncTaskExecutor请注意, 执行程序名称空间创建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 . 仅当您计划在应用程序中计划任务时才是冗余的,否则它将用于标记为@Async任何任务。

Hope this answers your queries, do let know in comments if more information is required. 希望这能回答您的问题,如果需要更多信息,请在评论中告知我们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM