使用elasticsearch-rest-high-level-client 7.3.1版本的异步方法报错ConnectionClosedException
Elasticsearch | 作者 try2learn | 发布于2019年09月14日 | 阅读数:9562
服务端使用的是elasticsearch7.3.1版本,java端使用elasticsearch-rest-high-level-client 7.3.1版本的异步方法,例如:
indexAsync和getAsync。会报一下错误:
org.apache.http.ConnectionClosedException: Connection is closed
at org.apache.http.impl.nio.NHttpConnectionBase.assertNotClosed(NHttpConnectionBase.java:502)
at org.apache.http.impl.nio.DefaultNHttpClientConnection.submitRequest(DefaultNHttpClientConnection.java:320)
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.requestReady(HttpAsyncRequestExecutor.java:211)
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.connected(HttpAsyncRequestExecutor.java:134)
at org.apache.http.impl.nio.client.InternalIODispatch.onConnected(InternalIODispatch.java:63)
at org.apache.http.impl.nio.client.InternalIODispatch.onConnected(InternalIODispatch.java:39)
at org.apache.http.impl.nio.reactor.AbstractIODispatch.connected(AbstractIODispatch.java:73)
at org.apache.http.impl.nio.reactor.BaseIOReactor.sessionCreated(BaseIOReactor.java:246)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processNewChannels(AbstractIOReactor.java:429)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:287)
at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:591)
at java.base/java.lang.Thread.run(Thread.java:835)
而使用一些同步方法却不会报错,例如index和get。
这个是我调用indexAsync和getAsync方法的代码
indexAsync和getAsync。会报一下错误:
org.apache.http.ConnectionClosedException: Connection is closed
at org.apache.http.impl.nio.NHttpConnectionBase.assertNotClosed(NHttpConnectionBase.java:502)
at org.apache.http.impl.nio.DefaultNHttpClientConnection.submitRequest(DefaultNHttpClientConnection.java:320)
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.requestReady(HttpAsyncRequestExecutor.java:211)
at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.connected(HttpAsyncRequestExecutor.java:134)
at org.apache.http.impl.nio.client.InternalIODispatch.onConnected(InternalIODispatch.java:63)
at org.apache.http.impl.nio.client.InternalIODispatch.onConnected(InternalIODispatch.java:39)
at org.apache.http.impl.nio.reactor.AbstractIODispatch.connected(AbstractIODispatch.java:73)
at org.apache.http.impl.nio.reactor.BaseIOReactor.sessionCreated(BaseIOReactor.java:246)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.processNewChannels(AbstractIOReactor.java:429)
at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:287)
at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)
at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:591)
at java.base/java.lang.Thread.run(Thread.java:835)
而使用一些同步方法却不会报错,例如index和get。
这个是我调用indexAsync和getAsync方法的代码
@Autowired
private final RestHighLevelClient client;
public void addBookAsync(Book book){
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("type",book.getType());
jsonMap.put("word_count",book.getWordCount());
jsonMap.put("title",book.getTitle());
jsonMap.put("author",book.getAuthor());
jsonMap.put("publish_date",book.getPublishDate());
IndexRequest indexRequest = new IndexRequest("book").source(jsonMap);
client.indexAsync(indexRequest, RequestOptions.DEFAULT, new ActionListener<IndexResponse>() {
@Override
public void onResponse(IndexResponse indexResponse) {
System.out.println("He did it!");
}
@Override
public void onFailure(Exception e) {
e.printStackTrace();
}
});
}
public void getBookByIdAsync(String id){
GetRequest getRequest = new GetRequest("book",id);
client.getAsync(getRequest, RequestOptions.DEFAULT,
new ActionListener<GetResponse>() {
@Override
public void onResponse(GetResponse documentFields) {
if (documentFields.isExists()) {
System.out.println(documentFields.toString());
}
}
@Override
public void onFailure(Exception e) {
e.printStackTrace();
}
});
}
今天弄了一天都没弄明白是怎么回事,希望大家能帮到我,谢谢
2 个回复
regin
赞同来自:
foresttiger
赞同来自:
请确保你调用这个方法得时候,整个程序没有结束。