zoukankan      html  css  js  c++  java
  • ChannelInitializer: 每个channel都new ChannelHandle

    State management

    1.业务状态管理-是否登录

    ChannelHandler often needs to store some stateful information. The simplest and recommended approach is to use member variables:

     public interface Message {
         // your methods here
     }
    
     public class DataServerHandler extends SimpleChannelInboundHandler<Message> {
    
         private boolean loggedIn;
    
          @Override
         public void channelRead0(ChannelHandlerContext ctx, Message message) {
             Channel ch = e.getChannel();
             if (message instanceof LoginMessage) {
                 authenticate((LoginMessage) message);
                 loggedIn = true;
             } else (message instanceof GetDataMessage) {
                 if (loggedIn) {
                     ch.write(fetchSecret((GetDataMessage) message));
                 } else {
                     fail();
                 }
             }
         }
         ...
     }

    Because the handler instance has a state variable which is dedicated to one connection, you have to create a new handler instance for each new channel to avoid a race condition where a unauthenticated client can get the confidential information:

    // Create a new handler instance per channel.
     // See ChannelInitializer.initChannel(Channel).
     public class DataServerInitializer extends ChannelInitializer<Channel> {
          @Override
         public void initChannel(Channel channel) {
             channel.pipeline().addLast("handler", new DataServerHandler());
         }
     }

    2。channel 自身状态管理

    public class HeartbeatHandlerInitializer extends ChannelInitializer<Channel> {
    
        private static final int READ_IDEL_TIME_OUT = 10; // 读超时
        private static final int WRITE_IDEL_TIME_OUT = 10;// 写超时
        private static final int ALL_IDEL_TIME_OUT = 0; // 所有超时
    
        @Override
        protected void initChannel(Channel ch) throws Exception {
            System.out.println( "channelId"    +   ch.id());
            //一直执行,一直打印
            while (ch.eventLoop().iterator().hasNext()){
                System.out.println(ch.eventLoop().iterator().next().inEventLoop());
            }
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast(new IdleStateHandler(READ_IDEL_TIME_OUT, WRITE_IDEL_TIME_OUT, ALL_IDEL_TIME_OUT, TimeUnit.SECONDS)); // 1
            pipeline.addLast(new HeartbeatServerHandler());
        }
    }
  • 相关阅读:
    TF-IDF
    3.路径模板两张表设计
    6.订单支付回调接口
    5.创建订单并生成支付链接接口
    5.使用ES代替whoosh全文检索
    4.docker基本使用
    3.ubuntu安装docker
    2.课程全文检索接口
    1.搜索引擎工作原理
    7.视频播放页面接口开发
  • 原文地址:https://www.cnblogs.com/yuyutianxia/p/7264110.html
Copyright © 2011-2022 走看看