在使用NioLocker将其锁定在FileReadingMessageSource轮询器中之后,何时应该解锁文件?

时间:2016-10-05 19:13:35

标签: java spring spring-integration spring-batch

我正在使用通用源代码在Spring Integration中设置目录轮询器。 Poller使用NioLocker来锁定和文件,根据JavaDocs,我负责使用NioFileLocker.unlock(文件)解锁文件。

我的主要问题是这应该在何时发生,当然还有如何?

我最初的想法是在我的作业监听器将文件移动到新目录之后。此时我只知道文件路径名,并且没有文件本身的句柄。

目录轮询码:

    @Bean
    @InboundChannelAdapter(value = "claimInputChannel", poller = @Poller(fixedDelay = "5000"))
    public FileReadingMessageSource claimPollingFileSource(){
        logger.debug("Setting up inbound channel adapter ===> claimInputChannel");

        CompositeFileListFilter<File> compositeFileListFilter= new CompositeFileListFilter<File>();
        compositeFileListFilter.addFilter(new RegexPatternFileListFilter("(?i).*_CLAIM_.*[.]txt"));
        compositeFileListFilter.addFilter(lastModifiedFilter());
        compositeFileListFilter.addFilter(new IgnoreHiddenFileListFilter());
        compositeFileListFilter.addFilter(new AcceptOnceFileListFilter<File>());

        FileReadingMessageSource pollDirectory = new FileReadingMessageSource();

        pollDirectory.setDirectory(new File(pollingDirectory));
        pollDirectory.setAutoCreateDirectory(false); 
        pollDirectory.setFilter(compositeFileListFilter);
        pollDirectory.setLocker(new NioFileLocker());           
        return pollDirectory;
    }

听众中的AfterJob代码:

@Override
public void afterJob(JobExecution jobExecution) {
    logger.debug("In afterJob routine");

    if (jobExecution.getStatus() == BatchStatus.COMPLETED) {    
        // check if directory exists
        File dir = new File(processedDirectory);
        if (dir.exists()) {
            Path sourcePath = Paths.get(jobExecution.getJobParameters().getString("fileName"));
                String target = processedDirectory + "/" + sourcePath.getFileName();
            logger.debug("Moving processed file " + sourcePath.toString() + " to " + target);

            try {
                Files.move(sourcePath, Paths.get(target), StandardCopyOption.REPLACE_EXISTING);
            } catch (IOException e) {
                logger.error("Something went wrong in moving file to processed directory.");
                e.printStackTrace();
            }
        } else {
            logger.error("The " + processedDirectory + " does not exist.");
        }

    } else {
        logger.debug("Batch job is not yet complete.");
    }

}

0 个答案:

没有答案