I am using Elasticsearch Java API Client [8.13] and Elasticsearch (8.13), but I encounter the following exception:
复制代码
unable to create native thread: possibly out of memory or process/resource limits reached
at java.base/java.lang.Thread.start0(Native Method)
at java.base/java.lang.Thread.start(Thread.java:809)
at org.apache.http.impl.nio.client.CloseableHttpAsyncClientBase.start(CloseableHttpAsyncClientBase.java:83)
at org.elasticsearch.client.RestClientBuilder.build(RestClientBuilder.java:302)
at kepler.schedulesearcher.dao.FixQueryTeacherByEsDao.<init>(FixQueryTeacherByEsDao.java:75)
at kepler.schedulesearcher.dao.FixQueryTeacherByEsDao$FastClassByGuice$49598721.GUICE$TRAMPOLINE(<generated>)
at kepler.schedulesearcher.dao.FixQueryTeacherByEsDao$FastClassByGuice$49598721.apply(<generated>)
at com.google.inject.internal.DefaultConstructionProxyFactory$FastClassProxy.newInstance(DefaultConstructionProxyFactory.java:82)
at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:114)
at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:91)
at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:296)
at com.google.inject.internal.SingleParameterInjector.inject(SingleParameterInjector.java:40)
at com.google.inject.internal.SingleParameterInjector.getAll(SingleParameterInjector.java:60)
at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:113)
at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:91)
at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:296)
at com.google.inject.internal.SingleParameterInjector.inject(SingleParameterInjector.java:40)
at com.google.inject.internal.SingleParameterInjector.getAll(SingleParameterInjector.java:60)
at com.google.inject.internal.ConstructorInjector.provision(ConstructorInjector.java:113)
at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:91)
at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:296)
at com.google.inject.internal.InjectorImpl$1.get(InjectorImpl.java:1100)
... 38 common frames omitted
I use VisualVM to monitor my project, and I found many threads similar to elasticsearch-rest-client-550-thread-2
.
When I restart the project, I notice that every request adds two groups of similar threads, each group having 9 threads.
I have tried creating a singleton ElasticSearchClient object in the project to share this object, but every time I use this object, such threads are still created.
Occasionally, in concurrent situations, CPU usage reaches 100%. I am not sure if the memory overflow is related to this phenomenon, but I want to confirm whether it is normal for the ElasticSearch-rest-client-()- thread-()
threads to be created frequently.
my code:
@Singleton
public class ElasticsearchClientManagerUtils {
final static Logger logger
= LoggerFactory.getLogger(ElasticSearchConnectUtils.class);
private static ElasticsearchClientManagerUtils instance;
private static RestClient restClient;
private static ElasticsearchClient client;
private static ElasticsearchTransport transport;
private ElasticsearchClientManagerUtils() {
try {
String elasticSearchHost ="xxx.xxx.xxx.xxx";
String elasticSearchPort = "9200";
String elasticSearchSchema = "https";
String elasticSearchUser = "elastic";
String elasticSearchPassword = "**************";
String caCertificate = "*********************";
String credentials = elasticSearchUser + ":" + elasticSearchPassword;
String esAuthorization = Base64.getEncoder().encodeToString(credentials.getBytes());
RestClientBuilder builder = RestClient.builder(
new HttpHost(elasticSearchHost, Integer.parseInt(elasticSearchPort), elasticSearchSchema)).setHttpClientConfigCallback(httpAsyncClientBuilder -> {
httpAsyncClientBuilder.setMaxConnTotal(2);
httpAsyncClientBuilder.setMaxConnPerRoute(2);
return httpAsyncClientBuilder;
});
Header[] defaultHeaders =
new Header[]{new BasicHeader("Authorization",
"Basic "+esAuthorization)};
SSLContext sslContext = TransportUtils
.sslContextFromCaFingerprint(caCertificate);
BasicCredentialsProvider credsProv = new BasicCredentialsProvider();
credsProv.setCredentials(
AuthScope.ANY, new UsernamePasswordCredentials(elasticSearchUser, elasticSearchPassword)
);
builder.setDefaultHeaders(defaultHeaders);
builder.setHttpClientConfigCallback(hc->hc.setSSLContext(sslContext).setDefaultCredentialsProvider(credsProv));
restClient = builder.build();
// Create the transport with a Jackson mapper
transport = new RestClientTransport(
restClient, new JacksonJsonpMapper());
client = new ElasticsearchClient(transport);
} catch (Exception e) {
logger.info("create esClient error {}",e.getMessage());
}
}
public static synchronized ElasticsearchClientManagerUtils getInstance() {
if (instance == null) {
instance = new ElasticsearchClientManagerUtils();
}
logger.info(instance.toString());
return instance;
}
public ElasticsearchClient getClient() {
return client;
}
public void close() {
try {
if (restClient != null) {
restClient.close();
transport.close();
}
} catch (Exception e) {
logger.info("close es client error:{}",e.getMessage());
}
}
}
Monitoring content: