连接重置FTP后恢复文件传输

时间:2017-10-19 11:59:14

标签: java spring spring-boot spring-integration

我正在使用Spring Integration构建一个应用程序,用于将文件从一个FTP服务器(源)发送到另一个FTP服务器(目标)。我首先使用入站适配器将文件从源发送到本地目录,然后使用出站适配器将文件从本地目录发送到目标。

我的代码似乎工作正常,我能够实现我的目标,但我的问题是在文件传输过程中将连接重置为目标FTP服务器,然后文件传输不再继续连接开始工作。

我使用inboundoutbound适配器使用Java配置。任何人都可以告诉我是否可以在重置连接后以某种方式恢复文件传输?

P.S:我是春天的初学者,所以如果我在这里做错了,请纠正我。感谢

AppConfig.java:

@Configuration
@Component
public class FileTransferServiceConfig {

    @Autowired
    private ConfigurationService configurationService;

    public static final String FILE_POLLING_DURATION = "5000";

    @Bean
    public SessionFactory<FTPFile> sourceFtpSessionFactory() {
        DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
        sf.setHost(configurationService.getSourceHostName());
        sf.setPort(Integer.parseInt(configurationService.getSourcePort()));
        sf.setUsername(configurationService.getSourceUsername());
        sf.setPassword(configurationService.getSourcePassword());
        return new CachingSessionFactory<FTPFile>(sf);
    }

    @Bean
    public SessionFactory<FTPFile> targetFtpSessionFactory() {
        DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
        sf.setHost(configurationService.getTargetHostName());
        sf.setPort(Integer.parseInt(configurationService.getTargetPort()));
        sf.setUsername(configurationService.getTargetUsername());
        sf.setPassword(configurationService.getTargetPassword());
        return new CachingSessionFactory<FTPFile>(sf);
    }

    @MessagingGateway
    public interface MyGateway {

         @Gateway(requestChannel = "toFtpChannel")
         void sendToFtp(Message message);

    }

    @Bean
    public FtpInboundFileSynchronizer ftpInboundFileSynchronizer() {
        FtpInboundFileSynchronizer fileSynchronizer = new FtpInboundFileSynchronizer(sourceFtpSessionFactory());
        fileSynchronizer.setDeleteRemoteFiles(false);
        fileSynchronizer.setRemoteDirectory(configurationService.getSourceDirectory());
        fileSynchronizer.setFilter(new FtpSimplePatternFileListFilter(
                configurationService.getFileMask()));
        return fileSynchronizer;
    }

    @Bean
    @InboundChannelAdapter(channel = "ftpChannel",
            poller = @Poller(fixedDelay = FILE_POLLING_DURATION ))
    public MessageSource<File> ftpMessageSource() {
        FtpInboundFileSynchronizingMessageSource source =
                new FtpInboundFileSynchronizingMessageSource(ftpInboundFileSynchronizer());
        source.setLocalDirectory(new File(configurationService.getLocalDirectory()));
        source.setAutoCreateLocalDirectory(true);
        source.setLocalFilter(new AcceptOnceFileListFilter<File>());
        return source;
    }



    @Bean
    @ServiceActivator(inputChannel = "ftpChannel")
    public MessageHandler targetHandler() {
        FtpMessageHandler handler = new FtpMessageHandler(targetFtpSessionFactory());
        handler.setRemoteDirectoryExpression(new LiteralExpression(
                configurationService.getTargetDirectory()));
        return handler;
    }    
}

Application.java:

@SpringBootApplication
public class Application {

    public static ConfigurableApplicationContext context;

    public static void main(String[] args) {
        context = new SpringApplicationBuilder(Application.class)
                .web(false)
                .run(args);
    }

    @Bean
    @ServiceActivator(inputChannel = "ftpChannel")
    public MessageHandler sourceHandler() {
        return new MessageHandler() {

            @Override
            public void handleMessage(Message<?> message) throws MessagingException {
                Object payload = message.getPayload();
                System.out.println("Payload: " + payload);
                if (payload instanceof File) {
                    File file = (File) payload;
                    System.out.println("Trying to send " + file.getName() + " to target");
                }
                MyGateway gateway = context.getBean(MyGateway.class);
                gateway.sendToFtp(message);
            }

        };
    }
}

1 个答案:

答案 0 :(得分:1)

首先,不清楚sourceHandler是什么,但你确实应该确定它已订阅(或targetHandler)到适当的频道。

我不知何故相信,在您的目标代码中,targetHandler确实订阅了toFtpChannel

无论如何都没有关系。

我认为这里的问题恰恰与AcceptOnceFileListFilter和错误有关。因此,在目录扫描期间首先过滤工作,并出于性能原因将所有本地文件加载到内存中队列。然后将它们全部发送到通道进行处理。当我们到达targetHandler并获得异常时,我们只是默默地走向全局errorChannel,而忽略了文件尚未转移的事实。这发生在内存中的所有剩余文件中。我认为无论如何都要恢复传输,但它只能用于远程目录中的新文件。

我建议您将ExpressionEvaluatingRequestHandlerAdvice添加到targetHandler定义(@ServiceActivator(adviceChain)),如果出现错误,请致电AcceptOnceFileListFilter.remove(File)

/**
 * Remove the specified file from the filter so it will pass on the next attempt.
 * @param f the element to remove.
 * @return true if the file was removed as a result of this call.
 */
boolean remove(F f);

这样您就可以从过滤器中删除失败的文件,它将在下一个轮询任务中被选中。您必须AcceptOnceFileListFilter才能从onFailureExpression获取对它的访问权限。该文件是请求消息的payload

修改

ExpressionEvaluatingRequestHandlerAdvice的示例:

@Bean
public Advice expressionAdvice() {
    ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
    advice.setOnFailureExpressionString("@acceptOnceFileListFilter.remove(payload)");
    advice.setTrapException(true);
    return advice;
}

...

@ServiceActivator(inputChannel = "ftpChannel", adviceChain = "expressionAdvice")

你可以从他们的JavaDocs中获得所有的休息。