"目标服务器无法响应"使用HttpSolrServer时

时间:2014-05-10 09:17:22

标签: java tomcat solr httpclient

http://localhost:8080/solr在我的浏览器中运行良好,当我启动时,tomcat的控制台中没有错误。

我的代码是:

    public static void index() throws SolrServerException, IOException {
        String url = "http://localhost:8080/solr";
        HttpSolrServer server = new HttpSolrServer(url);
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("id", "1");
        doc.addField("name", "name");
        doc.addField("content", "cold today");
        server.add(doc);
        server.commit();
    }

当我运行代码时,tomcat的控制台中没有打印出来,我得到了 而是Caused by: org.apache.http.NoHttpResponseException: The target server failed to respond 完整的故障跟踪是:

    org.apache.solr.client.solrj.SolrServerException: IOException occured when talking to server at: http://localhost:8080/solr
        at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:507)
        at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:199)
        at org.apache.solr.client.solrj.request.AbstractUpdateRequest.process(AbstractUpdateRequest.java:118)
        at org.apache.solr.client.solrj.SolrServer.add(SolrServer.java:116)
        at org.apache.solr.client.solrj.SolrServer.add(SolrServer.java:102)
        at com.cc.solr.SolrDemo.index(SolrDemo.java:21)
        at com.cc.solr.TestSolrDemo.testIndex(TestSolrDemo.java:16)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:483)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
    Caused by: org.apache.http.NoHttpResponseException: The target server failed to respond
        at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:143)
        at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)
        at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:260)
        at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:283)
        at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:251)
        at org.apache.http.impl.conn.ManagedClientConnectionImpl.receiveResponseHeader(ManagedClientConnectionImpl.java:197)
        at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:271)
        at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:123)
        at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:682)
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:486)
        at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:863)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
        at org.apache.solr.client.solrj.impl.HttpSolrServer.request(HttpSolrServer.java:395)
        ... 29 more

我认为我的计算机出了问题,但我无法理解。

我尝试了solr 4.6.0solr 4.7.2,但都失败了 运行此代码时失败:
final HttpResponse response = httpClient.execute(method);
HttpSolrServer.java(line number 395 in 4.7.2)

但是当我把项目复制到室友的电脑上时,它工作正常。

非常奇怪。似乎服务器成功接受了请求,如果我更改了我的代码:

    public static void index() throws SolrServerException, IOException {
        String url = "http://localhost:8080/solr";
        HttpSolrServer server = new HttpSolrServer(url);
        server.setSoTimeout(10000); 
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("id", "3");
        doc.addField("name", "name3");
        doc.addField("content", "cold today3");
        try{
        server.add(doc);
        }catch(Exception e){
            e.printStackTrace(); //time out
        }
        server.commit();
    }  

它仍会打印异常消息,但它有效...

0 个答案:

没有答案