Search in sources :

Example 1 with StoreFileListener

use of net.openhft.chronicle.queue.impl.StoreFileListener in project Chronicle-Queue by OpenHFT.

the class AcquireReleaseTest method testAccquireAndRelease.

@Test
public void testAccquireAndRelease() throws Exception {
    File dir = DirectoryUtils.tempDir("AcquireReleaseTest");
    try {
        AtomicInteger acount = new AtomicInteger();
        AtomicInteger qcount = new AtomicInteger();
        StoreFileListener sfl = new StoreFileListener() {

            @Override
            public void onAcquired(int cycle, File file) {
                System.out.println("onAcquired(): " + file);
                acount.incrementAndGet();
            }

            @Override
            public void onReleased(int cycle, File file) {
                System.out.println("onReleased(): " + file);
                // TODO Auto-generated method stub
                qcount.incrementAndGet();
            }
        };
        AtomicLong time = new AtomicLong(1000l);
        TimeProvider tp = () -> time.getAndAccumulate(1000, (x, y) -> x + y);
        ChronicleQueue queue = SingleChronicleQueueBuilder.binary(dir).testBlockSize().rollCycle(RollCycles.TEST_SECONDLY).storeFileListener(sfl).timeProvider(tp).build();
        for (int i = 0; i < 10; i++) {
            queue.acquireAppender().writeDocument(w -> {
                w.write("a").marshallable(m -> {
                    m.write("b").text("c");
                });
            });
        }
        Assert.assertEquals(10, acount.get());
        Assert.assertEquals(9, qcount.get());
        queue.close();
    } finally {
        try {
            IOTools.deleteDirWithFiles(dir, 2);
        } catch (IORuntimeException e) {
        // ignored
        }
    }
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) TimeProvider(net.openhft.chronicle.core.time.TimeProvider) IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StoreFileListener(net.openhft.chronicle.queue.impl.StoreFileListener) File(java.io.File) Test(org.junit.Test)

Aggregations

File (java.io.File)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 IORuntimeException (net.openhft.chronicle.core.io.IORuntimeException)1 TimeProvider (net.openhft.chronicle.core.time.TimeProvider)1 StoreFileListener (net.openhft.chronicle.queue.impl.StoreFileListener)1 Test (org.junit.Test)1