我制作了一个java控制台应用程序来查询solr。这些查询非常密集,因为有些查询是按id进行的(同一个查询中大约有500个id),有些查询是按文本属性进行的。这个应用程序是多线程的,当我在一个线程中运行它时,我没有错误,但是当我设置多线程时,在运行一段时间后,我开始得到以下错误:
org.apache.solr.client.solrj.SolrServerException: IOException occured when talking to server at: http://xxx.xxx.xx.xxx:8983/solr/xx
at org.apache.solr.client.solrj.impl.HttpSolrClient.executeMethod(HttpSolrClient.java:624)
at org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:279)
at org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:268)
at org.apache.solr.client.solrj.SolrRequest.process(SolrRequest.java:149)
at org.apache.solr.client.solrj.SolrClient.query(SolrClient.java:942)
at org.apache.solr.client.solrj.SolrClient.query(SolrClient.java:957)
at org.example.ProcessFile.getByPropertyFromSolr(ProcessFile.java:570)
at org.example.ProcessFile.processLinesBulk(ProcessFile.java:226)
at org.example.ProcessFile.importItemsInFile(ProcessFile.java:117)
at org.example.ProcessFile.call(ProcessFile.java:97)
at org.example.ProcessFile.call(ProcessFile.java:56)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.NoRouteToHostException: Cannot assign requested address (Address not available)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180)
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:326)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:605)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:440)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:835)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
at org.apache.solr.client.solrj.impl.HttpSolrClient.executeMethod(HttpSolrClient.java:515)
这是我的maven依赖:
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>6.4.0</version>
</dependency>
这是抛出错误的函数:
private SolrDocument getByPropertyFromSolr(String property, String value){
String urlString = dotenv.get("GAIMP_SOLR_URL");
HttpSolrClient solr = new HttpSolrClient.Builder(urlString).build();
solr.setParser(new XMLResponseParser());
try {
SolrQuery query = new SolrQuery();
query.set("q", property + ":\"" + ClientUtils.escapeQueryChars(value) + "\"");
query.setStart(0);
query.setRows(1);
QueryResponse response = solr.query(query);
SolrDocumentList results = response.getResults();
if (results.size() > 0) {
return results.get(0);
}
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
solr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
知道在单线程中我没有这个问题,我相信这是因为solr服务器需要很长时间来响应一个请求,而另一个线程正在触发另一个查询。
有什么提示让我没有这个错误吗?我可以将solrclient设置为等待服务器响应而不是抛出错误吗?
暂无答案!
目前还没有任何答案,快来回答吧!