Search in sources :

Example 1 with KernelEventHandler

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

the class TestKernelEvents method testShutdownEvents.

@Test
public void testShutdownEvents() {
    GraphDatabaseService graphDb = new TestGraphDatabaseFactory().newImpermanentDatabase();
    DummyKernelEventHandler handler1 = new DummyKernelEventHandler(RESOURCE1) {

        @Override
        public ExecutionOrder orderComparedTo(KernelEventHandler other) {
            if (((DummyKernelEventHandler) other).resource == RESOURCE2) {
                return ExecutionOrder.AFTER;
            }
            return ExecutionOrder.DOESNT_MATTER;
        }
    };
    DummyKernelEventHandler handler2 = new DummyKernelEventHandler(RESOURCE1) {

        @Override
        public ExecutionOrder orderComparedTo(KernelEventHandler other) {
            if (((DummyKernelEventHandler) other).resource == RESOURCE1) {
                return ExecutionOrder.BEFORE;
            }
            return ExecutionOrder.DOESNT_MATTER;
        }
    };
    graphDb.registerKernelEventHandler(handler1);
    graphDb.registerKernelEventHandler(handler2);
    graphDb.shutdown();
    assertEquals(Integer.valueOf(0), handler2.beforeShutdown);
    assertEquals(Integer.valueOf(1), handler1.beforeShutdown);
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) KernelEventHandler(org.neo4j.graphdb.event.KernelEventHandler) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) Test(org.junit.Test)

Example 2 with KernelEventHandler

use of org.neo4j.graphdb.event.KernelEventHandler in project neo4j-mobile-android by neo4j-contrib.

the class EmbeddedGraphDbImpl method registerKernelEventHandler.

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 3 with KernelEventHandler

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

the class EmbeddedGraphDbImpl method registerKernelEventHandler.

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 4 with KernelEventHandler

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

the class TestKernelEvents method testShutdownEvents.

@Test
public void testShutdownEvents() {
    GraphDatabaseService graphDb = new EmbeddedGraphDatabase("target/var/neodb");
    DummyKernelEventHandler handler1 = new DummyKernelEventHandler(RESOURCE1) {

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

        public ExecutionOrder orderComparedTo(KernelEventHandler other) {
            if (((DummyKernelEventHandler) other).resource == RESOURCE1) {
                return ExecutionOrder.BEFORE;
            }
            return ExecutionOrder.DOESNT_MATTER;
        }
    };
    graphDb.registerKernelEventHandler(handler1);
    graphDb.registerKernelEventHandler(handler2);
    graphDb.shutdown();
    assertEquals(Integer.valueOf(0), handler2.beforeShutdown);
    assertEquals(Integer.valueOf(1), handler1.beforeShutdown);
}
Also used : EmbeddedGraphDatabase(org.neo4j.kernel.EmbeddedGraphDatabase) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) KernelEventHandler(org.neo4j.graphdb.event.KernelEventHandler) Test(org.junit.Test)

Example 5 with KernelEventHandler

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

the class TestShutdownSequence method canInvokeShutdownFromShutdownHandler.

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

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

        public Object getResource() {
            return null;
        }

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

        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)

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