java - 如何在运行 Eclipse 的 GWT 中获取 Neo4j Graph 数据库

标签 java eclipse gwt neo4j

您好,我尝试在 GWT 中使用 eclipse 创建一个 neo4j 图形数据库。 我用了这个视频https://www.youtube.com/watch?v=WSj5hNwDnEc 但是 chrom 和 nappi 的 gwt 有一些错误(通过像 Windows 7 一样运行 chrom 来修复它)

然后我这样写了我的代码:

/**
 * The server-side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
GreetingService {

private static enum RelTypes implements RelationshipType
{
    KNOWS
}

private static final String DB_PATH = "target/neo4j-hello-db";
private static String result;

GraphDatabaseService graphDb;
Node firstNode;
Node secondNode;
Relationship relationship;

public String greetServer(String input) throws IllegalArgumentException {
    // Verify that the input is valid. 
    if (!FieldVerifier.isValidName(input)) {
        // If the input is not valid, throw an IllegalArgumentException back to
        // the client.
        throw new IllegalArgumentException(
                "Name must be at least 4 characters long");
    }

    String serverInfo = getServletContext().getServerInfo();
    String userAgent = getThreadLocalRequest().getHeader("User-Agent");

    // Escape data from the client to avoid cross-site script vulnerabilities.
    input = escapeHtml(input);
    userAgent = escapeHtml(userAgent);
    System.out.println("1");

    result="";



    createDb();
    System.out.println("2");
    removeData();
    System.out.println("3");
    shutDown();






    return result + input + "!<br><br>I am running " + serverInfo
            + ".<br><br>It looks like you are using:<br>" + userAgent;
}

private void shutDown() {
    // TODO Auto-generated method stub
    //         System.out.println();
    //          System.out.println( "Shutting down database ..." );
    //          // START SNIPPET: shutdownServer
    graphDb.shutdown();
    // END SNIPPET: shutdownServer

}

private void removeData() {
    // TODO Auto-generated method stub
    try ( Transaction tx = graphDb.beginTx() )
    {
        // START SNIPPET: removingData
        // let's remove the data
        firstNode.getSingleRelationship( RelTypes.KNOWS, Direction.OUTGOING ).delete();
        firstNode.delete();
        secondNode.delete();
        // END SNIPPET: removingData

        tx.success();
    }

}

private void createDb() {
    deleteFileOrDirectory( new File( DB_PATH ) );
    System.out.println("2.1");
    // START SNIPPET: startDb
    graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH );
    registerShutdownHook( graphDb );
    System.out.println("******************2.2*******************************************");
    registerShutdownHook( graphDb );
    // END SNIPPET: startDb

    // START SNIPPET: transaction
    try ( Transaction tx = graphDb.beginTx() )
    {
        // Database operations go here
        // END SNIPPET: transaction
        // START SNIPPET: addData
        firstNode = graphDb.createNode();
        firstNode.setProperty( "message", "Hello, " );
        secondNode = graphDb.createNode();
        secondNode.setProperty( "message", "World!" );

        relationship = firstNode.createRelationshipTo( secondNode, RelTypes.KNOWS );
        relationship.setProperty( "message", "brave Neo4j " );
        // END SNIPPET: addData

        // START SNIPPET: readData
        result +=firstNode.getProperty( "message" );
        result +=relationship.getProperty( "message" );
        result +=secondNode.getProperty( "message" );
        // END SNIPPET: readData


        // START SNIPPET: transaction
        tx.success();
    }
    // END SNIPPET: transaction

}

private static void deleteFileOrDirectory( File file )
{
    if ( file.exists() )
    {
        if ( file.isDirectory() )
        {
            for ( File child : file.listFiles() )
            {
                deleteFileOrDirectory( child );
            }
        }
        file.delete();
    }
}

private static void registerShutdownHook( final GraphDatabaseService graphDb )
{
    // Registers a shutdown hook for the Neo4j instance so that it
    // shuts down nicely when the VM exits (even if you "Ctrl-C" the
    // running application).
    Runtime.getRuntime().addShutdownHook( new Thread()
    {
        @Override
        public void run()
        {
            graphDb.shutdown();
        }
    } );
}


/**
 * Escape an html string. Escaping data received from the client helps to
 * prevent cross-site script vulnerabilities.
 * 
 * @param html the html string to escape
 * @return the escaped string
 */
private String escapeHtml(String html) {
    if (html == null) {
        return null;
    }
    return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
            .replaceAll(">", "&gt;");
}

. . . .

现在我的问题是这条线:

    graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH );

这是我得到的错误:

服务方法'public abstract java.lang.String neo4jingwtpackage.client.GreetingService.greetServer(java.lang.String) throws java.lang.IllegalArgumentException' throws an unexpected exception: java.lang.RuntimeException: Error starting org.neo4j .kernel.EmbeddedGraphDatabase,C:\Users\yogev\workspace\temp\neo4jinjgwt\war\target\neo4j-hello-db . . .

引起:java.lang.RuntimeException: Error starting org.neo4j.kernel.EmbeddedGraphDatabase, C:\Users\yogev\workspace\temp\neo4jinjgwt\war\target\neo4j-hello-db . . . Caused by: org.neo4j.kernel.lifecycle.LifecycleException: Component 'org.neo4j.kernel.impl.transaction.XaDataSourceManager@30e1e97f' 已成功初始化,但未能启动。请参阅附件原因异常。

有人知道这个问题吗? 我试图通过以下方式修复它: EmbeddedGraphDatabase(storeDir, params, dependencies)

但我不知道在参数和依赖项中放什么... 当我使用 null 时,它也给了我错误。

谢谢大家

最佳答案

您没有将 Neo4j 交叉编译到客户端,您必须将 Neo4j 作为后端服务的一部分运行。并通过 GWT 远程基础设施访问它。

创建新 GDB 实例的 API 是:

new GraphDatabaseFactory().newEmbeddedDatabase()

参见:http://neo4j.com/docs/stable/tutorials-java-embedded-hello-world.html#_prepare_the_database

关于java - 如何在运行 Eclipse 的 GWT 中获取 Neo4j Graph 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27848892/

相关文章:

java - Tomcat v8+Servlet v3.1 在运行 servlet 时出现 404 错误

java - 如何检查注释的相等性?

java - 在Eclipse中降级java版本

java - GWT 文本框编码和 RPC

Java 字符串不等于

java - java中未知的父变量问题

eclipse - Gradle多项目任务执行行为

eclipse - eclipse中当前日期和时间的命令行参数

java - 如何获取树项的所有父级?

java - GWT 中的 MVP 模式 : How to initialize Model/View and handle server requests in the Presenter?