From: Robert Varga Date: Tue, 31 May 2022 18:29:24 +0000 (+0200) Subject: Eliminate BootstrapConnectListener X-Git-Tag: v4.0.0~67 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=netconf.git;a=commitdiff_plain;h=a33c469af7ba77c86a89fb1637c6e65bc9cf234b Eliminate BootstrapConnectListener Use a simple method and a method handle instead of an explicit class. This makes things a tad simpler on synchronization side of things. Change-Id: Id341b8a3ff124bd95a1e4e14f60721e6b443f72b Signed-off-by: Robert Varga --- diff --git a/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/NetconfSessionPromise.java b/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/NetconfSessionPromise.java index f411534452..6cb4ba59c9 100644 --- a/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/NetconfSessionPromise.java +++ b/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/NetconfSessionPromise.java @@ -54,7 +54,7 @@ final class NetconfSessionPromise extends DefaultPromi } final ChannelFuture connectFuture = bootstrap.connect(address); // Add listener that attempts reconnect by invoking this method again. - connectFuture.addListener(new BootstrapConnectListener()); + connectFuture.addListener((ChannelFutureListener) this::channelConnectComplete); pending = connectFuture; } catch (final Exception e) { LOG.info("Failed to connect to {}", address, e); @@ -79,44 +79,38 @@ final class NetconfSessionPromise extends DefaultPromi return super.setSuccess(result); } - private class BootstrapConnectListener implements ChannelFutureListener { - @Override - public void operationComplete(final ChannelFuture cf) { - synchronized (NetconfSessionPromise.this) { - LOG.debug("Promise {} connection resolved", NetconfSessionPromise.this); - - // Triggered when a connection attempt is resolved. - checkState(pending.equals(cf)); - - /* - * The promise we gave out could have been cancelled, - * which cascades to the connect getting cancelled, - * but there is a slight race window, where the connect - * is already resolved, but the listener has not yet - * been notified -- cancellation at that point won't - * stop the notification arriving, so we have to close - * the race here. - */ - if (isCancelled()) { - if (cf.isSuccess()) { - LOG.debug("Closing channel for cancelled promise {}", NetconfSessionPromise.this); - cf.channel().close(); - } - return; - } + // Triggered when a connection attempt is resolved. + private synchronized void channelConnectComplete(final ChannelFuture cf) { + LOG.debug("Promise {} connection resolved", this); + checkState(pending.equals(cf)); + + /* + * The promise we gave out could have been cancelled, + * which cascades to the connect getting cancelled, + * but there is a slight race window, where the connect + * is already resolved, but the listener has not yet + * been notified -- cancellation at that point won't + * stop the notification arriving, so we have to close + * the race here. + */ + if (isCancelled()) { + if (cf.isSuccess()) { + LOG.debug("Closing channel for cancelled promise {}", this); + cf.channel().close(); + } + return; + } - if (cf.isSuccess()) { - LOG.debug("Promise {} connection successful", NetconfSessionPromise.this); - return; - } + if (cf.isSuccess()) { + LOG.debug("Promise {} connection successful", this); + return; + } - LOG.debug("Attempt to connect to {} failed", address, cf.cause()); + LOG.debug("Attempt to connect to {} failed", address, cf.cause()); - final Future rf = strategy.scheduleReconnect(cf.cause()); - rf.addListener(new ReconnectingStrategyListener()); - pending = rf; - } - } + final Future rf = strategy.scheduleReconnect(cf.cause()); + rf.addListener(new ReconnectingStrategyListener()); + pending = rf; } private class ReconnectingStrategyListener implements FutureListener {