简体   繁体   中英

Spring integration - stop polling if no file returned

I have a poller that is polling a remote dir in order to sftp the file across but i want to stop it if it doesn't find the file after x amount of attempts. Is there a simple config for this?

ApplicationContext.xml

        <int-sftp:inbound-channel-adapter id="sftpInboundAdaptor"
                                          session-factory="sftpSessionFactory"
                                          local-directory="${local.dir}"
                                          auto-create-local-directory="true"
                                          auto-startup="false"
                                          channel="SftpChannel"
                                          remote-directory="${remote.dir}"
                                          filename-pattern="XXXX"
                                          delete-remote-files="false"
                                          charset="UTF-8"
                                          remote-file-separator="/"
                                          local-filename-generator-expression="#this">
            <int:poller max-messages-per-poll="1" fixed-rate="30000" >
            </int:poller>
        </int-sftp:inbound-channel-adapter>



Main.class

     private static void sftpFile(String party) throws Exception {
            SourcePollingChannelAdapter adapter = (SourcePollingChannelAdapter) applicationContext.getBean("sftpInboundAdaptor");
            adapter.start();
            SftpDownloader sftpProcessor = (SftpDownloader) applicationContext.getBean("sftpDownloader");
            LOGGER.info((fileDownloaded ? "Successful" : "Failed") + " downloading file"");
        }




SftpDownloader.class

    public boolean receiveFile(String party, String fileType) throws SftpJobException {
            if (Constants.1.equals(fileType)) {
                return isFile1SftpSuccessful();
            } else if (Constants.2.equals(fileType)) {
                return isFile2SftpSuccessful(party);
            }
            return false;
        }

        private boolean isFile1SftpSuccessful() throws SftpJobException {
            return isValidFile((File) SftpChannel.receive().getPayload());
        }
            private boolean isValidFile(File received) throws SftpJobException{
            if (received.length() != 0) {
                LOGGER.info("File is: {}", received.toString());
                return true;
            } else {
                throw new SftpJobException("File size is 0, either no file exists an empty file was created. ")
            }
        }

I seems like it polls indefinitely when i look for the above file (doesn't exist) whereas i'd like to throw an exception if the file wasn't there.

See Smart Polling - you can detect the lack of a message and stop the poller.

Version 4.2 introduced the AbstractMessageSourceAdvice. Any Advice objects in the advice-chain that subclass this class, are applied to just the receive operation. Such classes implement the following methods:

beforeReceive(MessageSource<?> source)

This method is called before the MessageSource.receive() method. It enables you to examine and or reconfigure the source at this time. Returning false cancels this poll (similar to the PollSkipAdvice mentioned above).

Message<?> afterReceive(Message<?> result, MessageSource<?> source)

This method is called after the receive() method; again, you can reconfigure the source, or take any action perhaps depending on the result (which can be null if there was no message created by the source). You can even return a different message!

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