简体   繁体   中英

How to stop spring sftp inbound channel adapter polling when files are downloaded

I am trying to download a couple of text files from a remote server via SFTP using a java program. For this I am using Spring Integration SFTP module and I have configured an Inbound Channel Adapter as below.

 <int:channel id="recieveChannel">
        <int:queue />
    </int:channel>

    <int-sftp:inbound-channel-adapter 
        session-factory="sftpSessionFactory"
        local-directory="C:\test-outbound\"
        channel="recieveChannel"
        filename-regex=".*\.txt$"            
        remote-directory="/opt/IInsurer/messages/" 
        >
        <int:poller fixed-rate="10000" receive-timeout="10000"></int:poller>
    </int-sftp:inbound-channel-adapter>

The problem is that every thing is working fine ie the files are downloaded correctly to the local directory but the pooling keeps on going.I just want this to be a one time thing.I don't want to continuously poll the remote directory.How can I make the polling stop once the files are download?.Basically what I need is some sort of an event driven download mechanism which I manually trigger and once it downloads the files it shuts down.

I have also spring out bound gateway adapter but it does the same.I will really appreciate your help.Many Thanks

I started the inbound adapter using start() method. Further used a separate channel to read receive a message with a relevant timeout. If no message is received within the timeout period, I stop the adapter using stop().

Adapter configuration

<int-sftp:inbound-channel-adapter   id="RemoteTestingInboundChannelAdapter"  
        channel="requestChannel"
        session-factory="sftpSessionFactoryRemoteTesting" 
        local-directory="${sync.dir}"
        remote-directory="${RemoteTesting.local.dir}"
        auto-startup="false" 
        temporary-file-suffix=".writing"
        filter = "compositeFilterRemoteTesting" 
        delete-remote-files="false"> 
        <int:poller fixed-rate="${RemoteTesting.poller}" max-messages-per-poll="${RemoteTesting.maxMessagesPerPoll}"/>
     </int-sftp:inbound-channel-adapter>

Main class

adapter.start();

QueueChannel localFileChannel = context.getBean("requestChannel", QueueChannel.class);

Message<?> received = localFileChannel.receive();

while (received!=null)
                received = localFileChannel.receive(timeOutPeriod);
adapter.stop();

The adapter starts when main class fires it. It downloads all files, waits for new file till the timeout then stops.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM