Skip to content

Commit

Permalink
add ShardingThreadFactoryBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Aug 14, 2018
1 parent 6c1978c commit a2db529
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import io.shardingsphere.core.constant.SQLType;
import io.shardingsphere.core.executor.event.AbstractExecutionEvent;
import io.shardingsphere.core.executor.event.DMLExecutionEvent;
Expand All @@ -40,7 +39,6 @@
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

/**
Expand All @@ -54,28 +52,17 @@
@Slf4j
public abstract class ExecutorEngine implements AutoCloseable {

private static final String EXECUTOR_NAME_FORMAT = "Sharding-Sphere-%d";

private static final String SHUTDOWN_EXECUTOR_NAME = "Sharding-Sphere-ExecutorEngineCloser";

private static final ExecutorService SHUTDOWN_EXECUTOR = Executors.newSingleThreadExecutor(newThreadFactory(SHUTDOWN_EXECUTOR_NAME));
private static final ExecutorService SHUTDOWN_EXECUTOR = Executors.newSingleThreadExecutor(ShardingThreadFactoryBuilder.build("ExecutorEngineCloser"));

@Getter
private final ListeningExecutorService executorService;

public ExecutorEngine(final int executorSize) {
executorService = MoreExecutors.listeningDecorator(0 == executorSize ? Executors.newCachedThreadPool(newThreadFactory()) : Executors.newFixedThreadPool(executorSize, newThreadFactory()));
executorService = MoreExecutors.listeningDecorator(
0 == executorSize ? Executors.newCachedThreadPool(ShardingThreadFactoryBuilder.build()) : Executors.newFixedThreadPool(executorSize, ShardingThreadFactoryBuilder.build()));
MoreExecutors.addDelayedShutdownHook(executorService, 60, TimeUnit.SECONDS);
}

private static ThreadFactory newThreadFactory(final String nameFormat) {
return new ThreadFactoryBuilder().setDaemon(true).setNameFormat(nameFormat).build();
}

private static ThreadFactory newThreadFactory() {
return newThreadFactory(EXECUTOR_NAME_FORMAT);
}

/**
* Execute.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/

package io.shardingsphere.core.executor;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.concurrent.ThreadFactory;

/**
* Sharding thread factory builder.
*
* @author zhangliang
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ShardingThreadFactoryBuilder {

private static final String NAME_FORMAT_PREFIX = "Sharding-Sphere-";

private static final String DEFAULT_EXECUTOR_NAME_FORMAT = NAME_FORMAT_PREFIX + "%d";

/**
* Build default sharding thread factory.
*
* @return default sharding thread factory
*/
public static ThreadFactory build() {
return new ThreadFactoryBuilder().setDaemon(true).setNameFormat(DEFAULT_EXECUTOR_NAME_FORMAT).build();
}

/**
* Build sharding thread factory.
*
* @param nameFormat thread name format
* @return sharding thread factory
*/
public static ThreadFactory build(final String nameFormat) {
return new ThreadFactoryBuilder().setDaemon(true).setNameFormat(NAME_FORMAT_PREFIX + nameFormat).build();
}
}

0 comments on commit a2db529

Please sign in to comment.