Netty-使用业务处理线程池处理业务.md
业务线程是区别与通信线程的另外一种线程的使用类别。
通信线程通常就是用来处理网络I/O相关的各种事件,例如连接操作,消息读取和发送操作。Netty中的NioEventLoop通常就是被用来处理这么一系列的操作。
业务线程通常侧重于处理业务相关的逻辑,在Netty中配置任务处理链的时候可以配置某一个处理器使用特定的处理器实现。
ChannelPipeline addLast(EventExecutorGroup group, ChannelHandler... handlers);
这个EventExecutorGroup group就是可以传入一个业务线程池,DefaultEventExecutorGroup
将业务的ChannelHandler与具体的DefaultEventExecutor绑定,就可以从该线程池中分配线程去处理该处理器中的业务。
public void start(int port) {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
// 创建一个工作线程池
DefaultEventExecutorGroup serviceHandlerGroup = new DefaultEventExecutorGroup(
RuntimeUtil.cpus() * 2,
ThreadPoolFactoryUtils.createThreadFactory("service-handler-group", false)
);
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.handler(new LoggingHandler(LogLevel.INFO))
.option(ChannelOption.SO_BACKLOG, 256)
.option(ChannelOption.SO_KEEPALIVE, true)
.childOption(ChannelOption.TCP_NODELAY, true)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new CommonEncoder(serializer));
pipeline.addLast(new CommonDecoder());
// 指定一个工作线程池,处理业务逻辑
pipeline.addLast(serviceHandlerGroup, new NettyServerHandler());
}
});
ChannelFuture future = serverBootstrap.bind(port).sync();
future.channel().closeFuture().sync();
} catch (InterruptedException e) {
logger.error("启动服务器时有错误发生: ", e);
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
当然也不一定是使用业务线程池处理业务逻辑,服务效率就会更高。**使用通信线程与业务线程并发,会在线程切换上消耗一定资源。**所以当业务逻辑较为简单时,还是可以用通信线程去处理业务逻辑的。
如果将复杂的业务逻辑放在通信线程中执行,那么就会造成长时间的线程阻塞。
下面是自定义线程池的代码:
/**
* 线程池自定义配置类,可自行根据业务场景修改配置参数。
*
* @author shuang.kou
* @createTime 2020年06月05日 17:03:00
*/
@Setter
@Getter
public class CustomThreadPoolConfig {
/**
* 线程池默认参数
*/
private static final int DEFAULT_CORE_POOL_SIZE = 10;
private static final int DEFAULT_MAXIMUM_POOL_SIZE_SIZE = 100;
private static final int DEFAULT_KEEP_ALIVE_TIME = 1;
private static final TimeUnit DEFAULT_TIME_UNIT = TimeUnit.MINUTES;
private static final int DEFAULT_BLOCKING_QUEUE_CAPACITY = 100;
private static final int BLOCKING_QUEUE_CAPACITY = 100;
/**
* 可配置参数
*/
private int corePoolSize = DEFAULT_CORE_POOL_SIZE;
private int maximumPoolSize = DEFAULT_MAXIMUM_POOL_SIZE_SIZE;
private long keepAliveTime = DEFAULT_KEEP_ALIVE_TIME;
private TimeUnit unit = DEFAULT_TIME_UNIT;
// 使用有界队列
private BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(BLOCKING_QUEUE_CAPACITY);
}
/**
* 创建 ThreadPool(线程池) 的工具类.
*
* @author shuang.kou
* @createTime 2020年05月26日 16:00:00
*/
@Slf4j
public final class ThreadPoolFactoryUtils {
/**
* 通过 threadNamePrefix 来区分不同线程池(我们可以把相同 threadNamePrefix 的线程池看作是为同一业务场景服务)。
* key: threadNamePrefix
* value: threadPool
*/
private static final Map<String, ExecutorService> THREAD_POOLS = new ConcurrentHashMap<>();
private ThreadPoolFactoryUtils() {
}
public static ExecutorService createCustomThreadPoolIfAbsent(String threadNamePrefix) {
CustomThreadPoolConfig customThreadPoolConfig = new CustomThreadPoolConfig();
return createCustomThreadPoolIfAbsent(customThreadPoolConfig, threadNamePrefix, false);
}
public static ExecutorService createCustomThreadPoolIfAbsent(String threadNamePrefix, CustomThreadPoolConfig customThreadPoolConfig) {
return createCustomThreadPoolIfAbsent(customThreadPoolConfig, threadNamePrefix, false);
}
public static ExecutorService createCustomThreadPoolIfAbsent(CustomThreadPoolConfig customThreadPoolConfig, String threadNamePrefix, Boolean daemon) {
// 存在则取出,不存新建一个
ExecutorService threadPool = THREAD_POOLS.computeIfAbsent(threadNamePrefix, k -> createThreadPool(customThreadPoolConfig, threadNamePrefix, daemon));
// 如果 threadPool 被 shutdown 的话就重新创建一个
if (threadPool.isShutdown() || threadPool.isTerminated()) {
THREAD_POOLS.remove(threadNamePrefix);
threadPool = createThreadPool(customThreadPoolConfig, threadNamePrefix, daemon);
THREAD_POOLS.put(threadNamePrefix, threadPool);
}
return threadPool;
}
/**
* shutDown 所有线程池
*/
public static void shutDownAllThreadPool() {
log.info("call shutDownAllThreadPool method");
THREAD_POOLS.entrySet().parallelStream().forEach(entry -> {
ExecutorService executorService = entry.getValue();
executorService.shutdown();
log.info("shut down thread pool [{}] [{}]", entry.getKey(), executorService.isTerminated());
try {
executorService.awaitTermination(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
log.error("Thread pool never terminated");
executorService.shutdownNow();
}
});
}
private static ExecutorService createThreadPool(CustomThreadPoolConfig customThreadPoolConfig, String threadNamePrefix, Boolean daemon) {
ThreadFactory threadFactory = createThreadFactory(threadNamePrefix, daemon);
return new ThreadPoolExecutor(customThreadPoolConfig.getCorePoolSize(), customThreadPoolConfig.getMaximumPoolSize(),
customThreadPoolConfig.getKeepAliveTime(), customThreadPoolConfig.getUnit(), customThreadPoolConfig.getWorkQueue(),
threadFactory);
}
/**
* 创建 ThreadFactory 。如果threadNamePrefix不为空则使用自建ThreadFactory,否则使用defaultThreadFactory
*
* @param threadNamePrefix 作为创建的线程名字的前缀
* @param daemon 指定是否为 Daemon Thread(守护线程)
* @return ThreadFactory
*/
public static ThreadFactory createThreadFactory(String threadNamePrefix, Boolean daemon) {
if (threadNamePrefix != null) {
if (daemon != null) {
return new ThreadFactoryBuilder()
.setNameFormat(threadNamePrefix + "-%d")
.setDaemon(daemon).build();
} else {
return new ThreadFactoryBuilder().setNameFormat(threadNamePrefix + "-%d").build();
}
}
return Executors.defaultThreadFactory();
}
/**
* 打印线程池的状态
*
* @param threadPool 线程池对象
*/
public static void printThreadPoolStatus(ThreadPoolExecutor threadPool) {
// 创建一个线程执行定时任务
ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(1, createThreadFactory("print-thread-pool-status", false));
scheduledExecutorService.scheduleAtFixedRate(() -> {
log.info("============ThreadPool Status=============");
log.info("ThreadPool Size: [{}]", threadPool.getPoolSize());
log.info("Active Threads: [{}]", threadPool.getActiveCount());
log.info("Number of Tasks : [{}]", threadPool.getCompletedTaskCount());
log.info("Number of Tasks in Queue: {}", threadPool.getQueue().size());
log.info("===========================================");
}, 0, 1, TimeUnit.SECONDS);
}
}