【问题标题】:Spring Integration polling directoriesSpring Integration 轮询目录
【发布时间】:2019-01-22 11:39:54
【问题描述】:

我有一个用例,我需要将 .fif 文件从本地上传到指定目录结构中的远程 sftp 服务器。如下:

现在其他一些服务器/应用程序将处理这些 .fif 文件并生成 .npz 文件并将其放置在相应访问/id 的输出文件夹中。

我可以使用 sftpOutboundAdapter 上传文件,它工作正常。

现在,我不知道如何添加轮询以知道在此访问的此输出目录中创建了输出文件。我不想下载输出文件夹中的文件。我只想知道创建了 4 个文件,以便我可以更新访问状态。

我目前的代码是

@Value("${sftp.host}")
private String host;

@Value("${sftp.port:22}")
private int port;

@Value("${sftp.user}")
private String user;

@Value("${sftp.privateKey:#{null}}")
private Resource privateKey;

@Value("${sftp.privateKeyPassphrase:}")
private String privateKeyPassphrase;

@Value("${sftp.password:#{null}}")
private String password;

private final FileService fileService;

@Autowired
public SftpConfig(FileService fileService) {
    this.fileService = fileService;
}

@Bean
public SessionFactory<LsEntry> sftpSessionFactory() {
    DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
    factory.setHost(host);
    factory.setPort(port);
    factory.setUser(user);
    if (privateKey != null) {
        factory.setPrivateKey(privateKey);
        factory.setPrivateKeyPassphrase(privateKeyPassphrase);
    } else {
        factory.setPassword(password);
    }
    factory.setAllowUnknownKeys(true);
    return new CachingSessionFactory<LsEntry>(factory);
}

@Bean
public IntegrationFlow sftpOutboundFlow() {
    return IntegrationFlows.from("toSftpChannel")
            .handle(Sftp.outboundAdapter(this.sftpSessionFactory(), FileExistsMode.FAIL)
                            .remoteDirectoryExpression("headers['path']")
                            .useTemporaryFileName(false)
                            .autoCreateDirectory(true)
                            .fileNameGenerator(message -> (String) message.getHeaders().get("fileName"))
                    , c -> c.advice(expressionAdvice())
            ).get();
}

@Bean
public Advice expressionAdvice() {
    ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
    advice.setSuccessChannelName("integrationFlow.input");
    advice.setOnSuccessExpressionString("payload");
    advice.setFailureChannelName("integrationFlow.input");
    advice.setOnFailureExpressionString("payload");
    advice.setTrapException(true);
    return advice;
}

@Bean
public IntegrationFlow integrationFlow() {
    return f -> f.handle((MessageHandler) fileService::OnFilesUpload);
}

@MessagingGateway
public interface UploadGateway {

    @Gateway(requestChannel = "toSftpChannel")
    void upload(@Payload File file, @Header("fileName") String fileName, @Header("path") String path,
                @Header("parentId") Long parentId);

}

请给出我如何轮询父目录(即 Providers)的 java 配置,并知道在其中创建 .npz 文件的各个访问/ID 的输出文件夹的路径。然后获取创建的文件数或文件列表。

【问题讨论】:

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


    【解决方案1】:

    在 LS 命令中使用outbound gateway(如果需要,它有一个递归选项)。

    只需将远程目录发送到网关,它就会返回列表。

    编辑

    在下面回答您的评论;你没有显示你的调用代码,但是为什么你不能做这样的事情......

    @MessagingGateway
    public interface UploadGateway {
    
        @Gateway(requestChannel = "toSftpChannel")
        void upload(@Payload File file, @Header("fileName") String fileName, @Header("path") String path,
                @Header("parentId") Long parentId);
    
        @Gateway(requestChannel = "toSftpLsGatewayChannel")
        List<LsEntry> getOutputFiles(String path);
    
    }
    

    this.gateway.upload(file, fileName, path, parentId);
    List<LsEntry> outputs;
    String outputPath = path.replace("/input", "/output");
    do {
        outputs = this.gateway.getOutputFiles(outputPath);
        Thread.sleep(5_000);
    } while (outputs == null || outputs.size() == 0);
    

    如果您希望它异步运行,只需在任务执行器上运行第二部分即可。

    【讨论】:

    • 如何获取创建文件的远程目录?
    • ?您已经知道 - 您在出站适配器中使用了 headers['path']。所以大概是相同的值,但使用output 而不是input
    • 我有多个提供者、患者和访问。在出站适配器中,我将.fif文件上传到每个患者各自访问的输入文件夹中,我不知道它的输出何时会在输出文件夹中准备好,所以我想知道哪个输出文件夹中的哪个访问哪个患者,.npz 文件被创建?所以,我可以将访问状态设置为“输出就绪”。请再次阅读问题..
    • 您没有显示您的呼叫代码;查看我的答案的编辑。
    • 感谢 gary,但是有没有办法可以轮询远程服务器上的父目录(例如,在我的情况下为提供程序),它会轮询其中的所有子目录并告诉我确切的路径在其中创建 .npz 文件的子目录(例如我的情况下是访问/id)?
    猜你喜欢
    • 2014-09-30
    • 2019-06-29
    • 2016-12-15
    • 2014-11-04
    • 1970-01-01
    • 2014-07-28
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    相关资源
    最近更新 更多