【问题标题】:Spring integration - stop polling if no file returnedSpring集成-如果没有返回文件,则停止轮询
【发布时间】:2017-07-13 13:03:45
【问题描述】:

我有一个轮询器,它正在轮询远程目录以 sftp 跨过文件,但如果在 x 次尝试后找不到文件,我想停止它。有没有一个简单的配置?

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. ")
            }
        }

当我查找上述文件(不存在)时,我似乎无限期地轮询,而如果文件不存在,我想抛出异常。

【问题讨论】:

  • 我会在 Executor 中运行它,但会超时。或者,更好的是,停止轮询并考虑响应式 Spring 和事件。

标签: java spring spring-integration spring-integration-sftp


【解决方案1】:

参见Smart Polling - 您可以检测到缺少消息并停止轮询器。

4.2 版引入了 AbstractMessageSourceAdvice。建议链中任何作为此类子类的建议对象都仅应用于接收操作。此类类实现以下方法:

beforeReceive(MessageSource&lt;?&gt; source)

此方法在 MessageSource.receive() 方法之前调用。它使您可以在此时检查和/或重新配置源。返回 false 会取消此轮询(类似于上面提到的 PollSkipAdvice)。

Message&lt;?&gt; afterReceive(Message&lt;?&gt; result, MessageSource&lt;?&gt; source)

该方法在receive()方法之后调用;同样,您可以重新配置源,或者根据结果采取任何行动(如果源没有创建消息,则可以为 null)。您甚至可以返回不同的消息!

【讨论】:

  • 不幸的是,我使用的是 4.1,我无法升级 :(
  • 因此,您只能选择将解决方案复制/粘贴到您自己的Advice 实现中:github.com/spring-projects/spring-integration/blob/master/…
  • 那不行;他还需要对SourcePollingChannelAdapter 进行相应的更改,以仅建议receive() 方法并在轮询之前和之后调用这些方法。您可以在频道上添加频道拦截器并启动计时器;如果一段时间后没有消息发送到通道,则停止适配器。
  • 我如何真正停止拦截器中的适配器?我在 postReceive 中有计时逻辑,但是我将如何停止适配器?即我重写 ChannelInterceptor 并将逻辑放在重写的 postReceive 方法中。
  • @Autowire SourcePollingChannelAdapter 字段为适配器 bean 名称(如果使用 XML,则为 id);然后adapter.stop().
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-11
  • 2020-10-28
  • 1970-01-01
  • 1970-01-01
  • 2014-12-06
  • 2015-04-03
相关资源
最近更新 更多