Register NioSocketChannel
[netconf.git] / transport / transport-tcp / src / main / java / org / opendaylight / netconf / transport / tcp / NioNettyImpl.java
index 3e1f93cf4513962204778f1bd102edff9108f216..c25f9b7db42dc8ef6062f7b28ffcd8ff0919c448 100644 (file)
@@ -98,17 +98,37 @@ final class NioNettyImpl extends AbstractNettyImpl {
         SUPPORT = support;
     }
 
-    private final boolean supportsKeepalives;
+    static final NioNettyImpl INSTANCE;
 
-    NioNettyImpl() {
-        final var ch = new NioSocketChannel();
+    static {
+        final var grp = new NioEventLoopGroup();
         try {
-            supportsKeepalives = SUPPORT.configureKeepalives(ch.config());
-        } finally {
-            ch.close();
+            try {
+                final var ch = new NioSocketChannel();
+                grp.register(ch).sync();
+
+                final boolean supportsKeepalives;
+                try {
+                    supportsKeepalives = SUPPORT.configureKeepalives(ch.config());
+                } finally {
+                    ch.close().sync();
+                }
+                INSTANCE = new NioNettyImpl(supportsKeepalives);
+            } finally {
+                grp.shutdownGracefully().sync();
+            }
+        } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
+            throw new ExceptionInInitializerError(e);
         }
     }
 
+    private final boolean supportsKeepalives;
+
+    private NioNettyImpl(final boolean supportsKeepalives) {
+        this.supportsKeepalives = supportsKeepalives;
+    }
+
     @Override
     Class<NioSocketChannel> channelClass() {
         return NioSocketChannel.class;