Last active
September 12, 2017 18:16
-
-
Save KowalczykBartek/c1d0147f8b2b9292547690841d424b87 to your computer and use it in GitHub Desktop.
Netty - misleading comment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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