Create elasticsearch client throws netty illegalstateexception in server enviroment


#1

The error info

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.elasticsearch.client.transport.TransportClient]: Factory method 'elasticsearchClient' threw exception; nested exception is java.lang.IllegalStateException: availableProcessors is already set to [4], rejecting [4]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:583)
... 112 more
Caused by: java.lang.IllegalStateException: availableProcessors is already set to [4], rejecting [4]
at io.netty.util.NettyRuntime$AvailableProcessorsHolder.setAvailableProcessors(NettyRuntime.java:51)
at io.netty.util.NettyRuntime.setAvailableProcessors(NettyRuntime.java:87)
at org.elasticsearch.transport.netty4.Netty4Utils.setAvailableProcessors(Netty4Utils.java:85)
at org.elasticsearch.transport.netty4.Netty4Transport.(Netty4Transport.java:140)
at org.elasticsearch.transport.Netty4Plugin.lambda$getTransports$0(Netty4Plugin.java:93)
at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:177)
at org.elasticsearch.client.transport.TransportClient.(TransportClient.java:257)

And I add "System.setProperty("es.set.netty.runtime.available.processors", "false");"
public static void main(final String[] args) {
System.setProperty("es.set.netty.runtime.available.processors", "false");
SpringApplication springApplication = new SpringApplication(Application.class);
springApplication.addListeners(new ApplicationPidFileWriter("sns.pid"));
springApplication.run(args);
}

It works in my local enviroment(oracle jdk1.8), but does not work in server
enviroment(openjdk1.8).
I am not sure whether the jdk version matters? If not, how to solve the error?Thanks.

OS: Centos
Springboot: 2.0.6
Elasticsearch: 6.3.1

Maven dependency

 <dependency>
    <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-elasticsearch</artifactId>
        <version>3.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        <version>2.0.6.RELEASE</version>
    </dependency>

Application.properties

spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.repositories.enabled=true


#2

Finally I added the parameters in the start bash, and it worked.

-Des.set.netty.runtime.available.processors=false

I thought that the main function code 'System.setProperty("es.set.netty.runtime.available.processors", "false");' should be loaded first.