
io.jsync.impl.AsyncExecutorFactory Maven / Gradle / Ivy
Show all versions of jsync.io Show documentation
/*
* Copyright (c) 2011-2013 The original author or authors
* ------------------------------------------------------
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*/
package io.jsync.impl;
import io.jsync.impl.management.ManagementRegistry;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Util factory for creating jsync.io thread pools
*
* The pools shouldn't be too configurable by the user otherwise they
* can get into problems. jsync.io requires quite specific behaviour from each pool
* and things can easily break if they are configured incorrectly.
*
* @author swilliams
* @author Tim Fox
*/
public class AsyncExecutorFactory {
public static final int WORKER_POOL_MAX_SIZE = 20;
// The worker pool needs to be fixed with a backing queue
public static ExecutorService workerPool(String poolName) {
int maxSize = Integer.getInteger("async.pool.worker.size", WORKER_POOL_MAX_SIZE);
ExecutorService exec = Executors.newFixedThreadPool(maxSize, new AsyncThreadFactory(poolName));
ManagementRegistry.registerThreadPool("Worker", exec);
return exec;
}
// The acceptor pools need to be fixed with a backing queue
public static EventLoopGroup eventLoopGroup(String poolName) {
return new NioEventLoopGroup(eventLoopSize(), new AsyncThreadFactory(poolName));
}
public static int eventLoopSize() {
return Integer.getInteger("async.pool.eventloop.size", 2 * Runtime.getRuntime().availableProcessors());
}
}