netflix hystrix库中的空指针异常

时间:2017-02-15 12:39:04

标签: java nullpointerexception hystrix netflix

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

请找到堆栈跟踪:

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)

1 个答案:

答案 0 :(得分:2)

我面临类似的问题,这就是我如何解决它。

Hystrix - &gt;使用archaius-core-0.4.1.jar - &gt;它使用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 - &gt;是一个子类 org.apache.commons.configuration.AbstractConfiguration - &gt;这是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中重写。因此,将调用子类方法。但到目前为止,侦听器仍然为null,因为只有在超类构造函数完成后,子类才能开始初始化它的东西。因此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();
       ...
   }
   ...
}

希望这有帮助!