1
votes
  1. I am new to Netty and trying to count received and sent bytes for each channel. I found that i should use ChannelTrafficShapingHandler for that but i have no idea how to reach my goal. As i undersand, i should write my own handler which will exetds from ChannelTrafficShapingHandler with adding some methods? It would be great to provide real code examples.

    @Override
    public void initChannel(SocketChannel ch) {
    ChannelPipeline p = ch.pipeline();
    if (sslCtx != null) {
        p.addLast(sslCtx.newHandler(ch.alloc()));
    }
    p.addLast(new HttpRequestDecoder());
    // Uncomment the following line if you don't want to handle HttpChunks.
    //p.addLast(new HttpObjectAggregator(1048576));
    p.addLast(new HttpResponseEncoder());
    // Remove the following line if you don't want automatic content compression.
    //p.addLast(new HttpContentCompressor());
    p.addLast(new HttpSnoopServerHandler());
    
    p.addLast(new ChannelTrafficShapingHandler(0, 0));
    

    }

like adding class to handlers, but how can i use that?

  1. I wrote post (Netty http count requests) and was advised to write my own handler to count my requests, opened connections etc. However, i can't understand how to manage different url's with different handlers. For example, i have url /info - that's okey and I want to handle it in Handler #1. Then, i have url /statistic and i want to count there my values and print them. This url should be managed by Handler #2. How could i do that?

  2. I have a lot of data which i should output on page(like several tables), what should i use for that? I think that string is not suitable for huge output

Thank you for your patience and answers

1

1 Answers

1
votes

First, add a HttpObjectAggregator handler on pipeline. It will handle all http chuncks for you and put on pipeline a FullHttpRequest.

Then, implement a handler extending SimpleChannelInboundHandler<FullHttpRequest> and add it at the end of the pipeline. On the FullHttpRequest catched into this handler you have methods to get headers, requested URI, content, etc. You can store content data size as you want into a map or other, by requested path, etc. Implement all you need.

For the response, if you use http/html, you use text (so strings). Instantiante an instance of DefaultFullHttpResponse, and put your html text as content for this response. Next, you have just to push it on pipeline by calling ctx.writeAndFlush() (the HttpResponseEncoder will convert the message into a valid http response for you).

Moreover, you can optionnaly add a HttpContentCompressor on pipeline to activate compression when possible.