繁体   English   中英

Spring Integration轮询目录

[英]Spring Integration polling directories

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

在此处输入图片说明

现在,其他一些服务器/应用程序将处理这些.fif文件,并将生成.npz文件,并将其放置在相应visit / 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);

}

请给java配置如何我可以轮询父目录(即提供者)并知道路径,直到创建.npz文件的相应visits / id的输出文件夹为止。 然后获取创建的文件数或文件列表。

出站网关与LS命令一起使用(如果需要,它具有递归选项)。

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

编辑

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

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

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

暂无
暂无

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

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