gpt4 book ai didi

java - ChannelHandlerContext setAttachment 方法

转载 作者:行者123 更新时间:2023-12-01 18:44:36 37 4
gpt4 key购买 nike

因此,在 Netty 3.x 库中,ChannelHandlerContext 类有一个名为 setAttachment 的方法。不幸的是,它似乎不存在于新的 4.0 库中。我想知道是否有一种方法可以像旧库一样添加附件。

最佳答案

经过一番研究后,我发现了以下内容:

接口(interface)的 javadoc org.jboss.netty.channel.ChannelHandlerContext在版本 3.2 状态

Storing stateful information

setAttachment(Object) and getAttachment() allow you to store and access stateful information that is related with a handler and its context. Please refer to ChannelHandler to learn various recommended ways to manage stateful information.

以及 io.netty.channel.ChannelHandlerContext 的 javadoc在版本 4.0 中,它显然取代了版本 3.x 的 org.jboss.netty.channel.ChannelHandlerContext 声明以下内容用于存储状态信息

Storing stateful information

AttributeMap.attr(AttributeKey) allow you to store and access stateful information that is related with a handler and its context. Please refer to ChannelHandler to learn various recommended ways to manage stateful information.

似乎 3.x 版本的附件现在使用 Attribute<T> 进行处理(generics 而不是 Object!)在版本 4.0

所以你需要看看AttributeMap.attr(AttributeKey)以及示例说明

// This handler will receive a sequence of increasing integers starting from 1.

在两个版本的 ChannelHandlerContext 的 javadoc 中都有给出。

我希望这将帮助您找到一种使用新 API 来编码您的需求的方法。

关于java - ChannelHandlerContext setAttachment 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18344312/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com