Neo4j 3.1.2嵌入式数据库关闭不能与apoc.periodic.iterate一起使用

时间:2018-03-30 16:42:02

标签: neo4j neo4j-apoc

我正在使用shutdown hook和apoc程序注册启动Neo4j嵌入式应用程序。我正在使用apoc.periodic.iterate过程调用{batchSize:100,parallel:true,iterateList:true} config。获得Result后,我正在调用graphdb.shutdown。调用shutdown方法后,DB没有关闭。我错过了什么吗?

公共课APOCTest {

private static final File DB_PATH = new File("I://apps//neo4j-enterprise-3.1.2//data//databases//graph.db");

public static void main(String[] args) throws JsonParseException, UnsupportedEncodingException, IOException {
    System.out.println("Neo4j starting " + new Date());

    GraphDatabaseService graphDb = new HighlyAvailableGraphDatabaseFactory().newEmbeddedDatabaseBuilder(DB_PATH)
            .loadPropertiesFromFile("I://apps//standalone//neo4j.conf").newGraphDatabase();

    registerShutdownHook(graphDb);
    registerProcedures(graphDb);
    System.out.println("Neo4j started:: " + new Date());

    createData(graphDb);
    deleteData(graphDb);

    graphDb.shutdown();

}

private static void registerProcedures(final GraphDatabaseService graphDb) {
    Procedures procedures = ((GraphDatabaseAPI) graphDb).getDependencyResolver()
            .resolveDependency(Procedures.class);

    List<Class<?>> apocProcedures = asList(Coll.class, apoc.map.Maps.class, Json.class, Create.class,
            apoc.date.Date.class, FulltextIndex.class, apoc.lock.Lock.class, LoadJson.class, Xml.class,
            PathExplorer.class, Meta.class, GraphRefactoring.class, apoc.periodic.Periodic.class);
    apocProcedures.forEach((proc) -> {
        try {
            procedures.registerProcedure(proc);
            procedures.registerFunction(proc);

        } catch (KernelException e) {
            throw new RuntimeException("Error registering " + proc, e);
        }
    });

}

private static void registerShutdownHook(final GraphDatabaseService graphDb) {

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            graphDb.shutdown();
        }
    });
}

private static void createData(final GraphDatabaseService graphDb) {
    System.out.println("createData " + new Date());
    Transaction tx = graphDb.beginTx();
    try {

        for (int i = 1; i < 900000; i++) {
            Node n = graphDb.createNode(Label.label("Test"));
            n.setProperty("createDate", System.currentTimeMillis());
            if (i % 30000 == 0) {
                tx.success();
                tx.close();
                tx = graphDb.beginTx();
            }

        }
        tx.success();

    } finally {
        tx.close();
    }
    System.out.println("createData " + new Date());
}

private static void deleteData(final GraphDatabaseService graphDb) {
    System.out.println("deleteData " + new Date());
    try (Transaction tx = graphDb.beginTx()) {

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("oldStmtDate1", 1);

        String query = "CALL apoc.periodic.iterate(\"MATCH (n:Test) WHERE n.createDate > {oldStmtDate} RETURN n \",\"DETACH DELETE n\",{batchSize:100,parallel:true,iterateList:true,params:{oldStmtDate:{oldStmtDate1}}})";
        Result r = graphDb.execute(query, map);
        System.out.println(r.next());

        tx.success();
    }
    System.out.println("deleteData " + new Date());
}

}

0 个答案:

没有答案