如果出现异常,任务执行线程不会返回到池中。

3

当嵌套链失败并到达错误通道时,任务执行器线程会被阻塞且不会返回池中。有没有办法指示流程已经结束,它们需要返回到池中。

例如,拆分器将有效负载拆分为3条消息。这些消息的服务方式如下 -

message 1 - fileChannelTaskExecutor1
message 2 - fileChannelTaskExecutor2

如果“嵌套链”网关调用成功,则第3条消息将被发送到任何早期释放的执行器线程中。
但是,如果“嵌套链”网关调用失败并到达errChannel,则上述两个执行器线程都会被阻塞并且不会返回到池中。因此,由于没有可用的线程池,后续消息(第3条消息)将无法处理。
<bean id="fileChannelTaskExecutor"
class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
    <property name="corePoolSize" value="2"/>
    <property name="daemon" value="false"/>
</bean>

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

<int:splitter input-channel="splitterRequestChannel" output-channel="splitterResponseChannel" >

<int:chain input-channel="splitterResponseChannel">
    <int:gateway request-channel="nested-chain" error-channel="errChannel"/>
</int:chain>

<int:chain input-channel="errChannel" output-channel="nullChannel">
     .....
</int:chain>
1个回答

2
这里的问题涉及到单向nullChannel<int:gateway>请求/响应机制。即使您将异常发送到error-channel,也应该重新抛出它以便于调用或从错误流返回一些compensation消息。否则,您最终会在等待回复的网关上挂起,而这可能是永久的。当然,默认情况下是这样的!您可以在此问题上调整reply-timeout,并在出现单向错误处理时在一段时间内释放线程到池中。

换句话说,如果您的错误流程没有向网关返回任何回复,线程将永远等待永远不会到来的回复;设置 reply-timeout="0" 将释放该线程。只要您仅使用网关下游的直接通道,超时为 0 不会有任何影响 - 计时器直到线程返回网关才会启动。 - Gary Russell
谢谢Artem。我已经从错误流返回了一个补偿消息,并在此之后合并了成功和错误流。这个解决方案非常好,它实际上释放了线程。 - ruchi ahuja
Gary,我也尝试过设置回复超时时间,但并没有释放线程。由于流程未从错误通道返回,因此流程在nullChannel处结束,回复超时不会产生任何影响。我有什么遗漏吗? - ruchi ahuja

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接