Reputation: 1
I am trying to create a class which owns a Netty client connection, and is able to receive all of the channel events, such as channelActive, exceptionCaught, and channelRead, like it would if it was on the end of the pipeline.
My class can create a client TCP connection by calling something like this:
ChannelFuture cf = bootstrap.connect(address, port);
I'm wondering if what I need to do is to insert my class onto the end of the channel pipeline when the channel is created, but I'm not sure how to do that without missing any events (i.e. initial data from the far end). The channel pipeline gets created based on how the bootstrap was configured, so I don't see how I can arrange to have my class added to the pipeline initially.
I can wait for the connection to be made, then do something like this:
cf.get().pipeline().addLast(this);
but I'm not sure that my class is getting added to the pipeline soon enough in this case. For example, will I still see the channelActive event if I do this?
Upvotes: 0
Views: 823
Reputation: 31
If you add the handler in a channel initializer, it will get all the events.
Upvotes: 0
Reputation: 98
Apologies if you already know the thoughts below but if you didn't then might help (the quotes are from the Manning book Netty in Action which is a great book to understand all things Netty):
"the ServerBootstrap bootstraps ServerChannel implementations, which are responsible for creating child Channels...When a new connection is accepted, a new child Channel will be created and the ChannelInitializer will add an instance of our [handler] to the Channel's ChannelPipeline."
There are great examples to follow in:
Upvotes: 1