@Slf4j
public class CloseFutureClient3 {
public static void main(String[] args) throws InterruptedException {
NioEventLoopGroup group = new NioEventLoopGroup();
ChannelFuture channelFuture = new Bootstrap()
.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
ch.pipeline().addLast(new StringEncoder());
}
})
.connect(new InetSocketAddress("localhost", 8888));
Channel channel = channelFuture.sync().channel();
new Thread (()->{
Scanner scanner = new Scanner(System.in);
while (true){
String s = scanner.nextLine();
if("q".equals(s)){
channel.close();
break;
}
channel.writeAndFlush(s);
}
},"input").start();
ChannelFuture closeFuture = channel.closeFuture();
closeFuture.addListener((ChannelFutureListener) future -> {
log.info("guan bi ...");
group.shutdownGracefully();
});
}
}