配置源
配置源定义和实现了获取配置文件的方式。有两种配置源,一种是主动拉去方式获取配置,一种是被动监听方式获取配置
类图结构:
接口PolledConfigurationSource定义了获取配置的方法
public PollResult poll(boolean initial, Object checkPoint) throws Exception;
initial是否是初次获取,checkPoint上次获取的检查点。
JDBCConfigurationSource 从数据库中获取配置信息。从指定的DataSource中读取指定的key和value字段。
public PollResult poll(boolean initial, Object checkPoint) throws Exception { Map<String, Object> map = new HashMap<String, Object>(); ... conn = getConnection(); pstmt = conn.prepareStatement(query.toString()); rs = pstmt.executeQuery(); while (rs.next()) { String key = (String) rs.getObject(keyColumnName); Object value = rs.getObject(valueColumnName); map.put(key, value); } ... return PollResult.createFull(map); }
URLConfigurationSource从指定url中获取配置信息。从指定的url中读取配置文件。
public PollResult poll(boolean initial, Object checkPoint) throws IOException { if (configUrls == null || configUrls.length == 0) { return PollResult.createFull(null); } Map<String, Object> map = new HashMap<String, Object>(); for (URL url: configUrls) { InputStream fin = url.openStream(); Properties props = ConfigurationUtils.loadPropertiesFromInputStream(fin); for (Entry<Object, Object> entry: props.entrySet()) { map.put((String) entry.getKey(), entry.getValue()); } } return PollResult.createFull(map); }
WatchedConfigurationSource定义了监听器,当配置发生变化时,调用这些监听器。
public void addUpdateListener(WatchedUpdateListener l); public void removeUpdateListener(WatchedUpdateListener l); public Map<String, Object> getCurrentData() throws Exception;