did/did-server/src/main/java/io/github/ehlxr/did/server/BaseServer.java

101 lines
3.9 KiB
Java
Raw Normal View History

2021-02-13 03:56:06 +00:00
/*
* The MIT License (MIT)
*
* Copyright © 2020 xrv <xrg@live.com>
*
* 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.
*/
2021-01-22 07:40:02 +00:00
package io.github.ehlxr.did.server;
2018-08-14 07:21:56 +00:00
2021-02-08 09:15:36 +00:00
import io.github.ehlxr.did.common.Try;
2021-03-04 03:24:45 +00:00
import io.github.ehlxr.did.generator.SnowFlake;
2018-08-14 07:21:56 +00:00
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
2021-01-27 03:47:09 +00:00
import io.netty.channel.ChannelOption;
2018-08-14 07:21:56 +00:00
import io.netty.channel.DefaultEventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
2021-01-27 03:47:09 +00:00
import io.netty.channel.socket.nio.NioServerSocketChannel;
2018-08-14 07:21:56 +00:00
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 {
2021-01-19 10:50:50 +00:00
protected Logger logger = LoggerFactory.getLogger(BaseServer.class);
2018-08-14 07:21:56 +00:00
protected DefaultEventLoopGroup defLoopGroup;
protected NioEventLoopGroup bossGroup;
protected NioEventLoopGroup workGroup;
protected ChannelFuture channelFuture;
protected ServerBootstrap serverBootstrap;
2021-01-20 07:25:58 +00:00
protected SnowFlake snowFlake;
2018-08-14 07:21:56 +00:00
2021-01-27 03:47:09 +00:00
protected void init() {
2021-01-19 12:28:04 +00:00
defLoopGroup = new DefaultEventLoopGroup(Runtime.getRuntime().availableProcessors(), new ThreadFactory() {
2021-01-18 08:17:14 +00:00
private final AtomicInteger index = new AtomicInteger(0);
2018-08-14 07:21:56 +00:00
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "DEFAULTEVENTLOOPGROUP_" + index.incrementAndGet());
}
});
2021-01-19 12:28:04 +00:00
bossGroup = new NioEventLoopGroup(1, new ThreadFactory() {
2021-01-18 08:17:14 +00:00
private final AtomicInteger index = new AtomicInteger(0);
2018-08-14 07:21:56 +00:00
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "BOSS_" + index.incrementAndGet());
}
});
2021-01-19 12:28:04 +00:00
workGroup = new NioEventLoopGroup(new ThreadFactory() {
2021-01-18 08:17:14 +00:00
private final AtomicInteger index = new AtomicInteger(0);
2018-08-14 07:21:56 +00:00
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "WORK_" + index.incrementAndGet());
}
});
2021-02-08 09:15:36 +00:00
serverBootstrap = new ServerBootstrap()
.group(bossGroup, workGroup)
2021-01-27 03:47:09 +00:00
.channel(NioServerSocketChannel.class)
// .option(ChannelOption.SO_KEEPALIVE, true)
// .option(ChannelOption.TCP_NODELAY, true)
// .localAddress(new InetSocketAddress(port))
.option(ChannelOption.SO_BACKLOG, 1024);
2018-08-14 07:21:56 +00:00
}
@Override
public void shutdown() {
2021-02-08 09:15:36 +00:00
Try.of(() -> {
2021-01-19 12:28:04 +00:00
// 同步阻塞 shutdownGracefully 完成
if (defLoopGroup != null) {
defLoopGroup.shutdownGracefully().sync();
}
bossGroup.shutdownGracefully().sync();
workGroup.shutdownGracefully().sync();
2021-02-08 09:15:36 +00:00
}).trap(e -> logger.error("Server EventLoopGroup shutdown error.", e)).run();
2018-08-14 07:21:56 +00:00
}
}