gpt4 book ai didi

java - netflix hystrix 库中的空指针异常

转载 作者:行者123 更新时间:2023-11-30 02:39:20 26 4
gpt4 key购买 nike

最近我开始研究 netflix hystrix 库。我创建了一个 HystrixCommand 对象,但得到了一个 NPE。理想情况下,这不应该发生。任何帮助将不胜感激。这是一个已知问题吗?

请找到堆栈跟踪:

Exception in thread "main" java.lang.NullPointerException
at com.netflix.config.ConcurrentMapConfiguration.clearConfigurationListeners(ConcurrentMapConfiguration.java:330)
at org.apache.commons.configuration.event.EventSource.<init>(EventSource.java:76)
at org.apache.commons.configuration.AbstractConfiguration.<init>(AbstractConfiguration.java:63)
at com.netflix.config.ConcurrentMapConfiguration.<init>(ConcurrentMapConfiguration.java:68)
at com.netflix.config.ConcurrentCompositeConfiguration.<init>(ConcurrentCompositeConfiguration.java:172)
at com.netflix.config.ConfigurationManager.getConfigInstance(ConfigurationManager.java:125)
at com.netflix.config.DynamicPropertyFactory.getInstance(DynamicPropertyFactory.java:263)
at com.netflix.config.DynamicProperty.getInstance(DynamicProperty.java:245)
at com.netflix.config.PropertyWrapper.<init>(PropertyWrapper.java:58)
at com.netflix.hystrix.strategy.properties.archaius.HystrixDynamicPropertiesArchaius$ArchaiusDynamicProperty.<init>(HystrixDynamicPropertiesArchaius.java:62)
at com.netflix.hystrix.strategy.properties.archaius.HystrixDynamicPropertiesArchaius$StringDynamicProperty.<init>(HystrixDynamicPropertiesArchaius.java:73)
at com.netflix.hystrix.strategy.properties.archaius.HystrixDynamicPropertiesArchaius.getString(HystrixDynamicPropertiesArchaius.java:34)
at com.netflix.hystrix.strategy.HystrixPlugins.getPluginImplementationViaProperties(HystrixPlugins.java:344)
at com.netflix.hystrix.strategy.HystrixPlugins.getPluginImplementation(HystrixPlugins.java:334)
at com.netflix.hystrix.strategy.HystrixPlugins.getPropertiesStrategy(HystrixPlugins.java:243)
at com.netflix.hystrix.strategy.properties.HystrixPropertiesFactory.getCommandProperties(HystrixPropertiesFactory.java:62)
at com.netflix.hystrix.AbstractCommand.initCommandProperties(AbstractCommand.java:204)
at com.netflix.hystrix.AbstractCommand.<init>(AbstractCommand.java:163)
at com.netflix.hystrix.HystrixCommand.<init>(HystrixCommand.java:61)

最佳答案

我遇到了类似的问题,这是我的解决方法。

Hystrix -> 使用 archaius-core-0.4.1.jar -> 使用 commons-configuration-1.8.jar

但是由于我当前项目中的 jar 冲突,存在 commons-configuration-1.3.jar 而不是 commons-configuration-1.8.jar

不幸的是,org.apache.commons.configuration.event.EventSource 的构造函数中似乎存在commons-configuration-1.3.jar 中的错误(我将在下面解释)

所以,我的建议是查看您的类路径,我相信您会找到 commons-configuration-1.3.jar。如果是这样,只需确保您拥有正确的commons-configuration-1.8.jar。这应该可以解决您的问题!

根本原因:

com.netflix.config.ConcurrentMapConfiguration -> 是 的子类org.apache.commons.configuration.AbstractConfiguration -> 这是 org.apache.commons.configuration.event.EventSource 的子类

这是骨架

public class ConcurrentMapConfiguration extends AbstractConfiguration {
...
private Collection<ConfigurationListener> listeners = new CopyOnWriteArrayList<ConfigurationListener>();
...
public ConcurrentMapConfiguration() {
...
}
...
@Override
public void clearConfigurationListeners() {
listeners.clear(); // Here is the null pointer exception
}
...
}

但是在EventSource(commons-configuration-1.3.jar)中

public class EventSource {
...
public EventSource()
{
clearConfigurationListeners(); // This is the culprit
}
...
public void clearConfigurationListeners()
{
listeners = new LinkedList();
}
...
}

正如您在 EventSource 的构造函数中清楚看到的那样,您会看到对clearConfigurationListeners() 方法的调用。并且该方法已在子类ConcurrentMapConfiguration中被重写。因此,将调用子类方法。但此时监听器仍然为空,因为只有在父类(super class)构造函数完成后,子类才能开始初始化它的东西。因此就有了 NPE。

但在 EventSource(commons-configuration-1.4.jar 及更高版本)中 - 它已修复

public class EventSource {
...
public EventSource()
{
initListeners(); // this is good
}
...
private void initListeners() // private method... Much better! No one can override this :)
{
listeners = new LinkedList();
...
}
...
}

希望这有帮助!

关于java - netflix hystrix 库中的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42249680/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com