Explorar el Código

20240215: VmsTcpCommServerService code refectoring

shjung hace 1 año
padre
commit
e7a0406095

+ 30 - 25
src/main/java/com/its/vms/xnettcp/vms/VmsTcpCommServerService.java

@@ -35,10 +35,9 @@ public class VmsTcpCommServerService {
     private ChannelFuture channelFuture = null;
 
     public void run() {
-        if (!OS.isWindows()) {
-            if (!Epoll.isAvailable()) {
-                Epoll.unavailabilityCause().printStackTrace();
-            }
+        if (!OS.isWindows() && !Epoll.isAvailable()) {
+            log.warn("OS is not windows but Epoll is not available");
+//            Epoll.unavailabilityCause().printStackTrace();
         }
         if (NettyUtils.isEpollAvailable()) {
             log.info("서버가 리눅스 EPOLL 모드에서 실행됩니다.");
@@ -76,39 +75,45 @@ public class VmsTcpCommServerService {
     public void stop() {
         try {
             this.acceptGroups.shutdownGracefully().sync();
+        } catch (InterruptedException e) {
+            log.error("stop, acceptGroups.InterruptedException");
+        }
+        try {
             this.workerGroups.shutdownGracefully().sync();
+        } catch (InterruptedException e) {
+            log.error("stop, workerGroups.InterruptedException");
+        }
+        try {
             this.channelFuture.channel().closeFuture().sync();
         } catch (InterruptedException e) {
-            log.error("stop, InterruptedException");
+            log.error("stop, channelFuture.InterruptedException");
         }
     }
 
     public ServerBootstrap createBootstrap() {
-        ServerBootstrap serverBootstrap = new ServerBootstrap();
-        EventLoopGroup acceptGroups;
-        EventLoopGroup workerGroups;
+        ServerBootstrap bootstrap = new ServerBootstrap();
 
-        acceptGroups = NettyUtils.newEventLoopGroup(config.getAcceptThreads(), "Accept");
-        workerGroups = NettyUtils.newEventLoopGroup(config.getWorkerThreads(), "Worker");
-        serverBootstrap.channel(NettyUtils.getServerSocketChannel());
-        serverBootstrap.group(acceptGroups, workerGroups);
+        this.acceptGroups = NettyUtils.newEventLoopGroup(this.config.getAcceptThreads(), "Accept");
+        this.workerGroups = NettyUtils.newEventLoopGroup(this.config.getWorkerThreads(), "Worker");
+        bootstrap.channel(NettyUtils.getServerSocketChannel());
+        bootstrap.group(this.acceptGroups, this.workerGroups);
 
-        serverBootstrap.option(ChannelOption.AUTO_READ, true);
-        serverBootstrap.option(ChannelOption.SO_BACKLOG, config.getBackLog());
-        serverBootstrap.option(ChannelOption.SO_REUSEADDR, true);
-        serverBootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.getConnectTimeoutSeconds()*1000);
+        bootstrap.option(ChannelOption.AUTO_READ, true);
+        bootstrap.option(ChannelOption.SO_BACKLOG, this.config.getBackLog());
+        bootstrap.option(ChannelOption.SO_REUSEADDR, true);
+        bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, this.config.getConnectTimeoutSeconds()*1000);
 
         // TODO: 20240213 - START
         //serverBootstrap.option(ChannelOption.SO_RCVBUF, config.getRcvBuf());
-        serverBootstrap.option(ChannelOption.SO_SNDBUF, 1024 * 32768);
-        serverBootstrap.option(ChannelOption.SO_RCVBUF, 1024 * 32768);
-        serverBootstrap.option(ChannelOption.TCP_NODELAY, true);
+        bootstrap.option(ChannelOption.SO_SNDBUF, 1024 * 32768);
+        bootstrap.option(ChannelOption.SO_RCVBUF, 1024 * 32768);
+        bootstrap.option(ChannelOption.TCP_NODELAY, true);
         // TODO: 20240213 - END
 
-        serverBootstrap.childOption(ChannelOption.SO_LINGER, 0);           // 4way-handshake 비활성
-        serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, false);    // KEEPALIVE 비활성(활성: true)
-        serverBootstrap.childOption(ChannelOption.SO_REUSEADDR, true);     // 소켓 재사용
-        serverBootstrap.childOption(ChannelOption.TCP_NODELAY, true);      // Nagle 알고리즘 비활성화, true
+        bootstrap.childOption(ChannelOption.SO_LINGER, 0);           // 4way-handshake 비활성
+        bootstrap.childOption(ChannelOption.SO_KEEPALIVE, false);    // KEEPALIVE 비활성(활성: true)
+        bootstrap.childOption(ChannelOption.SO_REUSEADDR, true);     // 소켓 재사용
+        bootstrap.childOption(ChannelOption.TCP_NODELAY, true);      // Nagle 알고리즘 비활성화, true
 
         VmsTcpCommServerInitializer vmsTcpCommServerInitializer = new VmsTcpCommServerInitializer(
                 this.config,
@@ -118,9 +123,9 @@ public class VmsTcpCommServerService {
                 this.tcpServerByteBufMessageDecoder,
                 this.repoService
         );
-        serverBootstrap.childHandler(vmsTcpCommServerInitializer);
+        bootstrap.childHandler(vmsTcpCommServerInitializer);
 
-        return serverBootstrap;
+        return bootstrap;
     }
 
 }

+ 2 - 1
src/main/java/com/its/vms/xnettcp/vms/codec/VmsTcpServerEncoder.java

@@ -55,7 +55,8 @@ public class VmsTcpServerEncoder extends MessageToByteEncoder<Object> {
             ByteBuffer buffer = (ByteBuffer) msg;
             byte[] sendBytes = buffer.array();
             outByteBuf.writeBytes(sendBytes);
-            buffer = null;  // TODO: 20240213
+            buffer = null;  // 20240213, io.netty.util.internal.OutOfDirectMemoryError
+            //ReferenceCountUtil.release(buffer);   // 20240213, io.netty.util.internal.OutOfDirectMemoryError
             log.info("SEND: VMS {}, VmsTcpServerEncoder.encode: {} Bytes.", obj.getVmsCtlrNmbr(), sendBytes.length);
         }
         finally {

+ 2 - 0
src/main/java/com/its/vms/xnettcp/vms/process/TcpServerRecvDataProcess.java

@@ -12,6 +12,7 @@ import com.its.vms.xnettcp.vms.protocol.impl.VmsResFramePacket;
 import com.its.vms.xnettcp.vms.protocol.impl.dle.VmsDleFramePacket;
 import com.its.vms.xnettcp.vms.protocol.impl.std.VmsStdFramePacket;
 import io.netty.channel.ChannelHandlerContext;
+import io.netty.util.ReferenceCountUtil;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.slf4j.MDC;
@@ -158,6 +159,7 @@ public class TcpServerRecvDataProcess {
         } catch (Exception e) {
             log.error("TcpServerRecvDataProcess.process: Exception: {}, {}", opCode, e.toString());
         }
+        ReferenceCountUtil.release(data);   // 20240213, io.netty.util.internal.OutOfDirectMemoryError
     }
 
     /*