Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance ExecutorUtil to create ScheduledThreadPool #29

Merged
merged 2 commits into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 3 additions & 2 deletions src/main/java/com/baidu/hugegraph/rest/RestClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

Expand All @@ -50,6 +49,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 +98,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.newScheduledThreadPool(
"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
17 changes: 17 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,33 @@

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 newFixedThreadPool(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 newScheduledThreadPool(String name) {
return newScheduledThreadPool(1, name);
}

public static ScheduledExecutorService newScheduledThreadPool(int size,
Linary marked this conversation as resolved.
Show resolved Hide resolved
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");
}