本图来自https://shardingsphere.apache.org/elasticjob/current/cn/dev-manual/thread-pool/
JobExecutorServiceHandler
public interface JobExecutorServiceHandler extends TypedSPI {
/**
* Create executor service.
*
* @param jobName job name
*
* @return executor service
*/
ExecutorService createExecutorService(String jobName);
}
实现图
ElasticJobExecutorService
其实就是1个ThreadPoolExecutor
public final class ElasticJobExecutorService {
private final ThreadPoolExecutor threadPoolExecutor;
private final BlockingQueue<Runnable> workQueue;
public ElasticJobExecutorService(final String namingPattern, final int threadSize) {
workQueue = new LinkedBlockingQueue<>();
threadPoolExecutor = new ThreadPoolExecutor(
threadSize, threadSize, 5L, TimeUnit.MINUTES, workQueue, new BasicThreadFactory.Builder().namingPattern(String.join("-", namingPattern, "%s")).build());
threadPoolExecutor.allowCoreThreadTimeOut(true);
}
}
AbstractJobExecutorServiceHandler
子类要实现getPoolSize()方法,这个方法返回值就是java线程池的corePoolSize、maximumPoolSize
/**
* Abstract job executor service handler.
**/
public abstract class AbstractJobExecutorServiceHandler implements JobExecutorServiceHandler {
@Override
public ExecutorService createExecutorService(final String jobName) {
return new ElasticJobExecutorService("elasticjob-" + jobName, getPoolSize()).createExecutorService();
}
protected abstract int getPoolSize();
}
SingleThreadJobExecutorServiceHandler
corePoolSize=1, maximumPoolSize=1
public final class SingleThreadJobExecutorServiceHandler extends AbstractJobExecutorServiceHandler {
@Override
protected int getPoolSize() {
return 1; // threadSize= corePoolSize=1, maximumPoolSize=1
}
@Override
public String getType() {
return "SINGLE_THREAD";
}
}
CPUUsageJobExecutorServiceHandler
corePoolSize = Runtime.getRuntime().availableProcessors() * 2
maximumPoolSize = corePoolSize
/**
* Job executor service handler with use CPU available processors.
*/
public final class CPUUsageJobExecutorServiceHandler extends AbstractJobExecutorServiceHandler {
@Override
protected int getPoolSize() {
return Runtime.getRuntime().availableProcessors() * 2;
}
@Override
public String getType() {
return "CPU";
}
}
JobExecutorServiceHandlerFactory
public final class JobExecutorServiceHandlerFactory {
private static final Map<String, JobExecutorServiceHandler> HANDLERS = new LinkedHashMap<>();
private static final String DEFAULT_HANDLER = "CPU";
static {
for (JobExecutorServiceHandler each : ServiceLoader.load(JobExecutorServiceHandler.class)) {
HANDLERS.put(each.getType(), each);
}
}
/**
* Get job executor service handler.
*
* @param type executor service handler type
* @return executor service handler
*/
public static JobExecutorServiceHandler getHandler(final String type) {
if (Strings.isNullOrEmpty(type)) {
return HANDLERS.get(DEFAULT_HANDLER);
}
if (!HANDLERS.containsKey(type)) {
throw new JobConfigurationException("Can not find executor service handler type '%s'.", type);
}
return HANDLERS.get(type);
}
}
spi
org.apache.shardingsphere.elasticjob.infra.handler.threadpool.JobExecutorServiceHandler
org.apache.shardingsphere.elasticjob.infra.handler.threadpool.impl.CPUUsageJobExecutorServiceHandler
org.apache.shardingsphere.elasticjob.infra.handler.threadpool.impl.SingleThreadJobExecutorServiceHandler