Neo4j ServerPlugin配置

时间:2014-06-30 19:10:56

标签: neo4j

我正在构建一个Neo4J Server插件。我想在neo4j.properties或neo4j-server.properties中手动设置一些配置值,然后插件可以读取并使用这些值。如何从ServerPlugin访问配置值?

澄清: 我非常喜欢能够在未来的Neo4J版本中运行的东西,所以它是公共API的一部分并且不被弃用是最好的。

2 个答案:

答案 0 :(得分:2)

使用Neo4j的内部依赖机制,您可以访问Confighttps://github.com/neo4j/neo4j/blob/master/community/kernel/src/main/java/org/neo4j/kernel/configuration/Config.java)的实例。通过该类,您可以访问配置。

将以下未经测试的代码段作为指导原则:

...
import org.neo4j.kernel.configuration.Config
...

@Description( "An extension to the Neo4j Server accessing config" )
public class ConfigAwarePlugin extends ServerPlugin
{
    @Name( "config" )
    @Description( "Do stuff with config" )
    @PluginTarget( GraphDatabaseService.class )
    public void sample( @Source GraphDatabaseService graphDb ) {
        Config config = ((GraphDatabaseAPI)graphDb).getDependencyResolver().resolveDependency(Config.class);

        // do stuff with config
    }
}

答案 1 :(得分:0)

我使用Properties

    Properties props = new Properties();
    try
    {
        FileInputStream in = new FileInputStream("./conf/neo4j.properties");
        props.load(in);
    }
    catch (FileNotFoundException e)
    {
        try
        {
            FileInputStream in = new FileInputStream("./neo4j.properties");
            props.load(in);
        }
        catch (FileNotFoundException e2)
        {
            logger.warn(e2.getMessage());
        }
        catch (IOException e2)
        {
            logger.warn(e2.getMessage());
        }
    }
    catch (IOException e)
    {
        logger.warn(e.getMessage());
    }
    String myPropertyString = props.getProperty("myProperty");
    if (myPropertyString != null)
    {
        myProperty = Integer.parseInt(myPropertyString);
    }
    else
    {
        myProperty = 100;
    }

并在neo4j.properties我有:

...    
# Enable shell server so that remote clients can connect via Neo4j shell.
#remote_shell_enabled=true
# Specify custom shell port (default is 1337).
#remote_shell_port=1234

myProperty=100