2017-02-15 51 views
1

最近我開始研究netflix hystrix庫。我創建了一個HystrixCommand對象,但我得到了一個N​​PE。理想情況下,它不應該發生。任何幫助將不勝感激。這是一個已知的問題嗎?netflix hystrix庫中的空指針異常

請找到堆棧跟蹤:

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) 

回答

2

我遇到類似的問題,這裏是我如何解決它。

蝟 - >使用archaius核-0.4.1.jar - >它使用通用配置-1.8.jar

但由於我的當前項目的jar衝突,公共配置-1.3.jar是目前,而不是公共配置-1.8.jar

不幸的是,似乎是在org.apache.commons.configuration.event.EventSource的構造函數錯誤在公共配置-1.3.jar(我將在下面解釋)

所以,我的建議是看看你的classpath,我是su你會發現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的(公共配置-1.3.jar)

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

正如您在EventSource的構造函數中清楚地看到的,您會看到對方法clearConfigurationListeners()的調用。這個方法在子類ConcurrentMapConfiguration中被覆蓋。所以,子類方法將被調用。但是到此時監聽器仍然是空的,因爲只有在超類構造函數完成後,子類才能開始初始化它的東西。因此NPE。

但在EventSource的(公共配置-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(); 
     ... 
    } 
    ... 
} 

希望這有助於!