graphaware / neo4j-expire

GraphAware Module for Expiring (Deleting) Nodes and Relationships
29 stars 10 forks source link

Nodes not getting expired in neo4j embedded mode #7

Closed sbcd90 closed 7 years ago

sbcd90 commented 7 years ago

Hello,

When I try to run this simple example https://gist.github.com/sbcd90/26e3277f55912f3f05c5ffe5df73b711 using the neo4j-expire module,

it doesn't expire the nodes created.

I get response like,

1st print
1495342901787
1495342931787
2nd print
1495342901787
1495342931787
3rd print
1495342901787
1495342931787

Please help.

bachmanm commented 7 years ago

You're actually doing the whole thing in a single tx without realising it. tx.success() alone does not commit the tx. What you need to do is either call tx.close() afterwards, or even better put it in a try-with-resources block. Something like:

public class GraphAwareExpiryTest {

public static void main(String[] args) throws Exception {
    GraphDatabaseBuilder graphDatabaseBuilder = new GraphDatabaseFactory()
            .newEmbeddedDatabaseBuilder(new File(System.getProperty("java.io.tmpdir") + File.separator + "neo4j9"));
    GraphDatabaseService graphDatabaseService = graphDatabaseBuilder.newGraphDatabase();
    GraphAwareRuntime graphAwareRuntime = GraphAwareRuntimeFactory.createRuntime(graphDatabaseService);

    ExpirationConfiguration configuration = ExpirationConfiguration.defaultConfiguration().withNodeExpirationProperty("expire");

    graphAwareRuntime.registerModule(new ExpirationModule("EXP", graphDatabaseService, configuration));

    graphAwareRuntime.start();
    graphAwareRuntime.waitUntilStarted();

    long now = System.currentTimeMillis();
    long twentySecondsFromNow = now + 20 * 1000;
    long fiftySecondsFromNow = now + 50 * 1000;

    try (Transaction tx = graphDatabaseService.beginTx()) {
        Node s1 = graphDatabaseService.createNode(Label.label("State1"));
        s1.setProperty("name", "Cloudy");
        s1.setProperty("expire", twentySecondsFromNow);

        Node s2 = graphDatabaseService.createNode(Label.label("State1"));
        s2.setProperty("name", "Windy");
        s2.setProperty("expire", fiftySecondsFromNow);

        tx.success();
    }

    System.out.println("1st print");

    try (Transaction tx = graphDatabaseService.beginTx()) {
        graphDatabaseService.findNodes(Label.label("State1")).forEachRemaining(new Consumer<Node>() {
            @Override
            public void accept(Node node) {
                System.out.println(node.getProperty("expire"));
            }
        });

        tx.success();
    }

    Thread.sleep(25000 - (System.currentTimeMillis() - now));

    System.out.println("2nd print");

    try (Transaction tx = graphDatabaseService.beginTx()) {
        graphDatabaseService.findNodes(Label.label("State1")).forEachRemaining(new Consumer<Node>() {
            @Override
            public void accept(Node node) {
                System.out.println(node.getProperty("expire"));
            }
        });

        tx.success();
    }

    Thread.sleep(55000 - (System.currentTimeMillis() - now));

    System.out.println("3rd print");

    try (Transaction tx = graphDatabaseService.beginTx()) {
        graphDatabaseService.findNodes(Label.label("State1")).forEachRemaining(new Consumer<Node>() {
            @Override
            public void accept(Node node) {
                System.out.println(node.getProperty("expire"));
                System.out.println(System.currentTimeMillis() + " is now");
            }
        });

        tx.success();
    }

    graphDatabaseService.shutdown();
}
}