Neo4j无法接受大的http休息请求

时间:2012-11-11 22:52:58

标签: rest http-post neo4j

我正在尝试向一个独立的neo4j服务器发送一个非常大的http发布请求(neo4j-community-1.9.M01)。

Bellow是我正在使用的示例代码。 输入文件:ideas / src / test / test-rest.txt包含一个非常大的json字符串(超过(250k)。

问题是我无法控制独立版neo4j启动的Web服务器的很多设置。可能在Web服务器上有一些限制,阻止我发送大的帖子请求。有人可以帮我找出这个设置。 替代方案:有人可以告诉我如何替换neo4j使用的默认Web服务器,例如使用tomcat。 我在客户端收到的错误是:

Caused by: java.net.SocketException: Connection reset by peer: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
    at java.io.BufferedOutputStream.write(BufferedOutputStream.java:105)
    at java.io.FilterOutputStream.write(FilterOutputStream.java:80)
    at org.apache.commons.httpclient.WireLogOutputStream.write(WireLogOutputStream.java:86)
    at com.sun.jersey.client.apache.DefaultApacheHttpMethodExecutor$3.writeRequest(DefaultApacheHttpMethodExecutor.java:186)
    at org.apache.commons.httpclient.methods.EntityEnclosingMethod.writeRequestBody(EntityEnclosingMethod.java:499)
    at org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.java:2114)
    at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1096)
    at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:398)
    at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
    at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
    at com.sun.jersey.client.apache.DefaultApacheHttpMethodExecutor.executeMethod(DefaultApacheHttpMethodExecutor.java:210)

以下是来自网络客户端的一些日志:

11-11@08:28:01 INFO  (HttpMethodDirector.java:445)     - Retrying request
11-11@08:28:01 DEBUG (HttpConnection.java:692)     - Open connection to localhost:7474
11-11@08:28:01 DEBUG (    Wire.java:70)     - >> "POST /db/data/batch HTTP/1.1[\r][\n]"
11-11@08:28:01 DEBUG (HttpMethodBase.java:1352)     - Adding Host request header
11-11@08:28:01 DEBUG (    Wire.java:70)     - >> "Accept: application/json; stream=true[\r][\n]"
11-11@08:28:01 DEBUG (    Wire.java:70)     - >> "X-Stream: true[\r][\n]"
11-11@08:28:01 DEBUG (    Wire.java:70)     - >> "Content-Type: application/json[\r][\n]"
11-11@08:28:01 DEBUG (    Wire.java:70)     - >> "User-Agent: Jakarta Commons-HttpClient/3.1[\r][\n]"
11-11@08:28:01 DEBUG (    Wire.java:70)     - >> "Content-Length: 246292[\r][\n]"
11-11@08:28:01 DEBUG (    Wire.java:70)     - >> "Host: localhost:7474[\r][\n]"
11-11@08:28:01 DEBUG (    Wire.java:70)     - >> "[\r][\n]"
11-11@08:31:22 DEBUG (HttpMethodDirector.java:404)     - Closing the connection.
11-11@08:31:22 DEBUG (HttpMethodDirector.java:434)     - Method retry handler returned false. Automatic recovery will not be attempted
11-11@08:31:22 DEBUG (HttpConnection.java:1178)     - Releasing connection back to connection manager.

以下是客户端代码:

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.client.apache.ApacheHttpClient;
import org.apache.commons.io.FileUtils;
import org.neo4j.rest.graphdb.RequestResult;
import org.neo4j.rest.graphdb.UserAgent;

import javax.ws.rs.core.MediaType;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;

public class Main {
    private static final UserAgent userAgent = new UserAgent();
    public static final MediaType STREAMING_JSON_TYPE = new MediaType(APPLICATION_JSON_TYPE.getType(),APPLICATION_JSON_TYPE.getSubtype(), stringMap("stream","true"));

    public static void main(String[] args) throws URISyntaxException, IOException {
        String data = FileUtils.readFileToString(new File("ideas/src/test/test-rest.txt"));
        Client client = createClient();
        WebResource resource = client.resource(new URI("http://localhost:7474/db/data/batch"));
        WebResource.Builder builder = resource.accept(STREAMING_JSON_TYPE).header("X-Stream", "true");
        builder.entity( toInputStream(data), APPLICATION_JSON_TYPE );
        System.out.println(RequestResult.extractFrom(builder.post(ClientResponse.class)));
    }

    private static InputStream toInputStream(String data) throws IOException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(4024 * 1024);
        outputStream.write(data.getBytes());
        byte[] buf = outputStream.toByteArray();
        ByteArrayInputStream inputStream = new ByteArrayInputStream(buf);
        return inputStream;
    }


    protected static Client createClient1() {
        Client client = Client.create();
        client.setConnectTimeout(800);
        client.setReadTimeout(800);
        client.setChunkedEncodingSize(80 * 1024);
        userAgent.install(client);
        return client;
    }

    protected static Client createClient() {
        Client client = ApacheHttpClient.create();
        return client;
    }

    private static Map<String, String> stringMap(String stream, String aTrue) {
        HashMap<String, String> result = new HashMap<String, String>();
        result.put(stream, aTrue);
        return result;
    }

}

提前致谢。

1 个答案:

答案 0 :(得分:0)

我找到了问题的原因。 我使用Windows XP客户机和Windows 7主机机器在Virtual BOX 4.2.0下运行。 将服务器放在另一台机器上后,问题就消失了。 我使用wireshark进行了调查,在失败之前我看到了很多未被确认并因此重新传输的tcp包。

我的问题仍然存在:如何用tomcat替换neo4j使用的默认网络服务器?