使用 ThreadPoolExecutor 应用程序范围请求范围的 ExecutorChannel 的池大小

Is the pool-size of an ExecutorChannel using ThreadPoolExecutor application wide request wide

目前,我正在使用 executor channel with an executor 作为拆分器的输出通道,它将一条消息拆分为 2 条不同的消息,以便 运行 并行。

我已经设置了输出通道,如下所示:

<int:channel id="splitter_output">
   <int:dispatcher task-executor="executor"/>
</int:channel>

<task:executor id="executor" pool-size="4"/>

我一直无法完全理解当同时发送多个请求时这个池大小是如何工作的。如果我要通过此流程向我的应用程序发送 1 个请求,则会在 "splitter_output" 通道中产生 2 条不同的消息。如果我要向我的应用程序发送 3 个请求,它将在 "splitter_output" 通道中产生 6 条不同的消息,因为每个请求都流入一个拆分器,该拆分器将消息拆分为两个单独的消息。

这个池大小是按请求设置的,每个请求都会产生两个执行程序线程以继续向下的流程吗?

或者它是应用程序范围的,前两个请求将导致创建 (4) 个线程并 运行 通过流程,然后一旦其中一个请求完成,第三个请求将创建 (2 ) 线程并继续向下流动?

首先,它不是 Spring 集成功能,而是 Spring 框架核心功能。

这是关于此事的文档:https://docs.spring.io/spring/docs/5.2.3.RELEASE/spring-framework-reference/integration.html#scheduling

关于pool-size,见其描述:

The size of the executor's thread pool as either a single value or a range
(e.g. 5-10). If no bounded queue-capacity value is provided, then a max value
has no effect unless the range is specified as 0-n. In that case, the core pool
will have a size of n, but the 'allowCoreThreadTimeout' flag will be set to true.
If a queue-capacity is provided, then the lower bound of a range will map to the
core size and the upper bound will map to the max size. If this attribute is not
provided, the default core size will be 1, and the default max size will be
Integer.MAX_VALUE (i.e. unbounded).

<task:executor> 得到 ThreadPoolTaskExecutor 的支持。这是它的 JavaDocs:

 * JavaBean that allows for configuring a {@link java.util.concurrent.ThreadPoolExecutor}
 * in bean style (through its "corePoolSize", "maxPoolSize", "keepAliveSeconds", "queueCapacity"
 * properties) and exposing it as a Spring {@link org.springframework.core.task.TaskExecutor}.
 * This class is also well suited for management and monitoring (e.g. through JMX),
 * providing several useful attributes: "corePoolSize", "maxPoolSize", "keepAliveSeconds"
 * (all supporting updates at runtime); "poolSize", "activeCount" (for introspection only).

我不确定是什么让您认为线程行为是关于 "per-request" 的。事实上,它是一个具有 4 线程配置的全局单例 bean,每个想要在此执行器上执行任务的人都必须与所有其他人共享线程。因此,无论您的请求数量如何,这里只有 4 个线程可以工作。其他一切都在内部队列中等待。