Netty 4:如何在不获取IllegalReferenceCountException

时间:2019-02-14 10:52:53

标签: java http netty

我正在使用Netty 4编写模拟服务器。您可以给模拟服务器一个DefaultFullHttpResponse,每次指定请求匹配时都应返回该IllegalReferenceCountException

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

我的问题是:如何多次使用相同的retain()?发送内容时,我需要DefaultFullHttpResponse,对吗?这不是问题,因为所有响应都是Fensterkreuz(){ jl1 = new JLabel("0"); jl2 = new JLabel("0"); jl1.setSize(new Dimension(100,100)); jl2.setSize(new Dimension(100,100)); jl1.setFont(new Font ("Arial", Font.PLAIN, 15)); jl2.setFont(new Font ("Arial", Font.PLAIN, 15)); cP = new Point(); this.add(jl1); this.add(jl2); addMouseMotionListener(this); } public void mouseDragged (MouseEvent e){ } public void mouseMoved (MouseEvent e) { cP = e.getPoint(); repaint(); } public void paint (Graphics g){ g.drawLine((cP.x),(cP.y-15), (cP.x),(cP.y+15)); g.drawLine((cP.x-15),(cP.y), (cP.x+15),(cP.y)); jl1.setText(String.valueOf(cP.x)); jl2.setText(String.valueOf(cP.y)); } public static void main (String [] args) { JFrame f = new JFrame(); JComponent test = new Fensterkreuz(); test.setOpaque(false); f.setVisible(true); f.setSize(1500,1000); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setContentPane(test); } ,但是模拟服务器是通用的,并允许使用其他协议和编解码器。

1 个答案:

答案 0 :(得分:0)

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

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