[Solved] Springboot2.x ElasticSearch Error: availableProcessors is already set to [4], rejecting [4]

Error message:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'elasticsearchClient' defined in class path resource [org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is 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]

The solution to this problem encountered by the springboot startup class:

@SpringBootApplication
public class Application{
    public static void main(String[] args) {
        System.setProperty("es.set.netty.runtime.available.processors","false");
        SpringApplication.run(Application.class, args);
    }
}

The solution to this problem in unit testing:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class ESTest {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    public ESTest(){
        // Add this line of code to the test class constructor
        System.setProperty("es.set.netty.runtime.available.processors","false");
    }

    @Test
    public void testCreateIndex() {
        elasticsearchTemplate.createIndex(Users.class);
    }
}


Read More: