Search code examples
spring-integrationspring-integration-sftp

Spring Integration - Not all files are getting uploaded to destination - clearThreadKey issue


setThreadKey is getting invoked for every file, but clearThreadKey is getting invoked for alternative files. When ever a flow invokes clearThreadKey the file is not getting uploaded to SFTP destination path. Out of 10 files, only 5 files are getting uploaded. I have customized the DelegatingSessionFactory class to decide the threadKey. I guess i am doing something wrong. I think, clearThreadKey is getting invoked even before uploading the file to destination, due to which the flow is not able to upload the file to destination. But it is supposed to invoke only after uploading the file. At the same time, strangely clearThreadKey is not invoked for all the files.

public Message<?> setThreadKey(Message<?> message, Object key) {
    String keyStr = String.valueOf(key).split("-")[0];
    this.threadKey.set(keyStr);
    return message;
}

public Message<?> clearThreadKey(Message<?> message, Object key) {
    this.threadKey.remove();
    return message;
}

integration.xml

<int-file:inbound-channel-adapter directory="myowndirectorypath" id="fileInbound" channel="sftpChannel">
    <int:poller fixed-rate="1000" max-messages-per-poll="100"/>
</int-file:inbound-channel-adapter>

<int:channel id="sftpChannel"/>

<int:service-activator input-channel="sftpChannel" output-channel="outsftpChannel"
                       expression="@dsf.setThreadKey(#root, headers['file_name'])"/>

<int:channel id="outsftpChannel"/>

<int-sftp:outbound-channel-adapter id="sftpOutboundAdapter" session-factory="dsf"
                                   channel="outsftpChannel" charset="UTF-8" 
                                   remote-directory-expression="@sftpConfig.determineRemoteDirectory(headers['file_name'])"/>

<int:service-activator input-channel="outsftpChannel" output-channel="nullChannel"
                       expression="@dsf.clearThreadKey(#root, headers['file_name'])" requires-reply="true"/>

Solution

  • You have two subscribers on outsftpChannel; they will receive alternate messages in round-robin fashion because it is a DirectChannel.

    You need a publish-subscribe-channel so that both recipients will receive the message.