共计 3637 个字符,预计需要花费 10 分钟才能阅读完成。
背景
我的项目基于 SpringBoot 并且集成 ElasticSearch,明天在编写测试类筹备进行单元测试时,报了如下这个谬误。
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'client' defined in class path resource [com/lingyejun/project/ds/EsRepositoryConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.elasticsearch.client.Client]: Factory method 'client' threw exception; nested exception is java.lang.IllegalStateException: availableProcessors is already set to [8], rejecting [8]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:590)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1256)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1105)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:503)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:251)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:818)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:724)
... 94 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.elasticsearch.client.Client]: Factory method 'client' threw exception; nested exception is java.lang.IllegalStateException: availableProcessors is already set to [8], rejecting [8]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:582)
... 107 more
Caused by: java.lang.IllegalStateException: availableProcessors is already set to [8], rejecting [8]
查看了原有代码是如下设置的
public static void main(String[] args) {System.setProperty("es.set.netty.runtime.available.processors","false");
SpringApplication.run(TestApplication.class, args);
}
解决
查阅材料后发现有两种解决形式
1. 将代码段:System.setProperty(“es.set.netty.runtime.available.processors”,”false”);挪到动态代码块中。
如下:
static{System.setProperty("es.set.netty.runtime.available.processors", "false");
}
public static void main(String[] args) {SpringApplication.run(TestApplication.class, args);
}
2. 在 jvm 启动参数加上
-Des.set.netty.runtime.available.processors=false
起因
此问题见:Issue with NettyRuntime$AvailableProcessorsHolder: number of available processors · Issue #6956 · netty/netty · GitHub
起因是程序的其余中央应用了 Netty,如大部分我的项目也会同时应用 Redis。这会影响在实例化传输客户端以前初始化处理器的数量。因为在其余中央应用 Netty,所以曾经进行了初始化而且 Netty 会对此进行防备,所以首次实例化会因看到的非法状态异样而失败。
本篇文章如有帮忙到您,请给「翎野君」点个赞,感谢您的反对。
正文完