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 2020/12/16 08:24:27 UTC

[servicecomb-java-chassis] branch master updated: [SCB-2163] tiny code improve (#2138)

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/servicecomb-java-chassis.git


The following commit(s) were added to refs/heads/master by this push:
     new 4d613ee  [SCB-2163] tiny code improve (#2138)
4d613ee is described below

commit 4d613ee7e142116531b9bec31ae15c364980d01d
Author: wujimin <wu...@huawei.com>
AuthorDate: Wed Dec 16 16:24:14 2020 +0800

    [SCB-2163] tiny code improve (#2138)
---
 .../foundation/vertx/client/http/HttpClients.java  | 50 ++++++++++++----------
 1 file changed, 28 insertions(+), 22 deletions(-)

diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/client/http/HttpClients.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/client/http/HttpClients.java
index 74c3593..1f62e4a 100644
--- a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/client/http/HttpClients.java
+++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/client/http/HttpClients.java
@@ -36,6 +36,7 @@ import io.vertx.core.Context;
 import io.vertx.core.DeploymentOptions;
 import io.vertx.core.Vertx;
 import io.vertx.core.VertxOptions;
+import io.vertx.core.dns.AddressResolverOptions;
 
 /**
  *  load and manages a set of HttpClient at boot up.
@@ -65,7 +66,7 @@ public class HttpClients {
   }
 
   /* used for configurations module: these module must be boot before other HttpClients is initialized. so can
-  *  not load by SPI, must add manually  */
+   * not load by SPI, must add manually  */
   public static void addNewClientPoolManager(HttpClientOptionsSPI option) {
     httpClients.put(option.clientName(), createClientPoolManager(option));
   }
@@ -80,20 +81,7 @@ public class HttpClients {
   }
 
   private static ClientPoolManager<HttpClientWithContext> createClientPoolManager(HttpClientOptionsSPI option) {
-    Vertx vertx;
-
-    if (option.useSharedVertx()) {
-      vertx = SharedVertxFactory.getSharedVertx();
-    } else {
-      VertxOptions vertxOptions = new VertxOptions()
-          .setAddressResolverOptions(AddressResolverConfig
-              .getAddressResover(option.getConfigTag(), option.getConfigReader()))
-          .setEventLoopPoolSize(option.getEventLoopPoolSize());
-
-      // Maybe we can deploy only one vert.x for the application. However this has did it like this.
-      vertx = VertxUtils.getOrCreateVertxByName(option.clientName(), vertxOptions);
-    }
-
+    Vertx vertx = getOrCreateVertx(option);
     ClientPoolManager<HttpClientWithContext> clientPoolManager = new ClientPoolManager<>(vertx,
         new HttpClientPoolFactory(HttpClientOptionsSPI.createHttpClientOptions(option)));
 
@@ -104,10 +92,25 @@ public class HttpClients {
         .setWorkerPoolSize(option.getWorkerPoolSize());
     try {
       VertxUtils.blockDeploy(vertx, ClientVerticle.class, deployOptions);
+      return clientPoolManager;
     } catch (InterruptedException e) {
       throw new IllegalStateException(e);
     }
-    return clientPoolManager;
+  }
+
+  private static Vertx getOrCreateVertx(HttpClientOptionsSPI option) {
+    if (option.useSharedVertx()) {
+      return SharedVertxFactory.getSharedVertx();
+    }
+
+    AddressResolverOptions resolverOptions = AddressResolverConfig
+        .getAddressResover(option.getConfigTag(), option.getConfigReader());
+    VertxOptions vertxOptions = new VertxOptions()
+        .setAddressResolverOptions(resolverOptions)
+        .setEventLoopPoolSize(option.getEventLoopPoolSize());
+
+    // Maybe we can deploy only one vert.x for the application. However this has did it like this.
+    return VertxUtils.getOrCreateVertxByName(option.clientName(), vertxOptions);
   }
 
   /**
@@ -116,11 +119,12 @@ public class HttpClients {
    * @return the deployed instance name
    */
   public static HttpClientWithContext getClient(String clientName) {
-    if (httpClients.get(clientName) == null) {
+    ClientPoolManager<HttpClientWithContext> poolManager = httpClients.get(clientName);
+    if (poolManager == null) {
       LOGGER.error("client name [{}] not exists, should only happen in tests.", clientName);
       return null;
     }
-    return httpClients.get(clientName).findThreadBindClientPool();
+    return poolManager.findThreadBindClientPool();
   }
 
   /**
@@ -130,11 +134,12 @@ public class HttpClients {
    * @return the deployed instance name
    */
   public static HttpClientWithContext getClient(String clientName, boolean sync) {
-    if (httpClients.get(clientName) == null) {
+    ClientPoolManager<HttpClientWithContext> poolManager = httpClients.get(clientName);
+    if (poolManager == null) {
       LOGGER.error("client name [{}] not exists, should only happen in tests.", clientName);
       return null;
     }
-    return httpClients.get(clientName).findClientPool(sync);
+    return poolManager.findClientPool(sync);
   }
 
   /**
@@ -145,10 +150,11 @@ public class HttpClients {
    * @return the deployed instance name
    */
   public static HttpClientWithContext getClient(String clientName, boolean sync, Context targetContext) {
-    if (httpClients.get(clientName) == null) {
+    ClientPoolManager<HttpClientWithContext> poolManager = httpClients.get(clientName);
+    if (poolManager == null) {
       LOGGER.error("client name [{}] not exists, should only happen in tests.", clientName);
       return null;
     }
-    return httpClients.get(clientName).findClientPool(sync, targetContext);
+    return poolManager.findClientPool(sync, targetContext);
   }
 }