无法在春季集成流中的建议中删除文件资源



我在文件中定义了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;
}

成功的pdf文件的FTP上的流程,转移到RTWSourcedeletChannel(),谁进行了以下操作:

@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: \localhostatala-capture-upload45937.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=\localhostatala-capture-upload145937.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。

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

预先感谢....

好,看起来很奇怪。根据堆栈溢出的其他问题

添加System.gc()之前删除解决问题!

相关内容

  • 没有找到相关文章

最新更新