Эх сурвалжийг харах

netty-server close method update

shjung 1 өдөр өмнө
parent
commit
293da2aa14

+ 18 - 4
tsi-comm-server/src/main/java/com/tsi/comm/server/xnet/NettyTcpServer.java

@@ -53,13 +53,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();
@@ -68,14 +75,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) {