Netty中Channel的Attribute有什么用

Attribute是用来给channel绑定一些其他参数,同一个channel可以同时绑定多个,比如,在上一个处理器中设置了一些参数,通过Attribute设置给channel,传递给下一个channel

绑定参数的方法:

//先获取一个名为user的AttributeKey

AttributeKey<Object> userIdKey = AttributeKey.valueOf(“user”);

 //绑定到channel上

channel
        //在 Channel 中寻找名为 CHANNEL_CODE 的 AttributeKey
        .attr(userIdKey)
        //给这个 AttributeKey 设置值
        .set(JSON.toJSONString(stringObjectMap));

获取channel上的Attribute参数值

Attribute<String> userIdKey = channel.attr(AttributeKey.valueOf(“user”));
String userId = userIdKey.get();

猜你喜欢

转载自blog.csdn.net/weixin_59244784/article/details/131600127