Skip to content

Commit

Permalink
support config listener
Browse files Browse the repository at this point in the history
  • Loading branch information
ihaolin committed Oct 13, 2016
1 parent 0c0cf1d commit 7e7bb42
Show file tree
Hide file tree
Showing 24 changed files with 484 additions and 70 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,20 @@
client.setAppName("myapp");
client.setAppKey("123456");
client.setServers("127.0.0.1:2143,127.0.0.1:2144");
// 可添加配置监听
client.addListener(new ConfigListener<String>() {
@Override
public String name() {
// 配置项名称
return "test_config1";
}

@Override
public void onUpdate(String newValue) {
// 该方法会在本地配置更新后触发
System.out.println("test_config1 has updated to " + newValue);
}
});
client.start();
// 获取配置项最新的值
Expand Down Expand Up @@ -156,7 +170,22 @@
<property name="appName" value="myapp" />
<property name="appKey" value="123456" />
<property name="servers" value="127.0.0.1:2143,127.0.0.1:2144" />
<!-- 可选配置 -->
<property name="listeners">
<list>
<ref bean="activityNoListener" />
<ref bean="timeInfoListener" />
<ref bean="timeInfosListener" />
<ref bean="timeInfoMapListener" />
</list>
</property>
</bean>
<!-- 可配置一些监听器,会在本地配置更新后触发 -->
<bean id="activityNoListener" class="me.hao0.diablo.client.listener.ActivityNoListener" />
<bean id="timeInfoListener" class="me.hao0.diablo.client.listener.TimeInfoListener" />
<bean id="timeInfosListener" class="me.hao0.diablo.client.listener.TimeInfosListener" />
<bean id="timeInfoMapListener" class="me.hao0.diablo.client.listener.TimeInfoMapListener" />
```

+ 添加**diablo配置Bean**到**spring上下文中**, 如:
Expand Down
30 changes: 30 additions & 0 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,20 @@ A Lightweight Distributed Configuration Management Platform
client.setAppName("myapp");
client.setAppKey("123456");
client.setServers("127.0.0.1:2143,127.0.0.1:2144");
// You can add some config listener
client.addListener(new ConfigListener<String>() {
@Override
public String name() {
// config name
return "test_config1";
}

@Override
public void onUpdate(String newValue) {
// trigger this method after the local config updated
System.out.println("test_config1 has updated to " + newValue);
}
});
client.start();
// get the config's latest value
Expand Down Expand Up @@ -157,6 +171,22 @@ A Lightweight Distributed Configuration Management Platform
<property name="appName" value="myapp" />
<property name="appKey" value="123456" />
<property name="servers" value="127.0.0.1:2143,127.0.0.1:2144" />
<!-- optional configuration -->
<property name="listeners">
<list>
<ref bean="activityNoListener" />
<ref bean="timeInfoListener" />
<ref bean="timeInfosListener" />
<ref bean="timeInfoMapListener" />
</list>
</property>
<!-- Some config listeners -->
<bean id="activityNoListener" class="me.hao0.diablo.client.listener.ActivityNoListener" />
<bean id="timeInfoListener" class="me.hao0.diablo.client.listener.TimeInfoListener" />
<bean id="timeInfosListener" class="me.hao0.diablo.client.listener.TimeInfosListener" />
<bean id="timeInfoMapListener" class="me.hao0.diablo.client.listener.TimeInfoMapListener" />
</bean>
```

Expand Down
4 changes: 2 additions & 2 deletions diablo-client-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>diablo</artifactId>
<groupId>me.hao0</groupId>
<version>1.0.2</version>
<version>1.1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -22,7 +22,7 @@
<dependency>
<groupId>me.hao0</groupId>
<artifactId>diablo-client</artifactId>
<version>1.0.2</version>
<version>1.1.0</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
package me.hao0.diablo.client;

import com.fasterxml.jackson.databind.JavaType;
import com.google.common.base.Converter;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.common.primitives.*;
import me.hao0.common.util.Fields;
import me.hao0.diablo.client.converter.BooleanConverter;
import me.hao0.diablo.client.converter.JavaTypeConverter;
import me.hao0.diablo.client.converter.JsonConverter;
import me.hao0.diablo.common.convert.Converters;
import me.hao0.diablo.common.model.ConfigItem;
import me.hao0.diablo.common.util.CollectionUtil;
import me.hao0.diablo.common.util.JsonUtil;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -68,7 +62,7 @@ private void resolveConfigBeanItem(DiabloConfig configBean) {
// resolve fields to ConfigItemBean s
for (Field configItemField : configItemFields){
configName = configItemField.getName();
configItemBean = new ConfigItemBean(configBean, configItemField, determineConverter(configItemField));
configItemBean = new ConfigItemBean(configBean, configItemField, Converters.determine(configItemField));
configItemBeanSet = configItemBeans.get(configName);
if (configItemBeanSet == null){
configItemBeanSet = Sets.newHashSet();
Expand All @@ -78,50 +72,6 @@ private void resolveConfigBeanItem(DiabloConfig configBean) {
}
}

/**
* Determine the converter with the field type
* @param field the field
* @return the Converter
*/
private Converter<String, ?> determineConverter(Field field) {

Class fieldClass = field.getType();

if (String.class.equals(fieldClass)){
return null;
} else if (Boolean.class.equals(fieldClass) || boolean.class.equals(fieldClass)){
return BooleanConverter.INSTANCE;
} else if (Integer.class.equals(fieldClass) || int.class.equals(fieldClass)){
return Ints.stringConverter();
} else if (Long.class.equals(fieldClass) || long.class.equals(fieldClass)) {
return Longs.stringConverter();
} else if (Short.class.equals(fieldClass) || short.class.equals(fieldClass)){
return Shorts.stringConverter();
} else if (Float.class.equals(fieldClass) || float.class.equals(fieldClass)){
return Floats.stringConverter();
} else if (Double.class.equals(fieldClass) || double.class.equals(fieldClass)){
return Doubles.stringConverter();
} else if (List.class.equals(fieldClass) || Map.class.equals(fieldClass)){
// List or Map
JavaType type;
ParameterizedType parameterizedType = (ParameterizedType)field.getGenericType();
if (List.class.equals(fieldClass)){
// List
Class<?> listGenericType = (Class<?>)parameterizedType.getActualTypeArguments()[0];
type = JsonUtil.INSTANCE.createCollectionType(List.class, listGenericType);
} else {
// Map
Class<?> mapKeyType = (Class<?>)parameterizedType.getActualTypeArguments()[0];
Class<?> mapValueType = (Class<?>)parameterizedType.getActualTypeArguments()[1];
type = JsonUtil.INSTANCE.createCollectionType(Map.class, mapKeyType, mapValueType);
}
return new JavaTypeConverter(type);
}

// Common Object
return new JsonConverter(fieldClass);
}

@Override
protected void onConfigsUpdated(List<ConfigItem> updates) {
// update DiabloConfig beans' configs
Expand All @@ -135,7 +85,7 @@ protected void onConfigsUpdated(List<ConfigItem> updates) {
Fields.put(
c.bean,
c.configItem,
c.converter == null ? update.getValue() : c.converter.convert(update.getValue())
c.converter.convert(update.getValue())
);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package me.hao0.diablo.client.listener;

/**
* Author: haolin
* Email: haolin.h0@gmail.com
*/
public class ActivityNoListener implements ConfigListener<String> {

@Override
public String name() {
return "activityNo";
}

@Override
public void onUpdate(String newActivityNo) {
System.out.println("activityNo has updated to: " + newActivityNo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package me.hao0.diablo.client.listener;

import me.hao0.diablo.client.config.TimeInfo;

/**
* Author: haolin
* Email: haolin.h0@gmail.com
*/
public class TimeInfoListener implements ConfigListener<TimeInfo> {

@Override
public String name() {
return "timeInfo";
}

@Override
public void onUpdate(TimeInfo newTimeInfo) {
System.out.println("timeInfo has updated to: " + newTimeInfo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package me.hao0.diablo.client.listener;

import me.hao0.diablo.client.config.TimeInfo;
import java.util.Map;

/**
* Author: haolin
* Email: haolin.h0@gmail.com
*/
public class TimeInfoMapListener implements ConfigListener<Map<String, TimeInfo>> {

@Override
public String name() {
return "timeInfoMap";
}

@Override
public void onUpdate(Map<String, TimeInfo> newTimeInfoMap) {
System.out.println("timeInfoMap has updated to: " + newTimeInfoMap);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package me.hao0.diablo.client.listener;

import me.hao0.diablo.client.config.TimeInfo;

import java.util.List;

/**
* Author: haolin
* Email: haolin.h0@gmail.com
*/
public class TimeInfosListener implements ConfigListener<List<TimeInfo>> {

@Override
public String name() {
return "timeInfos";
}

@Override
public void onUpdate(List<TimeInfo> newTimeInfos) {
System.out.println("timeInfos has updated to: " + newTimeInfos);
}
}
14 changes: 14 additions & 0 deletions diablo-client-spring/src/test/resources/diablo-context.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@
<property name="appName" value="myapp" />
<property name="appKey" value="123456x" />
<property name="servers" value="127.0.0.1:12345,127.0.0.1:2143" />
<property name="listeners">
<list>
<ref bean="activityNoListener" />
<ref bean="timeInfoListener" />
<ref bean="timeInfosListener" />
<ref bean="timeInfoMapListener" />
</list>
</property>
</bean>

<!-- some config listeners -->
<bean id="activityNoListener" class="me.hao0.diablo.client.listener.ActivityNoListener" />
<bean id="timeInfoListener" class="me.hao0.diablo.client.listener.TimeInfoListener" />
<bean id="timeInfosListener" class="me.hao0.diablo.client.listener.TimeInfosListener" />
<bean id="timeInfoMapListener" class="me.hao0.diablo.client.listener.TimeInfoMapListener" />

</beans>
4 changes: 2 additions & 2 deletions diablo-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>diablo</artifactId>
<groupId>me.hao0</groupId>
<version>1.0.2</version>
<version>1.1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -22,7 +22,7 @@
<dependency>
<groupId>me.hao0</groupId>
<artifactId>diablo-common</artifactId>
<version>1.0.2</version>
<version>1.1.0</version>
</dependency>

<!-- use slf4j-api for logging -->
Expand Down
Loading

0 comments on commit 7e7bb42

Please sign in to comment.