0

I have 3 flows defined in a file, to poll tif files and send it to a channel. The channel is linked to another flow that converts and copies into a pdf file in the same location. Then the third flow ftp's the pdf file. An advice in linked to ftp flow, where both tif and pdf file are to be deleted after successexpression:

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

The advice looks something like:

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

The flow upon successful ftp of pdf file, diverts to rtwSourceDeletionChannel(), who does the following:

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

I am getting output as below, where tif file is locked. Using Files.delete() gave me Exception that file is in use by another process.

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

Please help me understand why I am facing this problem. Also, in the pdfTransformer there is no leak, as I have tested the code and I was able to acquire and close FileInputStream on both tif and pdf files.

Also, please guide me if the solution needs to be improved by design.

Thanks in advance....

Guru
  • 2,739
  • 1
  • 25
  • 27
  • The `RemoteFileTemplate` in the FTP outbound adapter reliably closes the input stream in a finally block; I don't see anywhere else where the framework opens your file, so I don't believe the framework has your file in use when it hits the advice. – Gary Russell Feb 14 '19 at 21:49

1 Answers1

0

Well, seems wierd. According to other question on stack overflow

adding System.gc() before delete fixed the problem!

Guru
  • 2,739
  • 1
  • 25
  • 27