- ChannelFuture channelFuture = serverBootstrap.bind(ip, port).sync();
- Channel serverListenChannel = channelFuture.channel();
- serverChannel = serverListenChannel;
- // Wait until the server socket is closed.
- serverListenChannel.closeFuture().sync();
- } catch (InterruptedException e) {
- LOG.error("Thread interrupted", e);
- } catch (Throwable throwable) {
- // sync() re-throws exceptions declared as Throwable, so the compiler doesn't see them
- LOG.error("Error while binding to address {}, port {}", ip, port, throwable);
- throw throwable;
- }
+ .bind(ip, port)
+ // Propagate the channel when its ready
+ .addListener((ChannelFutureListener) future -> {
+ if (future.isSuccess()) {
+ serverChannel = future.channel();
+ } else {
+ LOG.error("Error while binding to address {}, port {}", ip, port, future.cause());
+ }
+ });