mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-25 21:45:08 +08:00
rpc
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package com.jsowell.netty.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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user