2

I am learning netty and there is a following code from example

ChannelPipeline pipeline = pipeline();
// Enable stream compression (you can remove these two if unnecessary)
pipeline.addLast("deflater", new ZlibEncoder(ZlibWrapper.GZIP));
pipeline.addLast("inflater", new ZlibDecoder(ZlibWrapper.GZIP));

// Add the number codec first,
pipeline.addLast("decoder", new BigIntegerDecoder());
pipeline.addLast("encoder", new NumberEncoder());

// and then business logic.
// Please note we create a handler for every new channel
// because it has stateful properties.
pipeline.addLast("handler", new FactorialServerHandler());

My question is where can I see the list of valid 1st parameters for addLast method, like deflater, inflater, decoder, encoder, handler and so on.

And I cannot find the place in source code where mapping is implemented. Here I mean message arrive and ChannelPipeline checks that deflater is set and calls ZlibEncoder.GZIP method.

Viral Shah
  • 2,263
  • 5
  • 21
  • 36
Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101
  • 3
    The names are only used for convenience with the `remove` and `replace` methods. The internal mapping it uses for `DefaultChannelPipeline` is [`name2ctx`](http://static.netty.io/3.5/xref/org/jboss/netty/channel/DefaultChannelPipeline.html#43). – obataku Aug 10 '12 at 07:18
  • 2
    @veer, still it is not clear to me how the framework knows that when packet is received it should call deflater and decoder and when sending the packet inflater and encoder in correct sequence. – Nikolay Kuznetsov Aug 13 '12 at 05:37
  • 1
    upstream and downstream messages are merely propagated along the pipeline in order. – obataku Aug 13 '12 at 09:56

2 Answers2

3

the order as you added

ChannelPipeline p = pipeline();
p.addLast("1", new InboundHandlerA());
p.addLast("2", new InboundHandlerB());
p.addLast("3", new OutboundHandlerA());
p.addLast("4", new OutboundHandlerB());
p.addLast("5", new InboundOutboundHandlerX());

In the given example configuration, the handler evaluation order is 1, 2, 3, 4, 5 when an event goes inbound. When an event goes outbound, the order is 5, 4, 3, 2, 1. On top of this principle, ChannelPipeline skips the evaluation of certain handlers to shorten the stack depth

you can view this website for more detail

https://netty.io/4.0/api/io/netty/channel/ChannelPipeline.html

Year Fu
  • 31
  • 2
  • Many years later, but thanks for this very clear explanation. The subject is particularly confusing since the [ASCII art in the javadocs](https://netty.io/4.1/api/io/netty/channel/ChannelPipeline.html) talks about "inbound" being "bottom up", whereas as you've shown here coding-wise it's "top down" if you look at it in terms of code flow, at least as one typically writes it using `addLast()`. – Laird Nelson Sep 28 '19 at 05:26
2

The names can be anything, they are not keywords, i.e., calling it "encoder" is no different from calling it, "awesome_encoder".

The handlers are aded to the chain in the order in which you call addLast. Whether they get added to the inbound or outbound chain depends on whether they implement ChannelInboundHandler or ChannelOutboundHandler.

There is some good info here: https://docs.jboss.org/netty/3.2/api/org/jboss/netty/channel/ChannelPipeline.html

jpswain
  • 14,642
  • 8
  • 58
  • 63