Spring integration - stop polling if no file returned

不羁的心 提交于 2020-04-19 08:51:57

问题


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.


回答1:


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!



来源:https://stackoverflow.com/questions/45081464/spring-integration-stop-polling-if-no-file-returned

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!