Spring 任务执行器和调度器

Spring task executor and scheduler

我找不到必要的信息,无论是在文档中还是在此处已经存在的问题中,这就是我自己创建一个的原因(我还不能在类似的帖子下提问)。

我需要知道的是 Spring 任务执行器和调度器之间的关系。 我当前的配置如下所示:

<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>

我不确定它是如何工作的。 "Who" 运行 我的任务吗?难道是scheduler,as task都跟他一起安排?或者调度程序只创建它们,将它们放入队列和执行器 运行s 它们?

如果不是,运行ning 一个是调度程序,我必须在特定 类 及其方法之上创建注释,以便执行程序可以启动它们?

在文档中没有明确解释它们之间的关系,"pool-size"也没有,但至少可以在其他问题中找到。如果是 运行 执行任务的调度程序在此配置中是多余的?

文档中很好地解决了您的问题(请参阅 this section 了解完整的行为方面

回答您的具体问题

"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?

scheduler namespace creates an instance of ThreadPoolTaskScheduler which is capable of handling the task execution itself (as it implements the AsyncTaskExecutor)。因此,调度程序在没有执行程序帮助的情况下自行执行任务。

There is no clear explanation to how they are related

调度程序和执行程序之间没有任何关系,除了它们都实现 AsyncTaskExecutor 意味着异步执行任务(旁注 - executor namespace creates an instance of ThreadPoolTaskExecutor)

neither is for "pool-size"

section将为您提供相关详情。

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

仅当您计划在应用程序中安排任务时才冗余,否则它将用于标记为 @Async 的任何任务。

希望这能回答您的问题,如果需要更多信息,请在评论中告知。