Commit d24f878a by huangfusuper

Netty服务端 main方式与异步线程方式的实现 编写完毕

parent e6e970f1
package com.byit.server.netty;
import com.byit.factory.PluginServerFactory;
import com.byit.handler.PackerSpliterHandler;
import com.byit.handler.PacketDecodeHandler;
import com.byit.handler.PacketEncodeHandler;
import com.byit.server.PluginServer;
import com.byit.server.netty.handler.NettyPluginServerHandler;
import com.byit.utils.ThreadPoolUtil;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import java.util.concurrent.ThreadPoolExecutor;
/**
* main方法启动
* @author huangfu
*/
public class MainNettyPluginServer extends PluginServer {
@Override
public void start(PluginServerFactory pluginServerFactory) {
ThreadPoolExecutor threadPoolExecutor = ThreadPoolUtil.makeServerThreadPool(NettyPluginServer.class.getName(),
pluginServerFactory.getCorePoolSize(), pluginServerFactory.getMaxPoolSize());
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup,workGroup)
.channel(NioServerSocketChannel.class)
.childOption(ChannelOption.TCP_NODELAY, true)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("packerSpliterHandler",new PackerSpliterHandler());
pipeline.addLast("packetDecodeHandler",new PacketDecodeHandler());
pipeline.addLast("nettyPluginServerHandler",new NettyPluginServerHandler(pluginServerFactory,threadPoolExecutor));
pipeline.addLast("packetEncodeHandler",new PacketEncodeHandler());
}
});
ChannelFuture channelFuture = serverBootstrap.bind(pluginServerFactory.getPort()).sync();
super.onStart();
ChannelFuture closeFuture = channelFuture.channel().closeFuture().sync();
closeFuture.addListener(future ->{
if (future.isSuccess()) {
System.out.println("-----------------");
}
});
}catch (Exception e){
e.printStackTrace();
}finally {
try {
threadPoolExecutor.shutdown();
}catch (Exception e){
e.printStackTrace();
}
try {
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}catch (Exception e){
e.printStackTrace();
}
}
}
@Override
public void stop() {
System.out.println("-------------");
super.onStop();
}
}
package com.byit.server.netty;
import com.byit.factory.PluginServerFactory;
import com.byit.handler.PackerSpliterHandler;
import com.byit.handler.PacketDecodeHandler;
import com.byit.handler.PacketEncodeHandler;
import com.byit.server.PluginServer;
import com.byit.server.netty.handler.NettyPluginServerHandler;
import com.byit.utils.ThreadPoolUtil;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import java.util.concurrent.ThreadPoolExecutor;
/**
* Netty服务启动
* @author huangfu
*/
public class NettyPluginServer extends PluginServer {
private Thread serverThread;
@Override
public void start(PluginServerFactory pluginServerFactory) {
serverThread = new Thread(() ->{
final ThreadPoolExecutor threadPoolExecutor = ThreadPoolUtil.makeServerThreadPool(NettyPluginServer.class.getName(),
pluginServerFactory.getCorePoolSize(), pluginServerFactory.getMaxPoolSize());
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup,workGroup)
.channel(NioServerSocketChannel.class)
.childOption(ChannelOption.TCP_NODELAY, true)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("packerSpliterHandler",new PackerSpliterHandler());
pipeline.addLast("packetDecodeHandler",new PacketDecodeHandler());
pipeline.addLast("nettyPluginServerHandler",new NettyPluginServerHandler(pluginServerFactory,threadPoolExecutor));
pipeline.addLast("packetEncodeHandler",new PacketEncodeHandler());
}
});
ChannelFuture channelFuture = serverBootstrap.bind(pluginServerFactory.getPort()).sync();
super.onStart();
channelFuture.channel().closeFuture().sync();
}catch (Exception e){
e.printStackTrace();
}finally {
try {
threadPoolExecutor.shutdown();
}catch (Exception e){
e.printStackTrace();
}
try {
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}catch (Exception e){
e.printStackTrace();
}
}
});
serverThread.setName("【com.byit.server.netty.NettyPluginServer#start thread run】"+serverThread.hashCode());
serverThread.setDaemon(true);
serverThread.start();
}
@Override
public void stop() {
if (serverThread != null && serverThread.isAlive()) {
serverThread.interrupt();
}
super.onStop();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment