繁体   English   中英

Netty 4:如何在不获取IllegalReferenceCountException的情况下发送相同的DefaultFullHttpResponse实例

[英]Netty 4: How to send the same DefaultFullHttpResponse instance without getting a IllegalReferenceCountException

我正在使用netty 4编写模拟服务器。您可以为模拟服务器提供DefaultFullHttpResponse ,每次指定的请求匹配时应返回该DefaultFullHttpResponse

如果响应最多发送两次,则响应正常,当响应第三次发送时,抛出IllegalReferenceCountException 据我了解, DefaultFullHttpResponse具有其自己的ByteBuf(http内容)。 因此,当发送响应时,参考计数器会减少。 这个逻辑不掌握在我手中,因为它完成了HttpCodec。

我的问题是:如何多次使用相同的DefaultFullHttpResponse 我需要在发送内容时retain()内容,对吗? 这不是问题,因为所有响应都是DefaultFullHttpResponse但是模拟服务器是通用的,并允许使用其他协议和编解码器。

如果响应消息像DefaultFullHttpResponse一样实现ByteBufHolder ,则可以复制其ByteBuf。

if (resp is ByteBufHolder) {
    resp = resp.duplicate() //duplicates the byte buffer of the original message
    resp.retain()
    resp.touch()
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM