Spring Integration Java DSL SFTP Обработка исключений - PullRequest
0 голосов
/ 01 октября 2018

Я использую Spring Integration для передачи файлов

следующий код работает нормально, но если я удаляю папку из исходного каталога, он начинает выдавать исключения.

IntegrationFlow flow = IntegrationFlows.from(Sftp.inboundAdapter(inboundSftp)
            .localDirectory(new File(classLoader.getResource(".").getFile() + "/files/" + String.valueOf(config.getId())))
            .deleteRemoteFiles(true)
            .autoCreateLocalDirectory(true)
            .remoteDirectory(config.getInboundDirectory()), e -> e.poller(Pollers.fixedDelay(4_000)))
            .transform((File f) -> pgpEncryption.encrypt(f))
            .handle(Sftp.outboundAdapter(outboundSftp)
                    .useTemporaryFileName(false)
                    .autoCreateDirectory(true)
                    .remoteDirectory(config.getOutboundDirectory()), c -> c.advice(sftpConfig.deleteFileAdvice())
            )
            .get();

исключение является исключениемесли исходная папка не существует.

2018-10-01 17:13:02.362 ERROR 12320 --- [ask-scheduler-8] o.s.integration.handler.LoggingHandler   : org.springframework.messaging.MessagingException: Problem occurred while synchronizing remote to local directory; nested exception is org.springframework.messaging.MessagingException: Failed to execute on session; nested exception is org.springframework.core.NestedIOException: Failed to list files; nested exception is 2: No such file
    at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer.synchronizeToLocalDirectory(AbstractInboundFileSynchronizer.java:331)
    at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizingMessageSource.doReceive(AbstractInboundFileSynchronizingMessageSource.java:260)
    at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizingMessageSource.doReceive(AbstractInboundFileSynchronizingMessageSource.java:65)
    at org.springframework.integration.endpoint.AbstractFetchLimitingMessageSource.doReceive(AbstractFetchLimitingMessageSource.java:43)
    at org.springframework.integration.endpoint.AbstractMessageSource.receive(AbstractMessageSource.java:154)
    at org.springframework.integration.endpoint.SourcePollingChannelAdapter.receiveMessage(SourcePollingChannelAdapter.java:243)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint.doPoll(AbstractPollingEndpoint.java:262)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller.lambda$run$0(AbstractPollingEndpoint.java:391)
    at org.springframework.integration.util.ErrorHandlingTaskExecutor.lambda$execute$0(ErrorHandlingTaskExecutor.java:57)
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
    at org.springframework.integration.util.ErrorHandlingTaskExecutor.execute(ErrorHandlingTaskExecutor.java:55)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller.run(AbstractPollingEndpoint.java:385)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.messaging.MessagingException: Failed to execute on session; nested exception is org.springframework.core.NestedIOException: Failed to list files; nested exception is 2: No such file
    at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:445)
    at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer.synchronizeToLocalDirectory(AbstractInboundFileSynchronizer.java:286)
    ... 20 more
Caused by: org.springframework.core.NestedIOException: Failed to list files; nested exception is 2: No such file
    at org.springframework.integration.sftp.session.SftpSession.list(SftpSession.java:103)
    at org.springframework.integration.sftp.session.SftpSession.list(SftpSession.java:50)
    at org.springframework.integration.file.remote.session.CachingSessionFactory$CachedSession.list(CachingSessionFactory.java:218)
    at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer.lambda$synchronizeToLocalDirectory$1(AbstractInboundFileSynchronizer.java:287)
    at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:436)
    ... 21 more
Caused by: 2: No such file
    at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2873)
    at com.jcraft.jsch.ChannelSftp._stat(ChannelSftp.java:2225)
    at com.jcraft.jsch.ChannelSftp._stat(ChannelSftp.java:2242)
    at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1592)
    at com.jcraft.jsch.ChannelSftp.ls(ChannelSftp.java:1553)
    at org.springframework.integration.sftp.session.SftpSession.list(SftpSession.java:91)
    ... 25 more

Итак, как можно перехватить это исключение и записать соответствующее сообщение.У меня будут и другие проблемы, такие как неверный IP-адрес сервера, неверный закрытый ключ.

Ответы [ 2 ]

0 голосов
/ 01 октября 2018

То, что o.s.integration.handler.LoggingHandler : org.springframework.messaging.MessagingException: действительно происходит от глобального errorChannel, настроенного на SourcePollingChannelAdapter по умолчанию.Вы можете предоставить свой собственный вариант через SourcePollingChannelAdapterSpec и его poller() конфигурацию:

e -> e.poller(Pollers.fixedDelay(4_000).errorChannel(myErrorChannel))

Этот пользовательский канал может быть подписан как любой другой, и там вы можете обрабатывать ErrorChannel любым удобным для вас способом.@ServiceActivator или другие IntegrationFlow могут быть использованы по данному вопросу.

0 голосов
/ 01 октября 2018

Все исключения во время выполнения сообщения отправляются в errorChannel по умолчанию.
Вы можете определить активатор службы на этом канале и предоставить свою логику обработки исключений.

</p> <pre><code>@Bean @ServiceActivator(inputChannel = "errorChannel") public MyExceptionHandler exceptionHandler() { return new MyExceptionHandler (); }


См. - Spring Integration DSL ErrorHandling
https://docs.spring.io/spring-integration/docs/latest-ga/reference/html/configuration.html#namespace-errorhandler

...