mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-24 04:55:08 +08:00
55 lines
1.7 KiB
Java
55 lines
1.7 KiB
Java
package com.jsowell.pile.rpc;
|
|
|
|
import io.netty.bootstrap.ServerBootstrap;
|
|
import io.netty.channel.*;
|
|
import io.netty.channel.nio.NioEventLoopGroup;
|
|
import io.netty.channel.socket.nio.NioServerSocketChannel;
|
|
|
|
public class RpcServer {
|
|
|
|
public void bind(Integer port) {
|
|
|
|
EventLoopGroup parent = new NioEventLoopGroup();
|
|
EventLoopGroup child = new NioEventLoopGroup();
|
|
Channel channel = null;
|
|
|
|
try{
|
|
ServerBootstrap serverBootstrap = new ServerBootstrap();
|
|
|
|
serverBootstrap.group(parent, child)
|
|
.channel(NioServerSocketChannel.class)
|
|
.option(ChannelOption.SO_BACKLOG, 1024)
|
|
.childHandler(new ServerChannelInitializer());
|
|
|
|
ChannelFuture channelFuture = serverBootstrap.bind(port).sync();
|
|
|
|
System.out.println("server启动");
|
|
|
|
// 非阻塞等待关闭
|
|
channelFuture.channel().closeFuture().addListener(new ChannelFutureListener() {
|
|
@Override
|
|
public void operationComplete(ChannelFuture channelFuture) throws Exception {
|
|
System.out.println("server关闭");
|
|
parent.shutdownGracefully();
|
|
child.shutdownGracefully();
|
|
}
|
|
});
|
|
|
|
channel = channelFuture.channel();
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
if(channel == null || !channel.isActive()) {
|
|
System.out.println("server关闭");
|
|
parent.shutdownGracefully();
|
|
child.shutdownGracefully();
|
|
} else {
|
|
channel.close();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
} |