簡體   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