Common Config provide config parse, watch listener, parent child extend。
Schema is a interface, parse from source to instance
public interface Schema<S, T> {
/**
*
* @param source
* @return
* @throws Exception
*/
T unmarshal(S source) throws Exception;
/**
*
* @param parentTarget
* @param source
* @return
* @throws Exception
*/
T unmarshal(T parentTarget, S source) throws Exception;
}
InputStreamSchema extends Schema, Set source as InputStream
public interface InputStreamSchema<T> extends Schema<InputStream, T> {
}
JAXBSchema extends InputStreamSchema, Parse InputStream to Object by jaxb.
PropertiesSchema extends InputStreamSchema, Parse InputStream to Properties by JDK Properties Api.
GsonSchema extends InputStreamSchema, Parse InputStream to Object by GSon Api.
Config provide config abstract, source as key.
AbstractConfig extends Config, Linked parent config and children config
LocalConfig extends AbstractConfig, create inputstream with url
ZookeeperConfig extends AbstractConfig, create inputstream with zookeeper client
Watcher provide Config Watcher, Define:
public interface Watcher<C extends AbstractConfig<?, ?>> extends Closeable {
/**
*
* @param config
* @param listener
* @throws Exception
*/
void register(C config, AbstractConfigListener<C> listener) throws Exception;
/**
*
* @param config
* @throws Exception
*/
void register(C config) throws Exception;
/**
*
* @param config
* @throws Exception
*/
void unregister(C config) throws Exception;
/**
*
*/
void close() throws IOException;
}
CycleWatcher extends Watcher provide Watcher by cycle.All AbstractConfig can be register
ZookeeperWatcher extends Watcher provide Watcher by Zookeeper Api, ZookeeperConfig can be register.
LocalWatcher extends Watcher provide Watcher by JDK7 WatchService API, LocalConfig can be register.
AbstractConfigListener provide change call back interface.
public interface AbstractConfigListener<C extends AbstractConfig<?, ?>> {
/**
*
* @param config
* @throws Exception
*/
void onModify(C config) throws Exception;
}
LoggerConfigListener extends AbstractConfigListener, implements call back by slf4j api
public class LoggerConfigListener<C extends AbstractConfig<?, ?>> implements AbstractConfigListener<C> {
private static final Logger LOG = LoggerFactory.getLogger(LoggerConfigListener.class);
@Override
public void onModify(C config) throws Exception {
LOG.info(config.getSource() + " changed");
}
}
// create properties schema
private static final InputStreamSchema<Properties> PROPERTIES_SCHEMA = InputStreamSchemaFactory.createPropertiesSchema(StandardCharsets.UTF_8);
// create listener
private static final AbstractConfigListener<LocalConfig<?>> listener = new LoggerConfigListener2<>();
// create watcher
private static final Watcher<LocalConfig<?>> watcher = new CycleWatcher<>(1000 * 60);
//create config 1
private static final LocalConfig<Properties> localConfig = new LocalConfig<>("aa.properties", PROPERTIES_SCHEMA);
//create config 2, it's parent is config 1
private static final LocalConfig<Properties> localConfig2 = new LocalConfig<>("schema-aa2.properties", PROPERTIES_SCHEMA, localConfig);
static {
try {
// register config
watcher.register(localConfig, listener);
watcher.register(localConfig2, listener);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
// get newest config instance
System.out.println(localConfig.getInstance());
}