From 3d1f4a93071ffa5c58a269a87bf76e063492e9a5 Mon Sep 17 00:00:00 2001 From: jangdongsin Date: Sat, 28 Dec 2024 22:03:59 +0900 Subject: [PATCH] =?UTF-8?q?Kill=20shutdown=20Thread=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/config/RunnerConfiguration.java | 52 +++++++++++++ .../server/server/service/CollectServer.java | 5 +- .../server/server/service/Service.java | 76 ++++++++++++++++++- .../server/service/ShutdownService.java | 15 ++++ 4 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/munjaon/server/server/service/ShutdownService.java diff --git a/src/main/java/com/munjaon/server/config/RunnerConfiguration.java b/src/main/java/com/munjaon/server/config/RunnerConfiguration.java index e37fde7..7f2f6c0 100644 --- a/src/main/java/com/munjaon/server/config/RunnerConfiguration.java +++ b/src/main/java/com/munjaon/server/config/RunnerConfiguration.java @@ -53,6 +53,10 @@ public class RunnerConfiguration { 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 @@ public class RunnerConfiguration { 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 @@ public class RunnerConfiguration { 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 @@ public class RunnerConfiguration { 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 @@ public class RunnerConfiguration { 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 @@ public class RunnerConfiguration { 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 @@ public class RunnerConfiguration { 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 @@ public class RunnerConfiguration { 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 @@ public class RunnerConfiguration { 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 @@ public class RunnerConfiguration { 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 @@ public class RunnerConfiguration { 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 @@ public class RunnerConfiguration { 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 @@ public class RunnerConfiguration { 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); diff --git a/src/main/java/com/munjaon/server/server/service/CollectServer.java b/src/main/java/com/munjaon/server/server/service/CollectServer.java index fff3182..703a035 100644 --- a/src/main/java/com/munjaon/server/server/service/CollectServer.java +++ b/src/main/java/com/munjaon/server/server/service/CollectServer.java @@ -72,7 +72,10 @@ public class CollectServer extends Service { } private void closeCollectChannel() throws IOException { - selector.close(); + if (selector != null) { + selector.close(); + selector = null; + } } @Override diff --git a/src/main/java/com/munjaon/server/server/service/Service.java b/src/main/java/com/munjaon/server/server/service/Service.java index 2634ed3..2765c8d 100644 --- a/src/main/java/com/munjaon/server/server/service/Service.java +++ b/src/main/java/com/munjaon/server/server/service/Service.java @@ -16,11 +16,17 @@ public abstract class Service extends Thread { 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) { super(serviceName); @@ -52,7 +58,7 @@ public abstract class Service extends Thread { } 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 @@ public abstract class Service extends Thread { @Override public void run() { - while (true) { + while (!IS_KILL_YN) { + IS_STOP_YN = false; try { /* 1. 서비스간의 dependency에 따른 체크 */ checkReady(); @@ -122,10 +129,75 @@ public abstract class Service extends Thread { /* 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(); } } diff --git a/src/main/java/com/munjaon/server/server/service/ShutdownService.java b/src/main/java/com/munjaon/server/server/service/ShutdownService.java new file mode 100644 index 0000000..7b8856c --- /dev/null +++ b/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(); + } +}