Skip to content

Instantly share code, notes, and snippets.

@KowalczykBartek
Last active September 12, 2017 18:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KowalczykBartek/c1d0147f8b2b9292547690841d424b87 to your computer and use it in GitHub Desktop.
Save KowalczykBartek/c1d0147f8b2b9292547690841d424b87 to your computer and use it in GitHub Desktop.
Netty - misleading comment
Lets consider this simple code snippet :
final EventLoopGroup group = new NioEventLoopGroup(1);
group.execute(new Runnable()
{
@Override
public void run()
{
{
Bootstrap b = new Bootstrap();
b.group(group) //
.channel(NioSocketChannel.class)//
.handler(new SampleClientInitializer()); //
b.connect("localhost", 9999);
}
}
});
Now, "connect" will be invoked inside eventLoop,
knowing that lets jump to first method that will be called under the hood (initAndRegister()), we can read here that:
// If we are here and the promise is not failed, it's one of the following cases:
// 1) If we attempted registration from the event loop, the registration has been completed at this point.
// i.e. It's safe to attempt bind() or connect() now because the channel has been registered.
So, here method register0 (AbstractChannel.java:496) will be already invoked, and because of that channelRegistered event
is also triggered here. But some line later we can read that
private static void doConnect(
final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise connectPromise) {
// This method is invoked before channelRegistered() is triggered. Give user handlers a chance to set up
// the pipeline in its channelRegistered() implementation.
final Channel channel = connectPromise.channel();
channel.eventLoop().execute(new Runnable() {
@Override
public void run() {
if (localAddress == null) {
channel.connect(remoteAddress, connectPromise);
} else {
channel.connect(remoteAddress, localAddress, connectPromise);
}
connectPromise.addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
}
});
}
but it is not the case, doConnect is called after.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment