4
votes

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 i.e. 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

1
Thanks.I did stumble upon the link you provided but I didn't get it.I am not using any sort of batch service or anything.It is a simple java application.How can I apply it for thatCheeta
pls check if this will help.. stackoverflow.com/questions/23915524/…Jos
Thanks.Yes, I tried the control bus technique but I do not know when to send the "stop" command to stop the adapter.For me it should be the adapter has successfully downloaded all the files once.Also when I stop the adapter my java program does not exit.It is still running and I do not want to call system.exit on it.Cheeta

1 Answers

0
votes

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.