--- src/main/java/com/munjaon/client/config/RunnerConfiguration.java
+++ src/main/java/com/munjaon/client/config/RunnerConfiguration.java
... | ... | @@ -1,9 +1,6 @@ |
| 1 | 1 |
package com.munjaon.client.config; |
| 2 | 2 |
|
| 3 |
-import com.munjaon.client.server.service.CollectClientService; |
|
| 4 |
-import com.munjaon.client.server.service.DataMoveService; |
|
| 5 |
-import com.munjaon.client.server.service.PropertyLoader; |
|
| 6 |
-import com.munjaon.client.server.service.ReportClientService; |
|
| 3 |
+import com.munjaon.client.server.service.*; |
|
| 7 | 4 |
import lombok.RequiredArgsConstructor; |
| 8 | 5 |
import lombok.extern.slf4j.Slf4j; |
| 9 | 6 |
import org.apache.commons.configuration2.ex.ConfigurationException; |
... | ... | @@ -49,6 +46,10 @@ |
| 49 | 46 |
String serviceName = "SMS"; |
| 50 | 47 |
String serviceType = serverConfig.getString(serviceName + ".SERVICE_TYPE"); |
| 51 | 48 |
CollectClientService collectClientService = new CollectClientService(serviceName, serviceType); |
| 49 |
+ |
|
| 50 |
+ ShutdownService shutdownService = new ShutdownService(collectClientService); |
|
| 51 |
+ Runtime.getRuntime().addShutdownHook(shutdownService); |
|
| 52 |
+ |
|
| 52 | 53 |
collectClientService.start(); |
| 53 | 54 |
} catch (Exception e) {
|
| 54 | 55 |
throw new RuntimeException(e); |
... | ... | @@ -68,6 +69,10 @@ |
| 68 | 69 |
String serviceName = "LMS"; |
| 69 | 70 |
String serviceType = serverConfig.getString(serviceName + ".SERVICE_TYPE"); |
| 70 | 71 |
CollectClientService collectClientService = new CollectClientService(serviceName, serviceType); |
| 72 |
+ |
|
| 73 |
+ ShutdownService shutdownService = new ShutdownService(collectClientService); |
|
| 74 |
+ Runtime.getRuntime().addShutdownHook(shutdownService); |
|
| 75 |
+ |
|
| 71 | 76 |
collectClientService.start(); |
| 72 | 77 |
} catch (Exception e) {
|
| 73 | 78 |
throw new RuntimeException(e); |
... | ... | @@ -87,6 +92,10 @@ |
| 87 | 92 |
String serviceName = "MMS"; |
| 88 | 93 |
String serviceType = serverConfig.getString(serviceName + ".SERVICE_TYPE"); |
| 89 | 94 |
CollectClientService collectClientService = new CollectClientService(serviceName, serviceType); |
| 95 |
+ |
|
| 96 |
+ ShutdownService shutdownService = new ShutdownService(collectClientService); |
|
| 97 |
+ Runtime.getRuntime().addShutdownHook(shutdownService); |
|
| 98 |
+ |
|
| 90 | 99 |
collectClientService.start(); |
| 91 | 100 |
} catch (Exception e) {
|
| 92 | 101 |
throw new RuntimeException(e); |
... | ... | @@ -106,6 +115,10 @@ |
| 106 | 115 |
String serviceName = "KAT"; |
| 107 | 116 |
String serviceType = serverConfig.getString(serviceName + ".SERVICE_TYPE"); |
| 108 | 117 |
CollectClientService collectClientService = new CollectClientService(serviceName, serviceType); |
| 118 |
+ |
|
| 119 |
+ ShutdownService shutdownService = new ShutdownService(collectClientService); |
|
| 120 |
+ Runtime.getRuntime().addShutdownHook(shutdownService); |
|
| 121 |
+ |
|
| 109 | 122 |
collectClientService.start(); |
| 110 | 123 |
} catch (Exception e) {
|
| 111 | 124 |
throw new RuntimeException(e); |
... | ... | @@ -125,6 +138,10 @@ |
| 125 | 138 |
String serviceName = "KFT"; |
| 126 | 139 |
String serviceType = serverConfig.getString(serviceName + ".SERVICE_TYPE"); |
| 127 | 140 |
CollectClientService collectClientService = new CollectClientService(serviceName, serviceType); |
| 141 |
+ |
|
| 142 |
+ ShutdownService shutdownService = new ShutdownService(collectClientService); |
|
| 143 |
+ Runtime.getRuntime().addShutdownHook(shutdownService); |
|
| 144 |
+ |
|
| 128 | 145 |
collectClientService.start(); |
| 129 | 146 |
} catch (Exception e) {
|
| 130 | 147 |
throw new RuntimeException(e); |
... | ... | @@ -143,6 +160,10 @@ |
| 143 | 160 |
try {
|
| 144 | 161 |
String serviceName = "REPORT"; |
| 145 | 162 |
ReportClientService reportClientService = new ReportClientService(serviceName); |
| 163 |
+ |
|
| 164 |
+ ShutdownService shutdownService = new ShutdownService(reportClientService); |
|
| 165 |
+ Runtime.getRuntime().addShutdownHook(shutdownService); |
|
| 166 |
+ |
|
| 146 | 167 |
reportClientService.start(); |
| 147 | 168 |
} catch (Exception e) {
|
| 148 | 169 |
throw new RuntimeException(e); |
... | ... | @@ -161,6 +182,10 @@ |
| 161 | 182 |
try {
|
| 162 | 183 |
String serviceName = "LOG_MOVE"; |
| 163 | 184 |
DataMoveService dataMoveService = new DataMoveService(serviceName); |
| 185 |
+ |
|
| 186 |
+ ShutdownService shutdownService = new ShutdownService(dataMoveService); |
|
| 187 |
+ Runtime.getRuntime().addShutdownHook(shutdownService); |
|
| 188 |
+ |
|
| 164 | 189 |
dataMoveService.start(); |
| 165 | 190 |
} catch (Exception e) {
|
| 166 | 191 |
throw new RuntimeException(e); |
--- src/main/java/com/munjaon/client/server/service/Service.java
+++ src/main/java/com/munjaon/client/server/service/Service.java
... | ... | @@ -14,10 +14,15 @@ |
| 14 | 14 |
protected LogUtil logger; |
| 15 | 15 |
private Long LAST_PROPERTY_LOAD_TIME = 0L; |
| 16 | 16 |
|
| 17 |
+ /** 서비스 종료여부를 체크하는 변수 */ |
|
| 18 |
+ boolean bEndProcess = false; |
|
| 19 |
+ |
|
| 17 | 20 |
protected boolean IS_SERVER_RUN; // 서버가 구동중인지 여부 |
| 18 | 21 |
protected boolean IS_READY_YN; // 서비스 구동준비가 완료되었는지 체크 |
| 19 | 22 |
protected boolean IS_RUN_YN; // 서비스 실행여부 |
| 20 |
- protected boolean IS_STOP_YN; // 서비스 Stop여부 |
|
| 23 |
+ protected boolean IS_STOP_YN; // SERVICE IS TERMINATED >> EXCEPTION, 서비스 Stop여부 |
|
| 24 |
+ /** SERVICE KILL SYGNAL IS RECEIVED */ |
|
| 25 |
+ protected boolean IS_KILL_YN = false; |
|
| 21 | 26 |
|
| 22 | 27 |
public Service() {}
|
| 23 | 28 |
public Service(String serviceName) {
|
... | ... | @@ -74,7 +79,7 @@ |
| 74 | 79 |
* @return |
| 75 | 80 |
*/ |
| 76 | 81 |
public boolean isRun() {
|
| 77 |
- return IS_SERVER_RUN && IS_RUN_YN && !IS_STOP_YN; |
|
| 82 |
+ return IS_SERVER_RUN && IS_RUN_YN && !IS_STOP_YN && !IS_KILL_YN; |
|
| 78 | 83 |
} |
| 79 | 84 |
|
| 80 | 85 |
/** |
... | ... | @@ -144,12 +149,36 @@ |
| 144 | 149 |
saveSystemLog("Service Log Initializing ... ...");
|
| 145 | 150 |
} |
| 146 | 151 |
|
| 152 |
+ public synchronized void Start() {
|
|
| 153 |
+ super.start(); |
|
| 154 |
+ } |
|
| 155 |
+ |
|
| 156 |
+ protected synchronized void Stop() {
|
|
| 157 |
+ IS_STOP_YN = true; |
|
| 158 |
+ saveSystemLog("Service Stoping...");
|
|
| 159 |
+ } |
|
| 160 |
+ |
|
| 161 |
+ protected synchronized void kill() {
|
|
| 162 |
+ if( !IS_KILL_YN ) saveSystemLog("Service Killing...");
|
|
| 163 |
+ |
|
| 164 |
+ IS_KILL_YN = true; |
|
| 165 |
+ } |
|
| 166 |
+ |
|
| 167 |
+ protected void startService() throws Exception {
|
|
| 168 |
+ saveLog("startService() called.");
|
|
| 169 |
+ } |
|
| 170 |
+ |
|
| 171 |
+ protected void stopService() throws Exception {
|
|
| 172 |
+ saveLog("stopService() called.");
|
|
| 173 |
+ } |
|
| 174 |
+ |
|
| 147 | 175 |
/** |
| 148 | 176 |
* 서비스 실행 |
| 149 | 177 |
*/ |
| 150 | 178 |
@Override |
| 151 | 179 |
public void run() {
|
| 152 |
- while (true) {
|
|
| 180 |
+ while (!IS_KILL_YN) {
|
|
| 181 |
+ IS_STOP_YN = false; |
|
| 153 | 182 |
try {
|
| 154 | 183 |
/* 1. 서비스간의 dependency에 따른 체크 */ |
| 155 | 184 |
checkReady(); |
... | ... | @@ -169,20 +198,53 @@ |
| 169 | 198 |
} else {
|
| 170 | 199 |
saveSystemLog("[SERVICE IS NOT RUNNING] [... ...]");
|
| 171 | 200 |
} |
| 172 |
- /* 6. 3초간 sleep */ |
|
| 173 |
- Thread.sleep(3000); |
|
| 174 | 201 |
} catch (Exception e) {
|
| 202 |
+ IS_STOP_YN = true; |
|
| 175 | 203 |
saveSystemLog(e); |
| 176 |
- try {
|
|
| 177 |
- Thread.sleep(3000); |
|
| 178 |
- } catch (InterruptedException ex) {
|
|
| 179 |
- throw new RuntimeException(ex); |
|
| 180 |
- } |
|
| 181 | 204 |
} finally {
|
| 182 | 205 |
if(logger != null) { logger.close(); logger = null; }
|
| 183 | 206 |
/* Exception 발생 대비 자원 해제 */ |
| 184 | 207 |
releaseResources(); |
| 185 | 208 |
} |
| 209 |
+ |
|
| 210 |
+ if(!IS_KILL_YN) {
|
|
| 211 |
+ try {
|
|
| 212 |
+ Thread.sleep(3000); |
|
| 213 |
+ } catch(Exception e) {
|
|
| 214 |
+ } |
|
| 215 |
+ } |
|
| 216 |
+ } |
|
| 217 |
+ |
|
| 218 |
+ try {
|
|
| 219 |
+ stopService(); |
|
| 220 |
+ } catch (Exception e) {
|
|
| 221 |
+ |
|
| 222 |
+ } |
|
| 223 |
+ bEndProcess = true; |
|
| 224 |
+ } |
|
| 225 |
+ |
|
| 226 |
+ public void stopThread() {
|
|
| 227 |
+ bEndProcess = true; |
|
| 228 |
+ kill(); |
|
| 229 |
+ |
|
| 230 |
+ int i=0; |
|
| 231 |
+ while(true) {
|
|
| 232 |
+ try {
|
|
| 233 |
+ Thread.sleep(1*1000); |
|
| 234 |
+ } catch(InterruptedException e) {
|
|
| 235 |
+ e.printStackTrace(); |
|
| 236 |
+ } |
|
| 237 |
+ |
|
| 238 |
+ if(bEndProcess) {
|
|
| 239 |
+ break; |
|
| 240 |
+ } |
|
| 241 |
+ } |
|
| 242 |
+ |
|
| 243 |
+ this.interrupt(); |
|
| 244 |
+ try {
|
|
| 245 |
+ Thread.sleep(100); |
|
| 246 |
+ } catch(InterruptedException e) {
|
|
| 247 |
+ e.printStackTrace(); |
|
| 186 | 248 |
} |
| 187 | 249 |
} |
| 188 | 250 |
|
... | ... | @@ -196,4 +258,4 @@ |
| 196 | 258 |
public abstract void doService(); |
| 197 | 259 |
/* 모니터링을 위한 메소드 */ |
| 198 | 260 |
public abstract JSONObject monitorService(); |
| 199 |
-} |
|
| 261 |
+}(No newline at end of file) |
+++ src/main/java/com/munjaon/client/server/service/ShutdownService.java
... | ... | @@ -0,0 +1,15 @@ |
| 1 | +package com.munjaon.client.server.service; | |
| 2 | + | |
| 3 | +public class ShutdownService extends Thread { | |
| 4 | + private Service service = null; | |
| 5 | + | |
| 6 | + public ShutdownService(Service service) { | |
| 7 | + super(); | |
| 8 | + this.service = service; | |
| 9 | + } | |
| 10 | + | |
| 11 | + @Override | |
| 12 | + public void run() { | |
| 13 | + service.stopThread(); | |
| 14 | + } | |
| 15 | +} |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?