首页  

netty异步机制     所属分类 netty 浏览量 398
Netty  异步的 基于事件驱动的网络编程框架

BIO 
NIO  non-blocking IO 

Netty 中的 I/O 操作是异步的(Bind、Connect、Write等操作 ),会立刻返回一个 ChannelFuture

Future-Listener 机制

future 和 callback

sync 	阻塞等待结果返回
isDone  判断当前操作是否完成
isSuccess  
getCause  获取已完成的当前操作失败原因
isCancelled  
addListener  注册监听器


 ChannelFuture channelFuture = serverBootstrap.bind(8888).sync();
channelFuture.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture channelFuture) throws Exception {
            if (channelFuture.isSuccess()){
                System.out.println("Server bind success");
            }else {
                System.out.println("Server bind failed");
            }
            }
        });

 @Override
    public void channelActive(ChannelHandlerContext channelHandlerContext) throws Exception{
        ChannelFuture channelFuture = channelHandlerContext.writeAndFlush(Unpooled.copiedBuffer("hello,netty", CharsetUtil.UTF_8));
        channelFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture channelFuture) throws Exception {
                if (channelFuture.isSuccess()){
                    System.out.println("Client send success");
                }else {
                    System.out.println("Client send failed");
                }
            }
        });
    }




netty ChannelFuture 实例 netty中的future和promise

上一篇     下一篇
GO值类型和引用类型

go mod package 实例

go依赖查看

TDengine 白皮书 要点

杭州爬山路线

杭州跑步徒步路线