You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2018/08/09 00:59:35 UTC

[incubator-servicecomb-java-chassis] 05/05: make ParseConfigUtils singleton

This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git

commit 424bd022e03ba06fcbd9eb90ae8bb3a71f6ecf86
Author: heyile <he...@huawei.com>
AuthorDate: Tue Aug 7 19:46:11 2018 +0800

    make ParseConfigUtils singleton
---
 .../config/client/ConfigCenterClient.java          |  6 ++--
 .../config/client/ParseConfigUtils.java            | 38 ++++++++++++++++------
 2 files changed, 31 insertions(+), 13 deletions(-)

diff --git a/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ConfigCenterClient.java b/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ConfigCenterClient.java
index e7aa18f..09d26c4 100644
--- a/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ConfigCenterClient.java
+++ b/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ConfigCenterClient.java
@@ -128,14 +128,14 @@ public class ConfigCenterClient {
       LOGGER.error("refreshMode must be 0 or 1.");
       return;
     }
-    ParseConfigUtils parseConfigUtils = new ParseConfigUtils(updateHandler);
+    ParseConfigUtils.getInstance().initWithUpdateHandler(updateHandler);
     try {
       deployConfigClient();
     } catch (InterruptedException e) {
       throw new IllegalStateException(e);
     }
     refreshMembers(memberDiscovery);
-    ConfigRefresh refreshTask = new ConfigRefresh(parseConfigUtils, memberDiscovery);
+    ConfigRefresh refreshTask = new ConfigRefresh(ParseConfigUtils.getInstance(), memberDiscovery);
     refreshTask.run(true);
     executor.scheduleWithFixedDelay(refreshTask,
         firstRefreshInterval,
@@ -364,7 +364,7 @@ public class ConfigCenterClient {
         encodeServiceName = StringUtils.deleteWhitespace(serviceName);
       }
       String path = uriConst.ITEMS + "?dimensionsInfo=" + encodeServiceName + "&revision="
-          + ParseConfigUtils.getCurrentVersionInfo();
+          + ParseConfigUtils.getInstance().getCurrentVersionInfo();
       clientMgr.findThreadBindClientPool().runOnContext(client -> {
         IpPort ipPort = NetUtils.parseIpPortFromURI(configcenter);
         HttpClientRequest request = client.get(ipPort.getPort(), ipPort.getHostOrIp(), path, rsp -> {
diff --git a/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ParseConfigUtils.java b/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ParseConfigUtils.java
index 79366dc..3dd95c8 100644
--- a/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ParseConfigUtils.java
+++ b/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/client/ParseConfigUtils.java
@@ -38,25 +38,39 @@ public class ParseConfigUtils {
 
   private static final Logger LOGGER = LoggerFactory.getLogger(ParseConfigUtils.class);
 
-  private static LinkedHashMap<String, Map<String, Object>> multiDimensionItems = new LinkedHashMap<>();
-  //it's dangerous that makes flatItems public
-  private static final Map<String, Object> flatItems = new HashMap<>();
+  private static final ParseConfigUtils INSTANCE = new ParseConfigUtils();
 
-  private static String currentVersionInfo = "default";
+  private LinkedHashMap<String, Map<String, Object>> multiDimensionItems = new LinkedHashMap<>();
+
+  //it's dangerous to make flatItems public
+  private final Map<String, Object> flatItems = new HashMap<>();
+
+  private String currentVersionInfo = "default";
 
   private UpdateHandler updateHandler;
 
   private Lock configLock = new ReentrantLock();
 
+  //for compatibility with other modules and JunitTest
   public ParseConfigUtils(UpdateHandler updateHandler) {
     this.updateHandler = updateHandler;
   }
 
+  private ParseConfigUtils() {
+  }
+
+  public void initWithUpdateHandler(UpdateHandler updateHandler) {
+    if (updateHandler == null) {
+      LOGGER.error("when init ParseConfigUtils, updateHandler can not be null");
+    }
+    this.updateHandler = updateHandler;
+  }
+
   /*
-    as the data is returned, we can block the thread at a short time. consider that if the multiple verticle is deployed
-    and if we use pull mode and push mode at the same time , we must share a common lock with all methods which would
-    change the config setting
-   */
+      as the data is returned, we can block the thread at a short time. consider that if the multiple verticle is deployed
+      and if we use pull mode and push mode at the same time , we must share a common lock with all methods which would
+      change the config setting
+     */
   public void refreshConfigItems(Map<String, Map<String, Object>> remoteItems) {
     try {
       configLock.lock();
@@ -74,8 +88,12 @@ public class ParseConfigUtils {
     }
   }
 
-  public static String getCurrentVersionInfo() {
-    return currentVersionInfo;
+  public static ParseConfigUtils getInstance() {
+    return INSTANCE;
+  }
+
+  public String getCurrentVersionInfo() {
+    return this.currentVersionInfo;
   }
 
   public void refreshConfigItemsIncremental(Map<String, Object> action) {