0

我正在使用 CronicleQueue,但我只有一位作家/读者。想要在阅读器完成 CQ4 文件后立即进行清理。以下代码无法删除文件,在 onReleased() 事件期间 CQ 是否仍持有文件引用?

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 !!!
        }
        }
    .....
4

2 回答 2

0

预计队列报告文件已释放与操作系统实际检测/执行文件之间的小延迟是合理的。

StoreFileListener读取或写入队列的同一线程上调用 ,因此在while循环执行时会产生延迟。通过将关闭文件的工作移交给另一个线程(例如通过 a j.u.c.ConcurrentLinkedQueue),您将获得更好的结果。

于 2018-02-19T08:22:05.380 回答
0

不知道这是否是最佳解决方案,如果有更清洁的方法,请告诉我,但以下工作[除了它必须在删除之前循环几次,因为文件实际上是在几毫秒后释放的,我看到“文件已关闭”在实际删除发生之前打印了几次)

                @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);
                }
            }
于 2018-02-19T05:23:55.813 回答