Selaa lähdekoodia

netty close method update

shjung 1 päivä sitten
vanhempi
commit
9524fe75d6

+ 20 - 10
its-network/src/main/java/com/its/common/network/tcp/server/NettyTcpServer.java

@@ -30,16 +30,20 @@ public abstract class NettyTcpServer {
 
         try {
             if (NettyUtils.isEpollAvailable()) {
-                log.info("서버가 리눅스 EPOLL 모드에서 실행됩니다.");
+                log.info("Server is running in Linux EPOLL mode.");
             }
             else {
-                log.info("서버가 윈도우 NIO 모드에서 실행됩니다.");
+                log.info("Server is running in Windows NIO mode.");
             }
 
             this.serverBootstrap = ServerBootstrapFactory.createBootstrap(this.config, this.channelInitializer);
+            this.acceptGroups = this.serverBootstrap.config().group();
+            this.workerGroups = this.serverBootstrap.config().childGroup();
 
         } catch (Exception e) {
-            log.error("run exception: {}", e.toString());
+            log.error("NettyTcpServer createBootstrap exception: {}", e.getMessage());
+            log.error("NettyTcpServer cannot be started. It will be terminated. *************************************");
+            return;
         }
 
         log.info("================================================");
@@ -60,20 +64,26 @@ public abstract class NettyTcpServer {
             }
         }
         catch (Exception e) {
-            log.error("{}", e.toString());
-            this.acceptGroups.shutdownGracefully();
-            this.workerGroups.shutdownGracefully();
+            log.error("NettyTcpServer bind exception: {}", e.getMessage());
+            if (this.acceptGroups != null) this.acceptGroups.shutdownGracefully();
+            if (this.workerGroups != null) this.workerGroups.shutdownGracefully();
         }
     }
 
     public void stop() {
         try {
-            this.acceptGroups.shutdownGracefully().sync();
-            this.workerGroups.shutdownGracefully().sync();
-            this.channelFuture.channel().closeFuture().sync();
+            if (this.acceptGroups != null) {
+                this.acceptGroups.shutdownGracefully().sync();
+            }
+            if (this.workerGroups != null) {
+                this.workerGroups.shutdownGracefully().sync();
+            }
+//            if (this.channelFuture != null && this.channelFuture.channel() != null) {
+//                this.channelFuture.channel().closeFuture().sync();
+//            }
         }
         catch (InterruptedException e) {
-            log.error("{}", e.toString());
+            log.error("NettyTcpServer stop interrupt: {}", e.getMessage());
             Thread.currentThread().interrupt();
         }
     }