Skip to content

Commit

Permalink
Enhance ExecutorUtil to create ScheduledThreadPool
Browse files Browse the repository at this point in the history
  • Loading branch information
Linary committed Jun 22, 2019
1 parent 2f453c0 commit b6381e0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.baidu.hugegraph</groupId>
<artifactId>hugegraph-common</artifactId>
<version>1.6.5</version>
<version>1.6.6</version>

<name>hugegraph-common</name>
<url>https://github.com/hugegraph/hugegraph-common</url>
Expand Down Expand Up @@ -212,7 +212,7 @@
<manifestEntries>
<!-- Must be on one line, otherwise the automatic
upgrade script cannot replace the version number -->
<Implementation-Version>1.6.5.0</Implementation-Version>
<Implementation-Version>1.6.6.0</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/baidu/hugegraph/rest/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/baidu/hugegraph/rest/RestResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public <T> List<T> readList(String key, Class<T> 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);
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/baidu/hugegraph/util/ExecutorUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

0 comments on commit b6381e0

Please sign in to comment.