Search in sources :

Example 6 with KernelEventHandler

use of org.neo4j.graphdb.event.KernelEventHandler in project neo4j by neo4j.

the class KernelEventHandlers method registerKernelEventHandler.

public KernelEventHandler registerKernelEventHandler(KernelEventHandler handler) {
    if (this.kernelEventHandlers.contains(handler)) {
        return handler;
    }
    // Some algo for putting it in the right place
    for (KernelEventHandler registeredHandler : this.kernelEventHandlers) {
        KernelEventHandler.ExecutionOrder order = handler.orderComparedTo(registeredHandler);
        int index = this.kernelEventHandlers.indexOf(registeredHandler);
        if (order == KernelEventHandler.ExecutionOrder.BEFORE) {
            this.kernelEventHandlers.add(index, handler);
            return handler;
        } else if (order == KernelEventHandler.ExecutionOrder.AFTER) {
            this.kernelEventHandlers.add(index + 1, handler);
            return handler;
        }
    }
    this.kernelEventHandlers.add(handler);
    return handler;
}
Also used : KernelEventHandler(org.neo4j.graphdb.event.KernelEventHandler)

Example 7 with KernelEventHandler

use of org.neo4j.graphdb.event.KernelEventHandler in project graphdb by neo4j-attic.

the class TestKernelEvents method testRegisterUnregisterHandlers.

@Test
public void testRegisterUnregisterHandlers() {
    GraphDatabaseService graphDb = new EmbeddedGraphDatabase(PATH);
    KernelEventHandler handler1 = new DummyKernelEventHandler(RESOURCE1) {

        public ExecutionOrder orderComparedTo(KernelEventHandler other) {
            return ExecutionOrder.DOESNT_MATTER;
        }
    };
    KernelEventHandler handler2 = new DummyKernelEventHandler(RESOURCE2) {

        public ExecutionOrder orderComparedTo(KernelEventHandler other) {
            return ExecutionOrder.DOESNT_MATTER;
        }
    };
    try {
        graphDb.unregisterKernelEventHandler(handler1);
        fail("Shouldn't be able to do unregister on a " + "unregistered handler");
    } catch (IllegalStateException e) {
    /* Good */
    }
    assertTrue(handler1 == graphDb.registerKernelEventHandler(handler1));
    assertTrue(handler1 == graphDb.registerKernelEventHandler(handler1));
    assertTrue(handler1 == graphDb.unregisterKernelEventHandler(handler1));
    try {
        graphDb.unregisterKernelEventHandler(handler1);
        fail("Shouldn't be able to do unregister on a " + "unregistered handler");
    } catch (IllegalStateException e) {
    /* Good */
    }
    assertTrue(handler1 == graphDb.registerKernelEventHandler(handler1));
    assertTrue(handler2 == graphDb.registerKernelEventHandler(handler2));
    assertTrue(handler1 == graphDb.unregisterKernelEventHandler(handler1));
    assertTrue(handler2 == graphDb.unregisterKernelEventHandler(handler2));
    graphDb.shutdown();
}
Also used : EmbeddedGraphDatabase(org.neo4j.kernel.EmbeddedGraphDatabase) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) KernelEventHandler(org.neo4j.graphdb.event.KernelEventHandler) Test(org.junit.Test)

Example 8 with KernelEventHandler

use of org.neo4j.graphdb.event.KernelEventHandler in project graphdb by neo4j-attic.

the class TestShutdownSequence method eventHandlersAreOnlyInvokedOnceDuringShutdown.

@Test
public void eventHandlersAreOnlyInvokedOnceDuringShutdown() {
    final AtomicInteger counter = new AtomicInteger();
    graphDb.registerKernelEventHandler(new KernelEventHandler() {

        public void beforeShutdown() {
            counter.incrementAndGet();
        }

        public Object getResource() {
            return null;
        }

        public void kernelPanic(ErrorState error) {
        // do nothing
        }

        public ExecutionOrder orderComparedTo(KernelEventHandler other) {
            return ExecutionOrder.DOESNT_MATTER;
        }
    });
    graphDb.shutdown();
    graphDb.shutdown();
    assertEquals(1, counter.get());
}
Also used : KernelEventHandler(org.neo4j.graphdb.event.KernelEventHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ErrorState(org.neo4j.graphdb.event.ErrorState) Test(org.junit.Test)

Example 9 with KernelEventHandler

use of org.neo4j.graphdb.event.KernelEventHandler in project neo4j by neo4j.

the class TestShutdownSequence method canInvokeShutdownFromShutdownHandler.

@Test
public void canInvokeShutdownFromShutdownHandler() {
    graphDb.registerKernelEventHandler(new KernelEventHandler() {

        @Override
        public void beforeShutdown() {
            graphDb.shutdown();
        }

        @Override
        public Object getResource() {
            return null;
        }

        @Override
        public void kernelPanic(ErrorState error) {
        // do nothing
        }

        @Override
        public ExecutionOrder orderComparedTo(KernelEventHandler other) {
            return ExecutionOrder.DOESNT_MATTER;
        }
    });
    graphDb.shutdown();
}
Also used : KernelEventHandler(org.neo4j.graphdb.event.KernelEventHandler) ErrorState(org.neo4j.graphdb.event.ErrorState) Test(org.junit.Test)

Example 10 with KernelEventHandler

use of org.neo4j.graphdb.event.KernelEventHandler in project neo4j by neo4j.

the class TestShutdownSequence method eventHandlersAreOnlyInvokedOnceDuringShutdown.

@Test
public void eventHandlersAreOnlyInvokedOnceDuringShutdown() {
    final AtomicInteger counter = new AtomicInteger();
    graphDb.registerKernelEventHandler(new KernelEventHandler() {

        @Override
        public void beforeShutdown() {
            counter.incrementAndGet();
        }

        @Override
        public Object getResource() {
            return null;
        }

        @Override
        public void kernelPanic(ErrorState error) {
        // do nothing
        }

        @Override
        public ExecutionOrder orderComparedTo(KernelEventHandler other) {
            return ExecutionOrder.DOESNT_MATTER;
        }
    });
    graphDb.shutdown();
    graphDb.shutdown();
    assertEquals(1, counter.get());
}
Also used : KernelEventHandler(org.neo4j.graphdb.event.KernelEventHandler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ErrorState(org.neo4j.graphdb.event.ErrorState) Test(org.junit.Test)

Aggregations

KernelEventHandler (org.neo4j.graphdb.event.KernelEventHandler)11 Test (org.junit.Test)8 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)4 ErrorState (org.neo4j.graphdb.event.ErrorState)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 EmbeddedGraphDatabase (org.neo4j.kernel.EmbeddedGraphDatabase)2 TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)2