/* * The MIT License (MIT) * * Copyright © 2020 xrv * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package io.github.ehlxr.did.server; import io.github.ehlxr.did.common.Try; import io.github.ehlxr.did.generator.SnowFlake; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelOption; import io.netty.channel.DefaultEventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioServerSocketChannel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicInteger; /** * @author ehlxr */ public abstract class BaseServer implements Server { protected Logger logger = LoggerFactory.getLogger(BaseServer.class); protected DefaultEventLoopGroup defLoopGroup; protected NioEventLoopGroup bossGroup; protected NioEventLoopGroup workGroup; protected ChannelFuture channelFuture; protected ServerBootstrap serverBootstrap; protected SnowFlake snowFlake; protected void init() { defLoopGroup = new DefaultEventLoopGroup(Runtime.getRuntime().availableProcessors(), new ThreadFactory() { private final AtomicInteger index = new AtomicInteger(0); @Override public Thread newThread(Runnable r) { return new Thread(r, "DEFAULTEVENTLOOPGROUP_" + index.incrementAndGet()); } }); bossGroup = new NioEventLoopGroup(1, new ThreadFactory() { private final AtomicInteger index = new AtomicInteger(0); @Override public Thread newThread(Runnable r) { return new Thread(r, "BOSS_" + index.incrementAndGet()); } }); workGroup = new NioEventLoopGroup(new ThreadFactory() { private final AtomicInteger index = new AtomicInteger(0); @Override public Thread newThread(Runnable r) { return new Thread(r, "WORK_" + index.incrementAndGet()); } }); serverBootstrap = new ServerBootstrap() .group(bossGroup, workGroup) .channel(NioServerSocketChannel.class) // .option(ChannelOption.SO_KEEPALIVE, true) // .option(ChannelOption.TCP_NODELAY, true) // .localAddress(new InetSocketAddress(port)) .option(ChannelOption.SO_BACKLOG, 1024); } @Override public void shutdown() { Try.of(() -> { // 同步阻塞 shutdownGracefully 完成 if (defLoopGroup != null) { defLoopGroup.shutdownGracefully().sync(); } bossGroup.shutdownGracefully().sync(); workGroup.shutdownGracefully().sync(); }).trap(e -> logger.error("Server EventLoopGroup shutdown error.", e)).run(); } }