diff --git a/src/main/java/com/munjaon/client/config/RunnerConfiguration.java b/src/main/java/com/munjaon/client/config/RunnerConfiguration.java index e767b7a..7e01402 100644 --- a/src/main/java/com/munjaon/client/config/RunnerConfiguration.java +++ b/src/main/java/com/munjaon/client/config/RunnerConfiguration.java @@ -1,9 +1,6 @@ package com.munjaon.client.config; -import com.munjaon.client.server.service.CollectClientService; -import com.munjaon.client.server.service.DataMoveService; -import com.munjaon.client.server.service.PropertyLoader; -import com.munjaon.client.server.service.ReportClientService; +import com.munjaon.client.server.service.*; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.configuration2.ex.ConfigurationException; @@ -49,6 +46,10 @@ public class RunnerConfiguration { String serviceName = "SMS"; String serviceType = serverConfig.getString(serviceName + ".SERVICE_TYPE"); CollectClientService collectClientService = new CollectClientService(serviceName, serviceType); + + ShutdownService shutdownService = new ShutdownService(collectClientService); + Runtime.getRuntime().addShutdownHook(shutdownService); + collectClientService.start(); } catch (Exception e) { throw new RuntimeException(e); @@ -68,6 +69,10 @@ public class RunnerConfiguration { String serviceName = "LMS"; String serviceType = serverConfig.getString(serviceName + ".SERVICE_TYPE"); CollectClientService collectClientService = new CollectClientService(serviceName, serviceType); + + ShutdownService shutdownService = new ShutdownService(collectClientService); + Runtime.getRuntime().addShutdownHook(shutdownService); + collectClientService.start(); } catch (Exception e) { throw new RuntimeException(e); @@ -87,6 +92,10 @@ public class RunnerConfiguration { String serviceName = "MMS"; String serviceType = serverConfig.getString(serviceName + ".SERVICE_TYPE"); CollectClientService collectClientService = new CollectClientService(serviceName, serviceType); + + ShutdownService shutdownService = new ShutdownService(collectClientService); + Runtime.getRuntime().addShutdownHook(shutdownService); + collectClientService.start(); } catch (Exception e) { throw new RuntimeException(e); @@ -106,6 +115,10 @@ public class RunnerConfiguration { String serviceName = "KAT"; String serviceType = serverConfig.getString(serviceName + ".SERVICE_TYPE"); CollectClientService collectClientService = new CollectClientService(serviceName, serviceType); + + ShutdownService shutdownService = new ShutdownService(collectClientService); + Runtime.getRuntime().addShutdownHook(shutdownService); + collectClientService.start(); } catch (Exception e) { throw new RuntimeException(e); @@ -125,6 +138,10 @@ public class RunnerConfiguration { String serviceName = "KFT"; String serviceType = serverConfig.getString(serviceName + ".SERVICE_TYPE"); CollectClientService collectClientService = new CollectClientService(serviceName, serviceType); + + ShutdownService shutdownService = new ShutdownService(collectClientService); + Runtime.getRuntime().addShutdownHook(shutdownService); + collectClientService.start(); } catch (Exception e) { throw new RuntimeException(e); @@ -143,6 +160,10 @@ public class RunnerConfiguration { try { String serviceName = "REPORT"; ReportClientService reportClientService = new ReportClientService(serviceName); + + ShutdownService shutdownService = new ShutdownService(reportClientService); + Runtime.getRuntime().addShutdownHook(shutdownService); + reportClientService.start(); } catch (Exception e) { throw new RuntimeException(e); @@ -161,6 +182,10 @@ public class RunnerConfiguration { try { String serviceName = "LOG_MOVE"; DataMoveService dataMoveService = new DataMoveService(serviceName); + + ShutdownService shutdownService = new ShutdownService(dataMoveService); + Runtime.getRuntime().addShutdownHook(shutdownService); + dataMoveService.start(); } catch (Exception e) { throw new RuntimeException(e); diff --git a/src/main/java/com/munjaon/client/server/service/Service.java b/src/main/java/com/munjaon/client/server/service/Service.java index d2b6aa6..683bd24 100644 --- a/src/main/java/com/munjaon/client/server/service/Service.java +++ b/src/main/java/com/munjaon/client/server/service/Service.java @@ -14,10 +14,15 @@ 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; // 서비스 Stop여부 + protected boolean IS_STOP_YN; // SERVICE IS TERMINATED >> EXCEPTION, 서비스 Stop여부 + /** SERVICE KILL SYGNAL IS RECEIVED */ + protected boolean IS_KILL_YN = false; public Service() {} public Service(String serviceName) { @@ -74,7 +79,7 @@ public abstract class Service extends Thread { * @return */ 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; } /** @@ -144,12 +149,36 @@ public abstract class Service extends Thread { saveSystemLog("Service Log Initializing ... ..."); } + 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."); + } + /** * 서비스 실행 */ @Override public void run() { - while (true) { + while (!IS_KILL_YN) { + IS_STOP_YN = false; try { /* 1. 서비스간의 dependency에 따른 체크 */ checkReady(); @@ -169,20 +198,53 @@ public abstract class Service extends Thread { } else { saveSystemLog("[SERVICE IS NOT RUNNING] [... ...]"); } - /* 6. 3초간 sleep */ - Thread.sleep(3000); } catch (Exception e) { + IS_STOP_YN = true; saveSystemLog(e); - try { - Thread.sleep(3000); - } catch (InterruptedException ex) { - throw new RuntimeException(ex); - } } 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 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(); } } @@ -196,4 +258,4 @@ public abstract class Service extends Thread { public abstract void doService(); /* 모니터링을 위한 메소드 */ public abstract JSONObject monitorService(); -} +} \ No newline at end of file diff --git a/src/main/java/com/munjaon/client/server/service/ShutdownService.java b/src/main/java/com/munjaon/client/server/service/ShutdownService.java new file mode 100644 index 0000000..4b19e54 --- /dev/null +++ b/src/main/java/com/munjaon/client/server/service/ShutdownService.java @@ -0,0 +1,15 @@ +package com.munjaon.client.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(); + } +}