简体   繁体   English

如何设置 ExpressionEvaluatingRequestHandlerAdvice 以阻止任务被取消

[英]How to set up ExpressionEvaluatingRequestHandlerAdvice to stop task from being cancelled

I have a scenario which I'm not sure if I am setting up correctly in Integration Flow.我有一个场景,我不确定我是否在 Integration Flow 中设置正确。 The need is to:需要的是:

  • Poll SFTP location for files轮询文件的 SFTP 位置
  • Transfer all that are new / have been changed and store that information with SftpPersistentAcceptOnceFileListFilter传输所有新的/已更改的信息并使用SftpPersistentAcceptOnceFileListFilter存储该信息
  • On failure continue with the next available file失败时继续下一个可用文件

For the last point I have found in another answer that I could try an ExpressionEvaluatingRequestHandlerAdvice .对于最后一点,我在另一个答案中发现我可以尝试ExpressionEvaluatingRequestHandlerAdvice I have came up with the following configuration, but adding the Advice has broken the flow completely (no message is flowing through)我提出了以下配置,但是添加Advice完全破坏了流程(没有消息流过)

ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
advice.setSuccessChannel(out);
advice.setFailureChannel(err);
advice.setTrapException(true);

IntegrationFlow integrationFlow = IntegrationFlows
        .from(Sftp.inboundAdapter(cachingSessionFactory)
        .remoteDirectory("sftpSource")
        .deleteRemoteFiles(false)
        .preserveTimestamp(true)
        .localDirectory(getTargetLocalDirectory()), e -> e.id("sftpInboundAdapter")
                .poller(Pollers.fixedDelay(100)
                        .maxMessagesPerPoll(3)
                        .advice(advice)))
        .channel(out)
        .get();

The requirement for skipping a filed file transfer came out from a real world scenario, where our SFTP server refused to transfer an empty file.跳过已归档文件传输的要求来自现实世界的场景,我们的 SFTP 服务器拒绝传输空文件。 To simulate this I have added spies to the SessionFactory :为了模拟这一点,我在SessionFactory中添加了间谍:

CachingSessionFactory<ChannelSftp.LsEntry> cachingSessionFactory = Mockito.spy(sessionFactory());
CachingSessionFactory.CachedSession session = (CachingSessionFactory.CachedSession) Mockito.spy(cachingSessionFactory.getSession());

doReturn(session).when(cachingSessionFactory).getSession();
doThrow(new RuntimeException("test exception")).when(session).read(contains("sftpSource2.txt"), any(OutputStream.class));

and the test code:和测试代码:

Message<?> message = out.receive(1000);
assertThat(message).isNotNull();
Object payload = message.getPayload();
assertThat(payload).isInstanceOf(File.class);
File file = (File) payload;
assertThat(file.getName()).isEqualTo(" sftpSource1.txt");
assertThat(file.getAbsolutePath()).contains("localTarget");

message = out.receive(1000);
assertThat(message).isNull();

message = err.receive(1000);
System.out.println("error was:" + message.getPayload());

message = out.receive(1000);
assertThat(message).isNotNull();
file = (File) message.getPayload();
assertThat(file.getName()).isIn("sftpSource3.txt");
assertThat(file.getAbsolutePath()).contains("localTarget");

What I am confused about is - when I added the advice to the Poller should I remove .errorChannel(err) from the Poller ?我感到困惑的是 - 当我向Poller添加advice时,我应该从Poller中删除.errorChannel(err)吗? But if the advice is handling where the message ends up, shouldn't I also remove .channel(out) on the integrationFlow ?但是,如果建议是处理消息结束的地方,我不应该也删除integrationFlow上的.channel(out)吗? Without it the IntegrationFlow doesn't build, with error outputChannel is required .没有它,IntegrationFlow 不会构建,并出现错误outputChannel is required

My second worry is - if advice.setTrapException(true);我的第二个担心是——如果advice.setTrapException(true); does it mean SftpPersistentAcceptOnceFileListFilter will mark the file as successfully processed?这是否意味着SftpPersistentAcceptOnceFileListFilter会将文件标记为已成功处理? (the filter isn't present in the example code, but I will require it in real code). (过滤器不在示例代码中,但我会在实际代码中需要它)。

The ExpressionEvaluatingRequestHandlerAdvice is for consumer endpoints, not a source polling channel adapters. ExpressionEvaluatingRequestHandlerAdvice适用于消费者端点,而不是源轮询通道适配器。 So, you cannot handle errors from the poller using that advice.因此,您无法使用该建议处理来自轮询器的错误。

Indeed you cannot have an IntegrationFlow just with a SourcePollingChannelAdapter - you need to add that channel(out) either way.实际上,您不能仅使用SourcePollingChannelAdapter来拥有IntegrationFlow - 您需要以任何一种方式添加该channel(out)

Not clear by your current configuration where you transfer those files.您当前的配置不清楚您将这些文件传输到哪里。 So, probably you are talking about an error for handling those files, there that ExpressionEvaluatingRequestHandlerAdvice would make sense over there.因此,您可能正在谈论处理这些文件的错误,在那里ExpressionEvaluatingRequestHandlerAdvice在那里有意义。 This way indeed all your remote files would be transferred to the local dir and marked in the SftpPersistentAcceptOnceFileListFilter as watched.这样,您所有的远程文件实际上都会被传输到本地目录,并在SftpPersistentAcceptOnceFileListFilter中标记为已监视。 Just because you won't throw an exception from the handler back to the SFTP Inbound Channel Adapter.只是因为您不会将异常从处理程序抛回给 SFTP 入站通道适配器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM