From b6381e0f2c65d8b0f3310c9ed66ebf29f511dbcd Mon Sep 17 00:00:00 2001 From: liningrui Date: Sat, 22 Jun 2019 20:06:36 +0800 Subject: [PATCH] Enhance ExecutorUtil to create ScheduledThreadPool --- pom.xml | 4 ++-- .../com/baidu/hugegraph/rest/RestClient.java | 4 +++- .../com/baidu/hugegraph/rest/RestResult.java | 2 +- .../com/baidu/hugegraph/util/ExecutorUtil.java | 18 ++++++++++++++++++ .../baidu/hugegraph/version/CommonVersion.java | 2 +- 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 13b0cd76d..7f97011a3 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.baidu.hugegraph hugegraph-common - 1.6.5 + 1.6.6 hugegraph-common https://github.com/hugegraph/hugegraph-common @@ -212,7 +212,7 @@ - 1.6.5.0 + 1.6.6.0 diff --git a/src/main/java/com/baidu/hugegraph/rest/RestClient.java b/src/main/java/com/baidu/hugegraph/rest/RestClient.java index 63871e021..a6d815889 100644 --- a/src/main/java/com/baidu/hugegraph/rest/RestClient.java +++ b/src/main/java/com/baidu/hugegraph/rest/RestClient.java @@ -50,6 +50,7 @@ import org.glassfish.jersey.message.GZipEncoder; import org.glassfish.jersey.uri.UriComponent; +import com.baidu.hugegraph.util.ExecutorUtil; import com.google.common.collect.ImmutableMap; public abstract class RestClient { @@ -98,7 +99,8 @@ public RestClient(String url, ClientConfig config) { this.pool = (PoolingHttpClientConnectionManager) config.getProperty(CONNECTION_MANAGER); if (this.pool != null) { - this.cleanExecutor = Executors.newScheduledThreadPool(1); + this.cleanExecutor = ExecutorUtil.newSingleScheduledThreadPool( + "conn-clean-worker-%d"); this.cleanExecutor.scheduleWithFixedDelay(() -> { PoolStats stats = this.pool.getTotalStats(); int using = stats.getLeased() + stats.getPending(); diff --git a/src/main/java/com/baidu/hugegraph/rest/RestResult.java b/src/main/java/com/baidu/hugegraph/rest/RestResult.java index 92962eacd..e4908f8b3 100644 --- a/src/main/java/com/baidu/hugegraph/rest/RestResult.java +++ b/src/main/java/com/baidu/hugegraph/rest/RestResult.java @@ -77,7 +77,7 @@ public List readList(String key, Class clazz) { JavaType type = mapper.getTypeFactory() .constructParametrizedType(ArrayList.class, List.class, clazz); - return mapper.readValue(element.toString(), type); + return mapper.convertValue(element, type); } catch (IOException e) { throw new SerializeException( "Failed to deserialize %s", e, this.content); diff --git a/src/main/java/com/baidu/hugegraph/util/ExecutorUtil.java b/src/main/java/com/baidu/hugegraph/util/ExecutorUtil.java index 773ec0d8d..465ec95e3 100644 --- a/src/main/java/com/baidu/hugegraph/util/ExecutorUtil.java +++ b/src/main/java/com/baidu/hugegraph/util/ExecutorUtil.java @@ -21,16 +21,34 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ThreadFactory; import org.apache.commons.lang3.concurrent.BasicThreadFactory; public final class ExecutorUtil { + public static ExecutorService newSingleFixedThreadPool(String name) { + return newFixedThreadPool(1, name); + } + public static ExecutorService newFixedThreadPool(int size, String name) { ThreadFactory factory = new BasicThreadFactory.Builder() .namingPattern(name) .build(); return Executors.newFixedThreadPool(size, factory); } + + public static ScheduledExecutorService newSingleScheduledThreadPool( + String name) { + return newScheduledThreadPool(1, name); + } + + public static ScheduledExecutorService newScheduledThreadPool(int size, + String name) { + ThreadFactory factory = new BasicThreadFactory.Builder() + .namingPattern(name) + .build(); + return Executors.newScheduledThreadPool(size, factory); + } } diff --git a/src/main/java/com/baidu/hugegraph/version/CommonVersion.java b/src/main/java/com/baidu/hugegraph/version/CommonVersion.java index 55585ad67..da9b44c59 100644 --- a/src/main/java/com/baidu/hugegraph/version/CommonVersion.java +++ b/src/main/java/com/baidu/hugegraph/version/CommonVersion.java @@ -27,5 +27,5 @@ public class CommonVersion { // The second parameter of Version.of() is for all-in-one JAR public static final Version VERSION = Version.of(CommonVersion.class, - "1.6.5"); + "1.6.6"); }