Last active
May 21, 2017 20:29
-
-
Save sbcd90/26e3277f55912f3f05c5ffe5df73b711 to your computer and use it in GitHub Desktop.
Graph Aware expiry module test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.neo4j.test; | |
import com.graphaware.neo4j.expire.ExpirationModule; | |
import com.graphaware.neo4j.expire.config.ExpirationConfiguration; | |
import com.graphaware.runtime.GraphAwareRuntime; | |
import com.graphaware.runtime.GraphAwareRuntimeFactory; | |
import org.neo4j.graphdb.GraphDatabaseService; | |
import org.neo4j.graphdb.Label; | |
import org.neo4j.graphdb.Node; | |
import org.neo4j.graphdb.Transaction; | |
import org.neo4j.graphdb.factory.GraphDatabaseBuilder; | |
import org.neo4j.graphdb.factory.GraphDatabaseFactory; | |
import java.io.File; | |
import java.util.function.Consumer; | |
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 + "neo4j11")); | |
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")); | |
} | |
}); | |
tx.success(); | |
} | |
graphDatabaseService.shutdown(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 calltx.close()
afterwards, or even better put it in a try-with-resources block. Something like: