1

EDIT: The problem is now that even though start is called, https://github.com/ttiurani/neo4j-uuid/blob/master/src/main/java/org/neo4j/extension/uuid/UUIDTransactionEventHandler.java is activated only for an empty database of version 2.0.0-M03 (in 1.9.2 it is never called), and in there data.createdNodes() returns an empty iterator, even though a node was created. So it seems that there still is something wrong with registering a transaction event handler. I'm marking this as resolved, as the problem does not have anything to do with PluginLifecycle. I posted a new question here.

ORIGINAL:

I'm trying to get Neo4j to use a custom PluginLifecycle implementation in an embedded server. For some reason Neo4j calls the constructor of the implementation twice but never calls the start() method. The custom PluginLifecycle implementation is here:

https://github.com/ttiurani/neo4j-uuid/blob/master/src/main/java/org/neo4j/extension/uuid/UUIDPluginLifecycle.java

Here's a test project:

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>example</groupId>
  <artifactId>neo4j-embedded-pluginlifecycle-test</artifactId>
  <packaging>jar</packaging>
  <name>Neo4j Embedded PluginLifecycle Implementation Test</name>
  <version>1.0</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <neo4j.version>2.0.0-M03</neo4j.version>
  </properties>

  <repositories>
    <repository>
      <id>neo4j-public-repository</id>
      <url>http://m2.neo4j.org</url>
    </repository>
    <repository>
      <id>oss-sonatype-snapshots</id>
      <name>OSS Sonatype Snapshots</name>
      <url>https://oss.sonatype.org/content/repositories/snapshots</url>
    </repository>
  </repositories>

  <dependencies>
    <!-- Neo4j graph database -->
    <dependency>
      <groupId>org.extendedmind</groupId>
      <artifactId>neo4j-uuid</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>org.neo4j</groupId>
      <artifactId>server-api</artifactId>
      <version>${neo4j.version}</version>
    </dependency>
    <dependency>
      <groupId>org.neo4j.app</groupId>
      <artifactId>neo4j-server</artifactId>
      <version>${neo4j.version}</version>
      <type>jar</type>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.neo4j.app</groupId>
      <artifactId>neo4j-server</artifactId>
      <classifier>static-web</classifier>
      <version>${neo4j.version}</version>
    </dependency>    

    <!-- JUnit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.0</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
        <executions>
          <execution>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <archive>
            <addMavenDescriptor>false</addMavenDescriptor>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

src/test/java/example/PluginLifecycleTest.java:

package example;

import org.junit.Test;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.kernel.GraphDatabaseAPI;
import org.neo4j.server.WrappingNeoServerBootstrapper;
import org.neo4j.server.configuration.Configurator;
import org.neo4j.server.configuration.ServerConfigurator;

public class PluginLifecycleTest{

  @Test
  public void shouldCreateUUIDToNewNode()
  {
        GraphDatabaseAPI graphdb = (GraphDatabaseAPI) new GraphDatabaseFactory()
                .newEmbeddedDatabaseBuilder( "/tmp/neo4j-test" )
                .newGraphDatabase();
        new UUIDPluginLifecycle().start(graphdb, null);

        ServerConfigurator config;
        config = new ServerConfigurator( graphdb );
        config.configuration().setProperty(
            Configurator.THIRD_PARTY_PACKAGES_KEY, "org.neo4j.extension.uuid=/uuid");
          config.configuration().setProperty(
            Configurator.WEBSERVER_PORT_PROPERTY_KEY, 7473);

        WrappingNeoServerBootstrapper srv = new WrappingNeoServerBootstrapper( graphdb, config );
        srv.start();

        Transaction tx = graphdb.beginTx();
        Node node = graphdb.createNode();
        node.setProperty("test", "test");
        long id = node.getId();
        tx.success();

        tx = graphdb.beginTx();
        node = graphdb.getNodeById(id);
        node.getProperty("test");
        // New nodes should have a "uuid" property
        node.getProperty("uuid");
        tx.success();

        srv.stop();
  }
}

For some reason this does not work with error "org.neo4j.graphdb.NotFoundException: org.neo4j.kernel.api.exceptions.PropertyKeyNotFoundException: Property key 'uuid' not found". I've tried with 1.9.2 but the problem persists.

Is there something else I need to do to get it to work?

4

1 回答 1

1

尝试在嵌入式模式下使用 neo4j-uuid 时不需要服务器组件。最简单的方法是调用:

GraphDatabaseService graphdb = new GraphDatabaseFactory()
          .newEmbeddedDatabaseBuilder( "/tmp/neo4j-test" )
          .newGraphDatabase();
new UUIDPluginLifecycle().start(graphDb, null);

并且 UUID 插件应该可以工作。

如果你还需要服务器组件,例如你想允许远程访问,我必须做更多的调试。我的猜测是非托管扩展需要打包在一个 jar 文件中。

于 2013-08-07T07:42:24.217 回答