Elias Khattar
Elias Khattar

Reputation: 173

Polling a local folder and sending files to FTP using Spring Integration

I'm using spring integration output channel to send files to FTP server(s), from the researches I did, I was able to send one file from a folder to the ftp, my concern is that I want to keep on polling a local folder ex: BEY/finalBEY.csv and once there is a file with this name "finalBEY.csv" I want to send it to a certain FTP server and if there is no file in the folder I want to keep on watching or polling the folder until there is a one, I'm sure this is possible but not able to find the solution or the proper coding, thanks for helping

Code I used:

    public IntegrationFlow ftpOutboundFlow(Branch myBranch){
    return IntegrationFlows.from(OUTBOUND_CHANNEL)
            .handle(Ftp.outboundAdapter(createNewFtpSessionFactory(myBranch), FileExistsMode.REPLACE)
                    .useTemporaryFileName(true)
                    .autoCreateDirectory(true)

                    //.remoteFileSeparator("/")
                    .fileNameExpression("headers['" + FileHeaders.FILENAME + "']")

                    //.fileNameExpression("BEY/FEFOexportBEY.csv")
                    .remoteDirectory(myBranch.getFolderPath()))

            .get();
}

@Bean
public MessageChannel OUTBOUND_CHANNEL(){
    return new PublishSubscribeChannel();
}

@MessagingGateway
public interface MyGateway {

    @Gateway(requestChannel = OUTBOUND_CHANNEL)
    void sendToFtp(File file);

}

In the controller where I'm registering the flow and sending a file.

private void addFlowftpOutbound(String name) {
        branch = branchService.getById(Long.valueOf(name));
        System.out.println(branch.getBranchCode());
        IntegrationFlow flow = ftIntegration.ftpOutboundFlow(branch);
        this.flowContext.registration(flow).id(name +"o").register();
        myGateway.sendToFtp(new File("BEY/finalBEY.csv"));
    }

Consol trial output:

When no file in BEY folder, which is logical.

2018-11-27 08:32:47.793  WARN 6420 --- [nio-8081-exec-9] o.s.i.ftp.session.FtpRemoteFileTemplate  : File BEY\finalBEY.csv does not exist

After dropping finalBEY.csv in the folder, the inbound flow just reading what is new there in the folder and as you can see nothing is sent, as well I checked the ftp server and nothing is there, note that if the file is already there when I run the app, definitely it will send it, this is already tested.

2018-11-27 08:33:17.985  INFO 6420 --- [ask-scheduler-2] f.s.s.configuration.FTIntegration        : flow=stockInboundFlowFromAFT, message=incoming file: BEY\finalBEY.csv
2018-11-27 08:33:17.985  INFO 6420 --- [ask-scheduler-2] f.s.s.configuration.FTIntegration        : flow=stockIntermediateStageChannel, message=rename file: BEY\finalBEY.csv

Upvotes: 1

Views: 376

Answers (1)

Gary Russell
Gary Russell

Reputation: 174729

It looks like you are polling the directory yourself (and using a gateway). Simply ignore files where the final file does not exist.

You can use a polled FileReadingMessageSource to poll the directory and implement a custom FileListFilter.

The custom filter should filter out files where the final file does not yet exist.

Upvotes: 0

Related Questions