简体   繁体   English

Java Netty Server的高效channelRead

[英]Efficient channelRead for Java Netty Server

I'm using netty to develop a proxy server and my proxy ProxyBackendHandler class is as follows. 我正在使用netty开发代理服务器,我的代理ProxyBackendHandler类如下。 There on channelRead method I need to get the msg data and write to client as TextWebSocketFrame. 在channelRead方法上,我需要获取msg数据并将其作为TextWebSocketFrame写入客户端。 To do that I have used a StringBuilder and a while loop to iterate the ByteBuf. 为此,我使用了一个StringBuilder和一个while循环来迭代ByteBuf。 Can anyone suggest me a better way to do this as it seems that above code has high perfomance overhead when the high data loads. 任何人都可以建议我一种更好的方法来执行此操作,因为在加载大量数据时,上述代码似乎具有较高的性能开销。

public class ProxyBackendHandler extends ChannelInboundHandlerAdapter {

    private final Channel inboundChannel;
    StringBuilder sReplyBuffer;

    public ProxyBackendHandler(Channel inboundChannel) {
        this.inboundChannel = inboundChannel;
        sReplyBuffer = new StringBuilder(4000);
    }

    @Override
    public void channelRead(final ChannelHandlerContext ctx, Object msg) {

        // Please suggest a efficient implementation for read msg and pass it to writeAndFlush.
        ByteBuf in = (ByteBuf) msg;
        sReplyBuffer.setLength(0); 

        try {
            while (in.isReadable()) { 
                sReplyBuffer.append((char) in.readByte());
            }
        } finally {
            ((ByteBuf) msg).release(); 
        }

        inboundChannel.writeAndFlush(new TextWebSocketFrame (sReplyBuffer.toString())).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) {
                if (future.isSuccess()) {
                    ctx.channel().read();
                    System.out.println("Sent To Client");
                } else {
                    future.channel().close();
                }
            }
        });
    }
}

Maybe something like this: 也许是这样的:

public class ProxyBackendHandler extends ChannelInboundHandlerAdapter {

    private final Channel inboundChannel;

    public ProxyBackendHandler(Channel inboundChannel) {
        this.inboundChannel = inboundChannel;
    }

    @Override
    public void channelRead(final ChannelHandlerContext ctx, Object msg) {
        inboundChannel.writeAndFlush(new TextWebSocketFrame((ByteBuf) msg)).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) {
                if (future.isSuccess()) {
                    ctx.channel().read();
                    System.out.println("Sent To Client");
                } else {
                    future.channel().close();
                }
            }
        });
    }
}

I suggest not using a StringBuilder at all. 我建议根本不使用StringBuilder。 Just use the buffer you already have. 只需使用您已有的缓冲区即可。 You don't state what a TextWebSocketFrame might be, or why you think you need it, but ultimately a proxy server only has to copy bytes. 您没有说明TextWebSocketFrame可能是什么,也没有说明您为什么认为需要它,但是最终代理服务器只需要复制字节。 You don't need StringBuilders or extra classes for that. 您不需要StringBuilders或其他类。 Or Netty, frankly. 坦白说,还是Netty。

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

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