Browse Source

its-network netty close method update

shjung 16 hours ago
parent
commit
e812f90d58

+ 18 - 4
its-network/src/main/java/com/its/common/network/tcp/server/NettyTcpServer.java

@@ -57,13 +57,20 @@ public abstract class NettyTcpServer {
 
         try {
             if (this.config.getBindingAddr().equals("0.0.0.0")) {
-                this.channelFuture = this.serverBootstrap.bind(this.config.getBindingPort());
+                this.channelFuture = this.serverBootstrap.bind(this.config.getBindingPort()).sync();
             }
             else {
-                this.channelFuture = this.serverBootstrap.bind(this.config.getBindingAddr(), config.getBindingPort());
+                this.channelFuture = this.serverBootstrap.bind(this.config.getBindingAddr(), config.getBindingPort()).sync();
             }
-        }
-        catch (Exception e) {
+            if (!this.channelFuture.isSuccess()) {
+                log.error("NettyTcpServer bind failed: {}", channelFuture.cause().getMessage());
+            }
+        } catch (InterruptedException ie) {
+            Thread.currentThread().interrupt();
+            log.error("NettyTcpServer bind InterruptedException: {}", ie.getMessage());
+            if (this.acceptGroups != null) this.acceptGroups.shutdownGracefully();
+            if (this.workerGroups != null) this.workerGroups.shutdownGracefully();
+        } catch (Exception e) {
             log.error("NettyTcpServer bind exception: {}", e.getMessage());
             if (this.acceptGroups != null) this.acceptGroups.shutdownGracefully();
             if (this.workerGroups != null) this.workerGroups.shutdownGracefully();
@@ -72,14 +79,21 @@ public abstract class NettyTcpServer {
 
     public void stop() {
         try {
+            if (this.channelFuture != null && this.channelFuture.channel() != null) {
+                this.channelFuture.channel().close().sync();
+                this.channelFuture = null;
+            }
             if (this.acceptGroups != null) {
                 this.acceptGroups.shutdownGracefully().sync();
+                this.acceptGroups = null;
             }
             if (this.workerGroups != null) {
                 this.workerGroups.shutdownGracefully().sync();
+                this.workerGroups = null;
             }
 //            if (this.channelFuture != null && this.channelFuture.channel() != null) {
 //                this.channelFuture.channel().closeFuture().sync();
+//                this.channelFuture = null;
 //            }
         }
         catch (InterruptedException e) {