장동신 장동신 2024-12-28
Kill shutdown Thread 적용
@3d1f4a93071ffa5c58a269a87bf76e063492e9a5
src/main/java/com/munjaon/server/config/RunnerConfiguration.java
--- src/main/java/com/munjaon/server/config/RunnerConfiguration.java
+++ src/main/java/com/munjaon/server/config/RunnerConfiguration.java
@@ -53,6 +53,10 @@
                         SmsWriteQueue smsWriteQueue = new SmsWriteQueue(queueInfo);
                         SmsReadQueue smsReadQueue = new SmsReadQueue(queueInfo);
                         QueueServerService queueServerService = new QueueServerService(svc, smsWriteQueue, smsReadQueue);
+
+                        ShutdownService shutdownService = new ShutdownService(queueServerService);
+                        Runtime.getRuntime().addShutdownHook(shutdownService);
+
                         queueServerService.start();
                     }
                 }
@@ -80,6 +84,10 @@
                         LmsWriteQueue lmsWriteQueue = new LmsWriteQueue(queueInfo);
                         LmsReadQueue lmsReadQueue = new LmsReadQueue(queueInfo);
                         QueueServerService queueServerService = new QueueServerService(svc, lmsWriteQueue, lmsReadQueue);
+
+                        ShutdownService shutdownService = new ShutdownService(queueServerService);
+                        Runtime.getRuntime().addShutdownHook(shutdownService);
+
                         queueServerService.start();
                     }
                 }
@@ -107,6 +115,10 @@
                         MmsWriteQueue mmsWriteQueue = new MmsWriteQueue(queueInfo);
                         MmsReadQueue mmsReadQueue = new MmsReadQueue(queueInfo);
                         QueueServerService queueServerService = new QueueServerService(svc, mmsWriteQueue, mmsReadQueue);
+
+                        ShutdownService shutdownService = new ShutdownService(queueServerService);
+                        Runtime.getRuntime().addShutdownHook(shutdownService);
+
                         queueServerService.start();
                     }
                 }
@@ -134,6 +146,10 @@
                         KakaoAlarmWriteQueue katWriteQueue = new KakaoAlarmWriteQueue(queueInfo);
                         KakaoAlarmReadQueue katReadQueue = new KakaoAlarmReadQueue(queueInfo);
                         QueueServerService queueServerService = new QueueServerService(svc, katWriteQueue, katReadQueue);
+
+                        ShutdownService shutdownService = new ShutdownService(queueServerService);
+                        Runtime.getRuntime().addShutdownHook(shutdownService);
+
                         queueServerService.start();
                     }
                 }
@@ -161,6 +177,10 @@
                         KakaoFriendWriteQueue kftWriteQueue = new KakaoFriendWriteQueue(queueInfo);
                         KakaoFriendReadQueue kftReadQueue = new KakaoFriendReadQueue(queueInfo);
                         QueueServerService queueServerService = new QueueServerService(svc, kftWriteQueue, kftReadQueue);
+
+                        ShutdownService shutdownService = new ShutdownService(queueServerService);
+                        Runtime.getRuntime().addShutdownHook(shutdownService);
+
                         queueServerService.start();
                     }
                 }
@@ -180,6 +200,10 @@
             int port = serverConfig.getInt(serviceName + ".SERVICE_PORT");
 //            CollectBackServerService collectServerService = new CollectBackServerService(serviceName, serviceType, port);
             CollectServer collectServer = new CollectServer(serviceName, serviceType, port);
+
+            ShutdownService shutdownService = new ShutdownService(collectServer);
+            Runtime.getRuntime().addShutdownHook(shutdownService);
+
             collectServer.start();
         } catch (Exception e) {
             throw new RuntimeException(e);
@@ -196,6 +220,10 @@
             int port = serverConfig.getInt(serviceName + ".SERVICE_PORT");
 //            CollectBackServerService collectServerService = new CollectBackServerService(serviceName, serviceType, port);
             CollectServer collectServer = new CollectServer(serviceName, serviceType, port);
+
+            ShutdownService shutdownService = new ShutdownService(collectServer);
+            Runtime.getRuntime().addShutdownHook(shutdownService);
+
             collectServer.start();
         } catch (Exception e) {
             throw new RuntimeException(e);
@@ -212,6 +240,10 @@
             int port = serverConfig.getInt(serviceName + ".SERVICE_PORT");
 //            CollectBackServerService collectServerService = new CollectBackServerService(serviceName, serviceType, port);
             CollectServer collectServer = new CollectServer(serviceName, serviceType, port);
+
+            ShutdownService shutdownService = new ShutdownService(collectServer);
+            Runtime.getRuntime().addShutdownHook(shutdownService);
+
             collectServer.start();
         } catch (Exception e) {
             throw new RuntimeException(e);
@@ -228,6 +260,10 @@
             int port = serverConfig.getInt(serviceName + ".SERVICE_PORT");
 //            CollectBackServerService collectServerService = new CollectBackServerService(serviceName, serviceType, port);
             CollectServer collectServer = new CollectServer(serviceName, serviceType, port);
+
+            ShutdownService shutdownService = new ShutdownService(collectServer);
+            Runtime.getRuntime().addShutdownHook(shutdownService);
+
             collectServer.start();
         } catch (Exception e) {
             throw new RuntimeException(e);
@@ -244,6 +280,10 @@
             int port = serverConfig.getInt(serviceName + ".SERVICE_PORT");
 //            CollectBackServerService collectServerService = new CollectBackServerService(serviceName, serviceType, port);
             CollectServer collectServer = new CollectServer(serviceName, serviceType, port);
+
+            ShutdownService shutdownService = new ShutdownService(collectServer);
+            Runtime.getRuntime().addShutdownHook(shutdownService);
+
             collectServer.start();
         } catch (Exception e) {
             throw new RuntimeException(e);
@@ -258,6 +298,10 @@
             String serviceName = "REPORTER";
             int port = serverConfig.getInt(serviceName + ".SERVICE_PORT");
             ReportServer reportServer = new ReportServer(serviceName, port);
+
+            ShutdownService shutdownService = new ShutdownService(reportServer);
+            Runtime.getRuntime().addShutdownHook(shutdownService);
+
             reportServer.start();
         } catch (Exception e) {
             throw new RuntimeException(e);
@@ -271,6 +315,10 @@
         try {
             String serviceName = "REPORT_QUEUE";
             ReportQueueServer reportQueueServer = new ReportQueueServer(serviceName);
+
+            ShutdownService shutdownService = new ShutdownService(reportQueueServer);
+            Runtime.getRuntime().addShutdownHook(shutdownService);
+
             reportQueueServer.start();
         } catch (Exception e) {
             throw new RuntimeException(e);
@@ -284,6 +332,10 @@
         try {
             String serviceName = "HEALTH";
             HealthCheckServer healthCheckServer = new HealthCheckServer(serviceName);
+
+            ShutdownService shutdownService = new ShutdownService(healthCheckServer);
+            Runtime.getRuntime().addShutdownHook(shutdownService);
+
             healthCheckServer.start();
         } catch (Exception e) {
             throw new RuntimeException(e);
src/main/java/com/munjaon/server/server/service/CollectServer.java
--- src/main/java/com/munjaon/server/server/service/CollectServer.java
+++ src/main/java/com/munjaon/server/server/service/CollectServer.java
@@ -72,7 +72,10 @@
     }
 
     private void closeCollectChannel() throws IOException {
-        selector.close();
+        if (selector != null) {
+            selector.close();
+            selector = null;
+        }
     }
 
     @Override
src/main/java/com/munjaon/server/server/service/Service.java
--- src/main/java/com/munjaon/server/server/service/Service.java
+++ src/main/java/com/munjaon/server/server/service/Service.java
@@ -16,10 +16,16 @@
     protected LogUtil logger;
     private Long LAST_PROPERTY_LOAD_TIME = 0L;
 
+    /** 서비스 종료여부를 체크하는 변수 */
+    boolean bEndProcess = false;
+
     protected boolean IS_SERVER_RUN;          // 서버가 구동중인지 여부
     protected boolean IS_READY_YN;          // 서비스 구동준비가 완료되었는지 체크
     protected boolean IS_RUN_YN;
     protected boolean IS_STOP_YN;
+
+    /** SERVICE KILL SYGNAL IS RECEIVED */
+    protected boolean IS_KILL_YN = false;
 
     public Service() {}
     public Service(String serviceName) {
@@ -52,7 +58,7 @@
     }
 
     public boolean isRun() {
-        return IS_SERVER_RUN && IS_RUN_YN && !IS_STOP_YN;
+        return IS_SERVER_RUN && IS_RUN_YN && !IS_STOP_YN && !IS_KILL_YN;
     }
 
     public boolean isReady() {
@@ -99,7 +105,8 @@
 
     @Override
     public void run() {
-        while (true) {
+        while (!IS_KILL_YN) {
+            IS_STOP_YN = false;
             try {
                 /* 1. 서비스간의 dependency에 따른 체크 */
                 checkReady();
@@ -122,10 +129,75 @@
                 /* 6. 3초간 sleep */
                 Thread.sleep(3000);
             } catch (Exception e) {
+                IS_STOP_YN = true;
                 saveSystemLog(e);
             } finally {
                 if(logger != null) { logger.close(); logger = null; }
+                /* Exception 발생 대비 자원 해제 */
+                releaseResources();
             }
+
+            if(!IS_KILL_YN) {
+                try {
+                    Thread.sleep(3000);
+                } catch(Exception e) {
+                }
+            }
+
+            try {
+                stopService();
+            } catch (Exception e) {
+
+            }
+            bEndProcess = true;
+        }
+    }
+
+    public synchronized void Start() {
+        super.start();
+    }
+
+    protected synchronized void Stop() {
+        IS_STOP_YN = true;
+        saveSystemLog("Service Stoping...");
+    }
+
+    protected synchronized void kill() {
+        if( !IS_KILL_YN ) saveSystemLog("Service Killing...");
+
+        IS_KILL_YN = true;
+    }
+
+    protected void startService() throws Exception {
+        saveLog("startService() called.");
+    }
+
+    protected void stopService() throws Exception {
+        saveLog("stopService() called.");
+    }
+
+    public void stopThread() {
+        bEndProcess = true;
+        kill();
+
+        int i=0;
+        while(true) {
+            try {
+                Thread.sleep(1*1000);
+            } catch(InterruptedException e) {
+                e.printStackTrace();
+            }
+
+            if(bEndProcess) {
+                break;
+            }
+        }
+
+        this.interrupt();
+        try {
+            Thread.sleep(100);
+        } catch(InterruptedException e) {
+            e.printStackTrace();
         }
     }
 
 
src/main/java/com/munjaon/server/server/service/ShutdownService.java (added)
+++ src/main/java/com/munjaon/server/server/service/ShutdownService.java
@@ -0,0 +1,15 @@
+package com.munjaon.server.server.service;
+
+public class ShutdownService extends Thread {
+    private Service service = null;
+
+    public ShutdownService(Service service) {
+        super();
+        this.service = service;
+    }
+
+    @Override
+    public void run() {
+        service.stopThread();
+    }
+}
Add a comment
List