|
|
|
@ -32,11 +32,12 @@ import java.util.concurrent.Executors;
|
|
|
|
|
import java.util.concurrent.ThreadFactory; |
|
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
import java.util.concurrent.atomic.AtomicBoolean; |
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger; |
|
|
|
|
|
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
|
|
import com.google.common.util.concurrent.ThreadFactoryBuilder; |
|
|
|
|
|
|
|
|
|
import io.netty.bootstrap.ServerBootstrap; |
|
|
|
|
import io.netty.channel.ChannelFuture; |
|
|
|
|
import io.netty.channel.ChannelInitializer; |
|
|
|
@ -59,11 +60,6 @@ public class NettyRemotingServer {
|
|
|
|
|
*/ |
|
|
|
|
private final ServerBootstrap serverBootstrap = new ServerBootstrap(); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* encoder |
|
|
|
|
*/ |
|
|
|
|
private final NettyEncoder encoder = new NettyEncoder(); |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* default executor |
|
|
|
|
*/ |
|
|
|
@ -106,42 +102,14 @@ public class NettyRemotingServer {
|
|
|
|
|
*/ |
|
|
|
|
public NettyRemotingServer(final NettyServerConfig serverConfig) { |
|
|
|
|
this.serverConfig = serverConfig; |
|
|
|
|
ThreadFactory bossThreadFactory = new ThreadFactoryBuilder().setNameFormat("NettyServerBossThread_%s").build(); |
|
|
|
|
ThreadFactory workerThreadFactory = new ThreadFactoryBuilder().setNameFormat("NettyServerWorkerThread_%s").build(); |
|
|
|
|
if (NettyUtils.useEpoll()) { |
|
|
|
|
this.bossGroup = new EpollEventLoopGroup(1, new ThreadFactory() { |
|
|
|
|
private final AtomicInteger threadIndex = new AtomicInteger(0); |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Thread newThread(Runnable r) { |
|
|
|
|
return new Thread(r, String.format("NettyServerBossThread_%d", this.threadIndex.incrementAndGet())); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
this.workGroup = new EpollEventLoopGroup(serverConfig.getWorkerThread(), new ThreadFactory() { |
|
|
|
|
private final AtomicInteger threadIndex = new AtomicInteger(0); |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Thread newThread(Runnable r) { |
|
|
|
|
return new Thread(r, String.format("NettyServerWorkerThread_%d", this.threadIndex.incrementAndGet())); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
this.bossGroup = new EpollEventLoopGroup(1, bossThreadFactory); |
|
|
|
|
this.workGroup = new EpollEventLoopGroup(serverConfig.getWorkerThread(), workerThreadFactory); |
|
|
|
|
} else { |
|
|
|
|
this.bossGroup = new NioEventLoopGroup(1, new ThreadFactory() { |
|
|
|
|
private final AtomicInteger threadIndex = new AtomicInteger(0); |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Thread newThread(Runnable r) { |
|
|
|
|
return new Thread(r, String.format("NettyServerBossThread_%d", this.threadIndex.incrementAndGet())); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
this.workGroup = new NioEventLoopGroup(serverConfig.getWorkerThread(), new ThreadFactory() { |
|
|
|
|
private final AtomicInteger threadIndex = new AtomicInteger(0); |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Thread newThread(Runnable r) { |
|
|
|
|
return new Thread(r, String.format("NettyServerWorkerThread_%d", this.threadIndex.incrementAndGet())); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
this.bossGroup = new NioEventLoopGroup(1, bossThreadFactory); |
|
|
|
|
this.workGroup = new NioEventLoopGroup(serverConfig.getWorkerThread(), workerThreadFactory); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -191,7 +159,7 @@ public class NettyRemotingServer {
|
|
|
|
|
*/ |
|
|
|
|
private void initNettyChannel(SocketChannel ch) { |
|
|
|
|
ch.pipeline() |
|
|
|
|
.addLast("encoder", encoder) |
|
|
|
|
.addLast("encoder", new NettyEncoder()) |
|
|
|
|
.addLast("decoder", new NettyDecoder()) |
|
|
|
|
.addLast("server-idle-handle", new IdleStateHandler(0, 0, Constants.NETTY_SERVER_HEART_BEAT_TIME, TimeUnit.MILLISECONDS)) |
|
|
|
|
.addLast("handler", serverHandler); |
|
|
|
|