Skip to content

Commit

Permalink
Merge pull request #141 from nobodyiam/client-refactor-service-integr…
Browse files Browse the repository at this point in the history
…ation

Client refactor, Config service integration test.
  • Loading branch information
yiming187 committed Apr 25, 2016
2 parents f633293 + d5a3d39 commit 3d0186e
Show file tree
Hide file tree
Showing 39 changed files with 574 additions and 211 deletions.
75 changes: 75 additions & 0 deletions apollo-client/src/main/java/com/ctrip/apollo/Config.java
Expand Up @@ -13,6 +13,81 @@ public interface Config {
*/
public String getProperty(String key, String defaultValue);

/**
* Return the integer property value with the given key, or
* {@code defaultValue} if the key doesn't exist.
* @param key the property name
* @param defaultValue the default value is key is not found
* @return the property value as integer
*
* @throws NumberFormatException if the property value is invalid
*/
public Integer getIntProperty(String key, Integer defaultValue);

/**
* Return the long property value with the given key, or
* {@code defaultValue} if the key doesn't exist.
* @param key the property name
* @param defaultValue the default value is key is not found
* @return the property value as long
*
* @throws NumberFormatException if the property value is invalid
*/
public Long getLongProperty(String key, Long defaultValue);

/**
* Return the short property value with the given key, or
* {@code defaultValue} if the key doesn't exist.
* @param key the property name
* @param defaultValue the default value is key is not found
* @return the property value as short
*
* @throws NumberFormatException if the property value is invalid
*/
public Short getShortProperty(String key, Short defaultValue);

/**
* Return the float property value with the given key, or
* {@code defaultValue} if the key doesn't exist.
* @param key the property name
* @param defaultValue the default value is key is not found
* @return the property value as float
*
* @throws NumberFormatException if the property value is invalid
*/
public Float getFloatProperty(String key, Float defaultValue);

/**
* Return the double property value with the given key, or
* {@code defaultValue} if the key doesn't exist.
* @param key the property name
* @param defaultValue the default value is key is not found
* @return the property value as double
*
* @throws NumberFormatException if the property value is invalid
*/
public Double getDoubleProperty(String key, Double defaultValue);

/**
* Return the byte property value with the given key, or
* {@code defaultValue} if the key doesn't exist.
* @param key the property name
* @param defaultValue the default value is key is not found
* @return the property value as byte
*
* @throws NumberFormatException if the property value is invalid
*/
public Byte getByteProperty(String key, Byte defaultValue);

/**
* Return the boolean property value with the given key, or
* {@code defaultValue} if the key doesn't exist.
* @param key the property name
* @param defaultValue the default value is key is not found
* @return the property value as boolean
*/
public Boolean getBooleanProperty(String key, Boolean defaultValue);

/**
* Add change listener to this config instance.
* @param listener the config change listener
Expand Down
30 changes: 11 additions & 19 deletions apollo-client/src/main/java/com/ctrip/apollo/ConfigService.java
@@ -1,5 +1,6 @@
package com.ctrip.apollo;

import com.ctrip.apollo.core.ConfigConsts;
import com.ctrip.apollo.internals.ConfigManager;
import com.ctrip.apollo.spi.ConfigFactory;
import com.ctrip.apollo.spi.ConfigRegistry;
Expand All @@ -24,11 +25,11 @@ private ConfigService() {
}

/**
* Get the config instance with default namespace.
* Get Application's config instance.
* @return config instance
*/
public static Config getConfig() {
return getConfig(getDefaultNamespace());
public static Config getAppConfig() {
return getConfig(ConfigConsts.NAMESPACE_DEFAULT);
}

/**
Expand Down Expand Up @@ -59,25 +60,16 @@ private static ConfigRegistry getRegistry() {
}
}

private static String getDefaultNamespace() {
try {
return s_instance.m_container.lookup(ConfigUtil.class).getAppId();
} catch (ComponentLookupException ex) {
Cat.logError(ex);
throw new IllegalStateException("Unable to load ConfigUtil!", ex);
}
}

public static void setConfig(Config config) {
setConfig(getDefaultNamespace(), config);
static void setConfig(Config config) {
setConfig(ConfigConsts.NAMESPACE_DEFAULT, config);
}

/**
* Manually set the config for the namespace specified, use with caution.
* @param namespace the namespace
* @param config the config instance
*/
public static void setConfig(String namespace, final Config config) {
static void setConfig(String namespace, final Config config) {
getRegistry().register(namespace, new ConfigFactory() {
@Override
public Config create(String namespace) {
Expand All @@ -86,21 +78,21 @@ public Config create(String namespace) {
});
}

public static void setConfigFactory(ConfigFactory factory) {
setConfigFactory(getDefaultNamespace(), factory);
static void setConfigFactory(ConfigFactory factory) {
setConfigFactory(ConfigConsts.NAMESPACE_DEFAULT, factory);
}

/**
* Manually set the config factory for the namespace specified, use with caution.
* @param namespace the namespace
* @param factory the factory instance
*/
public static void setConfigFactory(String namespace, ConfigFactory factory) {
static void setConfigFactory(String namespace, ConfigFactory factory) {
getRegistry().register(namespace, factory);
}

// for test only
public static void setContainer(PlexusContainer m_container) {
static void setContainer(PlexusContainer m_container) {
s_instance.m_container = m_container;
}
}
Expand Up @@ -5,5 +5,5 @@
* @author Jason Song(song_s@ctrip.com)
*/
public enum PropertyChangeType {
NEW, MODIFIED, DELETED
ADDED, MODIFIED, DELETED
}

This file was deleted.

Expand Up @@ -32,6 +32,48 @@ public void addChangeListener(ConfigChangeListener listener) {
}
}

@Override
public Integer getIntProperty(String key, Integer defaultValue) {
String value = getProperty(key, null);
return value == null ? defaultValue : Integer.parseInt(value);
}

@Override
public Long getLongProperty(String key, Long defaultValue) {
String value = getProperty(key, null);
return value == null ? defaultValue : Long.parseLong(value);
}

@Override
public Short getShortProperty(String key, Short defaultValue) {
String value = getProperty(key, null);
return value == null ? defaultValue : Short.parseShort(value);
}

@Override
public Float getFloatProperty(String key, Float defaultValue) {
String value = getProperty(key, null);
return value == null ? defaultValue : Float.parseFloat(value);
}

@Override
public Double getDoubleProperty(String key, Double defaultValue) {
String value = getProperty(key, null);
return value == null ? defaultValue : Double.parseDouble(value);
}

@Override
public Byte getByteProperty(String key, Byte defaultValue) {
String value = getProperty(key, null);
return value == null ? defaultValue : Byte.parseByte(value);
}

@Override
public Boolean getBooleanProperty(String key, Boolean defaultValue) {
String value = getProperty(key, null);
return value == null ? defaultValue : Boolean.parseBoolean(value);
}

protected void fireConfigChange(ConfigChangeEvent changeEvent) {
for (ConfigChangeListener listener : m_listeners) {
try {
Expand All @@ -43,7 +85,8 @@ protected void fireConfigChange(ConfigChangeEvent changeEvent) {
}
}

List<ConfigChange> calcPropertyChanges(Properties previous,
List<ConfigChange> calcPropertyChanges(String namespace,
Properties previous,
Properties current) {
if (previous == null) {
previous = new Properties();
Expand All @@ -64,11 +107,12 @@ List<ConfigChange> calcPropertyChanges(Properties previous,

for (String newKey : newKeys) {
changes
.add(new ConfigChange(newKey, null, current.getProperty(newKey), PropertyChangeType.NEW));
.add(new ConfigChange(namespace, newKey, null, current.getProperty(newKey),
PropertyChangeType.ADDED));
}

for (String removedKey : removedKeys) {
changes.add(new ConfigChange(removedKey, previous.getProperty(removedKey), null,
changes.add(new ConfigChange(namespace, removedKey, previous.getProperty(removedKey), null,
PropertyChangeType.DELETED));
}

Expand All @@ -78,7 +122,7 @@ List<ConfigChange> calcPropertyChanges(Properties previous,
if (Objects.equal(previousValue, currentValue)) {
continue;
}
changes.add(new ConfigChange(commonKey, previousValue,
changes.add(new ConfigChange(namespace, commonKey, previousValue,
currentValue, PropertyChangeType.MODIFIED));
}

Expand Down
Expand Up @@ -18,15 +18,17 @@ public abstract class AbstractConfigRepository implements ConfigRepository {
private static final Logger logger = LoggerFactory.getLogger(AbstractConfigRepository.class);
private List<RepositoryChangeListener> m_listeners = Lists.newCopyOnWriteArrayList();

protected void trySync() {
protected boolean trySync() {
try {
sync();
return true;
} catch (Throwable ex) {
Cat.logError(ex);
logger
.warn("Sync config failed with repository {}, reason: {}", this.getClass(), ExceptionUtil
.getDetailMessage(ex));
}
return false;
}

protected abstract void sync();
Expand Down
Expand Up @@ -28,7 +28,7 @@
import java.util.concurrent.atomic.AtomicReference;

@Named(type = ConfigServiceLocator.class)
public class ConfigServiceLocator implements Initializable{
public class ConfigServiceLocator implements Initializable {
private static final Logger logger = LoggerFactory.getLogger(ConfigServiceLocator.class);
@Inject
private HttpUtil m_httpUtil;
Expand Down Expand Up @@ -69,12 +69,14 @@ public List<ServiceDTO> getConfigServices() {
return m_configServices.get();
}

private void tryUpdateConfigServices() {
private boolean tryUpdateConfigServices() {
try {
updateConfigServices();
return true;
} catch (Throwable ex) {
//ignore
}
return false;
}

private void schedulePeriodicRefresh() {
Expand All @@ -84,8 +86,9 @@ private void schedulePeriodicRefresh() {
public void run() {
logger.debug("refresh config services");
Transaction transaction = Cat.newTransaction("Apollo.MetaService", "periodicRefresh");
tryUpdateConfigServices();
transaction.setStatus(Message.SUCCESS);
boolean syncResult = tryUpdateConfigServices();
String status = syncResult ? Message.SUCCESS : "-1";
transaction.setStatus(status);
transaction.complete();
}
}, m_configUtil.getRefreshInterval(), m_configUtil.getRefreshInterval(),
Expand Down Expand Up @@ -129,8 +132,8 @@ private synchronized void updateConfigServices() {
}

private void logConfigServicesToCat(List<ServiceDTO> serviceDtos) {
for (ServiceDTO serviceDTO : serviceDtos) {
Cat.logEvent("Apollo.Config.Services", serviceDTO.getHomepageUrl());
for (ServiceDTO serviceDto : serviceDtos) {
Cat.logEvent("Apollo.Config.Services", serviceDto.getHomepageUrl());
}
}
}

0 comments on commit 3d0186e

Please sign in to comment.