Spring Service Threads驻留在Service Activator上

时间:2014-12-03 15:29:12

标签: multithreading asynchronous spring-integration

我遇到线程停在我的服务激活器上的问题,当池耗尽时导致文件挂在SftpGatewayChannel中。我认为这与SA有无效回报有关,这是正确的,因为它们只是递增指标。

我能够通过向SftpGateway添加一个default-reply-timeout来解决这个问题,但这并不理想,因为有重试建议,如果有连接,我不希望线程超时问题。我想要一个解决方案,在成功上传后将线程返回到池中并调用"成功"服务激活器。

<task:executor id="Tasker" rejection-policy="CALLER_RUNS" pool-size="${MaxThreads}" />

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

<int:service-activator id="SegmentStart" input-channel="SftpGatewayChannel" ref="SftpGateway" />

<int:gateway id="SftpGateway" default-request-channel="SftpOutboundChannel" error-channel="ErrorChannel" />

<int:channel id="SftpOutboundChannel" datatype="java.lang.String,java.io.File,byte[]" />

<int-sftp:outbound-channel-adapter id="SftpOutboundAdapter"
    session-factory="SftpCachingSessionFactory" channel="SftpOutboundChannel" charset="UTF-8" >
    <int-sftp:request-handler-advice-chain>
        <ref bean="exponentialRetryAdvice" />
        <bean id="SuccessAdvice" class="org.springframework.integration.handler.advice.ExpressionEvaluatingRequestHandlerAdvice" >
            <property name="successChannel" ref="SuccessChannel"/>
            <property name="onSuccessExpression" value="true"/>
        </bean>
    </int-sftp:request-handler-advice-chain>
</int-sftp:outbound-channel-adapter>

<int:channel id="ErrorChannel">
    <int:interceptors>
        <int:wire-tap channel="FailureChannel" />
    </int:interceptors>
</int:channel>

<int:channel id="AttemptChannel" />
<int:channel id="SuccessChannel" />
<int:channel id="FailureChannel" />

<int:service-activator id="AttemptMetrics" input-channel="AttemptChannel"
    expression="T(MetricsCounter).addAttempt()"  />
<int:service-activator id="SuccessMetrics" input-channel="SuccessChannel"
    expression="T(MetricsCounter).addSuccesses(inputMessage.Headers.messages.size())" />
<int:service-activator id="FailureMetrics" input-channel="FailureChannel"
    expression="T(MetricsCounter).addFailures(payload.getFailedMessage().Headers.messages.size())" />

1 个答案:

答案 0 :(得分:0)

是的,网关希望默认回复。您可以使用RequestReplyExchanger方法并service-interface返回void,而不是使用默认void process(Message<?> m)

或者,正如您所做的那样,只需在您的网关上添加default-reply-timeout="0",线程就会立即返回而无需等待回复(永远不会回复)。

  

......但这并不理想......

回复超时时钟仅在线程返回网关时启动,因此对下游流量没有影响。