无法在Spring Integration Flow中的建议中删除文件资源

时间:2019-02-14 21:19:49

标签: spring-integration spring-integration-dsl spring-integration-sftp

我在文件中定义了3个流,以轮询tif文件并将其发送到通道。该通道链接到另一个流,该流将转换并复制到同一位置的pdf文件。然后,第三个流ftp是pdf文件。链接到ftp流的建议,成功表达后将同时删除tif和pdf文件:

@Bean
public IntegrationFlow rtwInflow() {
    return IntegrationFlows
            .from(rtwTifFileSharePoller()
                    , e -> e.poller(Pollers.fixedDelay(15000)))
            .channel(tifToPdfConverterChannel())
            .get();
}

@Bean
public IntegrationFlow rtwTransformFlow() {
    return IntegrationFlows
            .from(tifToPdfConverterChannel())
            .transform(pdfTransfomer)
            .log()
            .get();
}

@Bean
public IntegrationFlow rtwFtpFlow() {
    return IntegrationFlows
            .from(rtwPdfFileSharePoller()
                    , e -> e.poller(Pollers.fixedDelay(15000)))
            .handle(ftpOutboundHandler(), out -> out.advice(after()))
            .get();
}

建议看起来像:

@Bean
public ExpressionEvaluatingRequestHandlerAdvice after() {
    logger.debug("Evaluating expression advice. ");
    ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
    advice.setOnFailureExpressionString("#root");
    advice.setOnSuccessExpressionString("#root");
    advice.setSuccessChannel(rtwSourceDeletionChannel());
    advice.setFailureChannel(rtwFtpFailureHandleChannel());
    advice.setPropagateEvaluationFailures(true);
    return advice;
}

成功ftp传输pdf文件时,流转移到rtwSourceDeletionChannel(),后者执行以下操作:

@Bean
@SuppressWarnings("unchecked")
public IntegrationFlow rtwSourceDeleteAfterFtpFlow() {
    return IntegrationFlows
            .from(this.rtwSourceDeletionChannel())
            .handle(msg -> {
                    logger.info("Deleting files at source and transformed objects. ");

                    Message<File> requestedMsg = (Message<File>) msg.getPayload();
                    String fileName = (String) requestedMsg.getHeaders().get(FileHeaders.FILENAME); 
                    String fileNameWithoutExtn = fileName.substring(0, fileName.lastIndexOf(".")); 

                    logger.info("payload: " + msg.getPayload());
                    logger.info("fileNameWithoutExtn: " + fileNameWithoutExtn);

                    // delete both pdf and tif files. 
                    File tifFile = new File(rtwSharedPath + File.separator + fileNameWithoutExtn + ".tif");
                    File pdfFile = new File(rtwSharedPath + File.separator + fileNameWithoutExtn + ".pdf");

                    while (!tifFile.isDirectory() && tifFile.exists()) {
                        logger.info("Tif Delete status: " + tifFile.delete());
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException e) {
                        } 
                    }
                    while (!pdfFile.isDirectory() && pdfFile.exists()) {
                        logger.info("PDF Delete status: " + pdfFile.delete());
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException e) {
                        } 
                    }
            })
            .get();
}

我将得到以下输出,其中tif文件已锁定。使用Files.delete()给我一个例外,该文件正在被另一个进程使用。

2019-02-14 21:06:48.882  INFO 972 --- [ask-scheduler-1] nsfomer$$EnhancerBySpringCGLIB$$c667e8e1 : transformed path: \\localhost\atala-capture-upload\45937.pdf
2019-02-14 21:06:48.898  INFO 972 --- [ask-scheduler-1] o.s.integration.handler.LoggingHandler   : GenericMessage [payload=145937.pdf, headers={file_originalFile=\\localhost\atala-capture-upload\145937.tif, id=077ad304-efe5-7af5-ed07-17f909f9b0e1, file_name=145937.tif, file_relativePath=145937.tif, timestamp=1550178408898}]
2019-02-14 21:06:53.765  INFO 972 --- [ask-scheduler-2] TWFlows$$EnhancerBySpringCGLIB$$4905989f : Tif Delete status: false
2019-02-14 21:06:58.774  INFO 972 --- [ask-scheduler-2] TWFlows$$EnhancerBySpringCGLIB$$4905989f : Tif Delete status: false
2019-02-14 21:07:03.782  INFO 972 --- [ask-scheduler-2] TWFlows$$EnhancerBySpringCGLIB$$4905989f : Tif Delete status: false
2019-02-14 21:07:08.791  INFO 972 --- [ask-scheduler-2] TWFlows$$EnhancerBySpringCGLIB$$4905989f : Tif Delete status: false
2019-02-14 21:07:13.800  INFO 972 --- [ask-scheduler-2] TWFlows$$EnhancerBySpringCGLIB$$4905989f : Tif Delete status: false

请帮助我理解为什么我会遇到此问题。另外,在pdfTransformer中没有泄漏,因为我已经测试了代码,并且能够在tif和pdf文件上获取并关闭FileInputStream。

此外,如果需要通过设计改进解决方案,请指导我。

提前谢谢。...

1 个答案:

答案 0 :(得分:0)

好吧,似乎很奇怪。根据{{​​3}}

在删除前添加System.gc()可以解决此问题!