基於上一節介紹的配置源,我們來繼續了解配置管理器。配置源只是抽象了配置的獲取來源,配置管理器是基於配置源的基礎上對這些配置項進行管理。配置管理器的主要功能是將配置從目標位置加載到內存中,並且管理內存配置項,實現讀取配置項,動態更新內存配置項,監聽器功能。
archaius的配置管理器繼承了Apache Commons Configuration的配置管理器,Apache Commons Configuration的AbstractConfiguration主要提供獲取配置項和監聽配置事件的功能,但是他不是線程安全的,archaius的ConcurrentMapConfiguration繼承了AbstractConfiguration並且實現了線程安全(內部其實使用了一個ConcurrentHashMap存儲配置項)。
achaius提供了三個配置管理器:
類路徑配置管理器(ClasspathPropertiesConfiguration)
用於加載jar下的配置文件,ClasspathPropertiesConfiguration不直接管理配置項,而是通過ConfigurationManager加載類路徑下的META-INF/conf/config.properties。
public class ClasspathPropertiesConfiguration extends ConcurrentMapConfiguration
{static String propertiesResourceRelativePath = "META-INF/conf/config.properties"; static ClasspathPropertiesConfiguration instance = null;
public static void initialize() { try { instance = new ClasspathPropertiesConfiguration(); loadResources(propertiesResourceRelativePath); } catch (Exception e) { throw new RuntimeException( "failed to read configuration properties from classpath", e); } } private static void loadResources(String resourceName) throws Exception { ConfigurationManager.loadPropertiesFromResources(resourceName); } }
動態配置管理器(DynamicConfiguration)
DynamicConfiguration實現動態更新配置,內部使用PolledConfigurationSource作為配置源,使用AbstractPollingScheduler來定時從配置源獲取配置然后再更新到DynamicConfiguration中。
子類DynamicURLConfiguration繼承DynamicConfiguration,內部使用URLConfigurationSource和FixedDelayPollingScheduler作為配置源和定時執行器。
監聽配置源動態配置管理器
DynamicWatchedConfiguration實現WatchedUpdateListener,通過監聽WatchedConfigurationSource,實現動態配置項管理。
復合配置管理器
類圖結構