ChronicleQueue - 无法清理旧的CQ4文件(ChronicleQueue - Una

2019-10-29 17:58发布

我使用CronicleQueue,我只有一个写入/读取器。 作为读者与CQ4文件中完成,尽快想清理。 下面的代码无法删除文件,在文件基准由CQ onReleased()事件期间仍持有?

public class ChronicleFactory {
public SingleChronicleQueue createChronicle(String instance, String persistenceDir, RollCycles rollCycles) {
    SingleChronicleQueue chronicle = null;

    String thisInstance = instance;
    try {
    chronicle = SingleChronicleQueueBuilder.binary(persistenceDir).rollCycle(rollCycles).storeFileListener(new StoreFileListener() {

        @Override
        public void onReleased(int i, File file) {
        String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy??-MM-dd HH:mm:ss.SSS"));
        System.out.println(instance + "> " + currentTime + ": " + Thread.currentThread().getName() + " onReleased called for file: " + file.getAbsolutePath() + " for cycle: " + i);
        if(instance.equals("Reader")) {
            System.out.println("Removing previous CQ file: " + file.getName() + ", deleted? " + file.delete());  //==> Not able to delete the file !!!
        }
        }
    .....

Answer 1:

不知道这是否是最佳的解决方案,请让我知道如果有一个更清洁的方式,但下面的作品[但是里面有循环几次在删除之前,因为该文件似乎几毫秒后实际释放,我看到“关闭文件”打印时间少的实际删除发生之前)

                @Override
            public void onAcquired(int cycle, File file) {
                String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy‌​-MM-dd HH:mm:ss.SSS"));
                System.out.println(instance + "> " + currentTime + ": " + Thread.currentThread().getName() + " onAcquired called for file: " + file.getAbsolutePath() + " for cycle: " + cycle);
                if(prevCycleFile != null && instance.equals("Reader")) {
                    System.out.println(">>> Trying to remove: " + prevCycleFile);
                    boolean bDelete;
                    while((bDelete=prevCycleFile.delete())==false) {

                        System.out.println(">>> file is closed");    
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    System.out.println(">>> Removing previous CQ file: " + prevCycleFile.getAbsolutePath() + ", deleted? " + bDelete);
                }
            }


Answer 2:

这是合理的预期报告队列之间的小延迟一个文件被释放,而OS实际检测/作用于它。

StoreFileListener被调用时读取或写入队列在同一线程上,因此当您将承担延迟while循环执行。 你会被移交关闭文件到另一个线程(例如,通过工作获得更好的效果jucConcurrentLinkedQueue )。



文章来源: ChronicleQueue - Unable to cleanup older CQ4 files