Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 连接重置FTP后恢复文件传输_Java_Spring_Spring Boot_Spring Integration - Fatal编程技术网

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

Java 连接重置FTP后恢复文件传输,java,spring,spring-boot,spring-integration,Java,Spring,Spring Boot,Spring Integration,我正在使用SpringIntegration构建一个应用程序,该应用程序用于将文件从一个FTP服务器(源)发送到另一个FTP服务器(目标)。我首先使用入站适配器将文件从源目录发送到本地目录,然后使用出站适配器将文件从本地目录发送到目标目录 我的代码似乎工作正常,我能够实现我的目标,但我的问题是,在文件传输过程中,当连接重置到目标FTP服务器时,在连接开始工作后,文件传输不会继续 我使用了Java配置和适配器。有谁能告诉我,在连接重置后,是否可以以某种方式恢复文件传输 附言:我是Spring的初学

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

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

我使用了Java配置和适配器。有谁能告诉我,在连接重置后,是否可以以某种方式恢复文件传输

附言:我是Spring的初学者,如果我在这里做错了什么,请纠正我。谢谢

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;
    }    
}
@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);
            }

        };
    }
}
@配置
@组成部分
公共类FileTransferServiceConfig{
@自动连线
专用配置服务配置服务;
公共静态最终字符串文件\u POLLING\u DURATION=“5000”;
@豆子
public SessionFactory sourceFtpSessionFactory(){
DefaultFtpSessionFactory sf=新的DefaultFtpSessionFactory();
setHost(configurationService.getSourceHostName());
setPort(Integer.parseInt(configurationService.getSourcePort());
setUsername(configurationService.getSourceUsername());
sf.setPassword(configurationService.getSourcePassword());
返回新的CachingSessionFactory(sf);
}
@豆子
public SessionFactory targetFTPSSessionFactory(){
DefaultFtpSessionFactory sf=新的DefaultFtpSessionFactory();
setHost(configurationService.getTargetHostName());
setPort(Integer.parseInt(configurationService.getTargetPort());
setUsername(configurationService.getTargetUsername());
sf.setPassword(configurationService.getTargetPassword());
返回新的CachingSessionFactory(sf);
}
@消息网关
公共接口MyGateway{
@网关(requestChannel=“toFtpChannel”)
void sendtoptp(消息消息);
}
@豆子
公共FtpInboundFileSynchronizer FtpInboundFileSynchronizer(){
FtpInboundFileSynchronizer fileSynchronizer=新的FtpInboundFileSynchronizer(sourceFtpSessionFactory());
fileSynchronizer.setDeleteRemoteFiles(false);
fileSynchronizer.setRemoteDirectory(configurationService.getSourceDirectory());
fileSynchronizer.setFilter(新的FtpSimplePatternFileListFilter(
configurationService.getFileMask());
返回文件同步器;
}
@豆子
@InboundChannelAdapter(channel=“ftpcchannel”,
轮询器=@poller(fixedDelay=文件\u轮询\u持续时间))
public MessageSource ftpMessageSource(){
FtpInboundFileSynchronizingMessageSource源=
新的FtpInboundFileSynchronizingMessageSource(ftpInboundFileSynchronizer());
setLocalDirectory(新文件(configurationService.getLocalDirectory());
source.setAutoCreateLocalDirectory(true);
setLocalFilter(新的AcceptOnceFileListFilter());
返回源;
}
@豆子
@ServiceActivator(inputChannel=“ftpChannel”)
public MessageHandler targetHandler(){
FtpMessageHandler=新的FtpMessageHandler(targetFtpSessionFactory());
handler.setRemoteDirectoryExpression(新的LiteralExpression(
configurationService.getTargetDirectory());
返回处理程序;
}    
}
Application.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;
    }    
}
@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);
            }

        };
    }
}
@springboot应用程序
公共类应用程序{
公共静态配置应用上下文上下文;
公共静态void main(字符串[]args){
上下文=新的SpringApplicationBuilder(Application.class)
.web(错误)
.run(args);
}
@豆子
@ServiceActivator(inputChannel=“ftpChannel”)
public MessageHandler sourceHandler(){
返回新的MessageHandler(){
@凌驾
public void handleMessage(消息消息消息)引发MessaginException{
对象负载=message.getPayload();
系统输出打印项次(“有效载荷:+有效载荷);
if(文件的有效负载实例){
文件=(文件)有效载荷;
System.out.println(“试图将”+file.getName()+“发送到目标”);
}
MyGateway=context.getBean(MyGateway.class);
gateway.sendtoptp(消息);
}
};
}
}

首先,不清楚
sourceHandler
用于什么,但您确实应该确保它已订阅(或
targetHandler
)到正确的频道

我相信在你的目标代码中,
targetHandler
实际上是订阅了
toFtpChannel

不管怎样,这没有关系

我认为这里的问题恰恰与
AcceptOnceFileListFilter
和错误有关。因此,在目录扫描期间,过滤器首先工作,出于性能原因,将所有本地文件加载到内存队列中。然后,所有这些数据都被发送到通道进行处理。当我们到达
targetHandler
并得到一个异常时,我们只是悄悄地转到了全局
errorChannel
,失去了文件尚未传输的事实。内存中的所有剩余文件都会发生这种情况。我认为传输已经恢复,但它已经开始工作,只适用于远程目录中的新文件

我建议您将
expressionevaluation请求Handleradvice
添加到
targetHandler
定义(
@ServiceActivator(adviceChain)
)中,如果出现错误,请调用
AcceptOnceFileListFilter.remove(File)

通过这种方式,您可以从筛选器中删除失败的文件,并在下一个筛选器中拾取这些文件