gpt4 book ai didi

java - spring 集成中 tcp-inbound-gateway 中的回复超时含义

转载 作者:可可西里 更新时间:2023-11-01 02:34:27 26 4
gpt4 key购买 nike

Spring 集成 tcp 网关可以设置如下:

<!-- Server side -->

<int-ip:tcp-connection-factory id="crLfServer"
type="server"
port="${availableServerSocket}"/>

<int-ip:tcp-inbound-gateway id="gatewayCrLf"
connection-factory="crLfServer"
request-channel="serverBytes2StringChannel"
error-channel="errorChannel"
reply-timeout="10000" />

<int:channel id="toSA" />

<int:service-activator input-channel="toSA"
ref="echoService"
method="test"/>

<bean id="echoService"
class="org.springframework.integration.samples.tcpclientserver.EchoService" />
<int:object-to-string-transformer id="serverBytes2String"
input-channel="serverBytes2StringChannel"
output-channel="toSA"/>
<int:transformer id="errorHandler"
input-channel="errorChannel"
expression="Error processing payload"/>

注意设置为 10 秒的回复超时。

是不是意味着TCP服务器会调用服务,最多可以等待10秒?如果服务在 10 秒内没有回复,TCP 服务器是否会将消息发送到 errorChannel,后者又发送客户端错误消息“Error processing payload”?

当我使用需要 20 秒的服务测试 TCP 服务器时,客户端需要 20 秒才能获得响应。我没有看到错误消息。

能否请您帮助理解 TCP 入站网关中的回复超时?

谢谢

更新:感谢 Artem 帮助解决这个问题。解决此问题的最佳方法是使用以下配置:

<beans>
<int-ip:tcp-connection-factory id="crLfServer" type="server" port="${availableServerSocket}"/>
<int-ip:tcp-inbound-gateway id="gatewayCrLf" connection-factory="crLfServer" request-channel="requestChannel" error-channel="errorChannel" reply-timeout="5000" />
<int:service-activator input-channel="requestChannel" ref="gateway" requires-reply="true"/>
<int:gateway id="gateway" default-request-channel="timeoutChannel" default-reply-timeout="5000" />
<int:object-to-string-transformer id="serverBytes2String" input-channel="timeoutChannel" output-channel="serviceChannel"/>
<int:channel id="timeoutChannel">
<int:dispatcher task-executor="executor"/>
</int:channel>
<bean id="executor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="5" />
<property name="maxPoolSize" value="10" />
<property name="queueCapacity" value="25" />
</bean>
<int:service-activator input-channel="serviceChannel" ref="echoService" method="test"/>
<bean id="echoService" class="org.springframework.integration.samples.tcpclientserver.EchoService" />
<int:transformer id="errorHandler" input-channel="errorChannel" expression="payload.failedMessage.payload + ' errorHandleMsg: may be timeout error'"/>
</beans>

谢谢

最佳答案

好吧,实际上我们应该像在其他类似地方那样对该属性进行描述,例如HTTP 入站网关:

        <xsd:attribute name="reply-timeout" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[
Used to set the receiveTimeout on the underlying MessagingTemplate instance
(org.springframework.integration.core.MessagingTemplate) for receiving messages
from the reply channel. If not specified this property will default to "1000"
(1 second).
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>

该超时表示等待下游流回复的时间。但!如果您将流转移到某个地方的另一个线程,那是可能的。否则一切都在调用者的线程中执行,因此等待时间是不确定的。

无论如何我们返回null超时后没有回复。它反射(reflect)在 TcpInboundGateway 中。 :

Message<?> reply = this.sendAndReceiveMessage(message);
if (reply == null) {
if (logger.isDebugEnabled()) {
logger.debug("null reply received for " + message + " nothing to send");
}
return false;
}

我们可以重新考虑 TcpInboundGateway 中的逻辑对于:

if (reply == null && this.errorOnTimeout) {
if (object instanceof Message) {
error = new MessageTimeoutException((Message<?>) object, "No reply received within timeout");
}
else {
error = new MessageTimeoutException("No reply received within timeout");
}
}

但在我看来,依赖客户端的超时确实会更好。

更新

我认为我们可以通过中流克服限制并满足您的要求 <gateway> :

<gateway id="gateway" default-request-channel="timeoutChannel" default-reply-timeout="10000"/>

<channel id="timeoutChannel">
<dispatcher task-executor="executor"/>
</channel>

<service-activator input-channel="requestChannel"
ref="gateway"
requires-reply="true"/>

因此,<service-activator>电话 <gateway>并等待那里的回复。当然,要求最后一个以 ReplyRequiredException 结束。 ,您可以将其转换为所需的 MessageTimeoutException在你的错误流程中 error-channel="errorChannel" .

timeoutChannel是一个执行者,使我们的default-reply-timeout="10000"非常有用,因为我们立即将网关上的一条消息转移到单独的线程中,然后从那里向右移动到用 CountDonwLatch 上的超时包裹的回复等待进程。 .

希望这是清楚的。

关于java - spring 集成中 tcp-inbound-gateway 中的回复超时含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37978738/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com