--- src/main/java/com/munjaon/server/config/RunnerConfiguration.java
+++ src/main/java/com/munjaon/server/config/RunnerConfiguration.java
... | ... | @@ -53,6 +53,10 @@ |
| 53 | 53 |
SmsWriteQueue smsWriteQueue = new SmsWriteQueue(queueInfo); |
| 54 | 54 |
SmsReadQueue smsReadQueue = new SmsReadQueue(queueInfo); |
| 55 | 55 |
QueueServerService queueServerService = new QueueServerService(svc, smsWriteQueue, smsReadQueue); |
| 56 |
+ |
|
| 57 |
+ ShutdownService shutdownService = new ShutdownService(queueServerService); |
|
| 58 |
+ Runtime.getRuntime().addShutdownHook(shutdownService); |
|
| 59 |
+ |
|
| 56 | 60 |
queueServerService.start(); |
| 57 | 61 |
} |
| 58 | 62 |
} |
... | ... | @@ -80,6 +84,10 @@ |
| 80 | 84 |
LmsWriteQueue lmsWriteQueue = new LmsWriteQueue(queueInfo); |
| 81 | 85 |
LmsReadQueue lmsReadQueue = new LmsReadQueue(queueInfo); |
| 82 | 86 |
QueueServerService queueServerService = new QueueServerService(svc, lmsWriteQueue, lmsReadQueue); |
| 87 |
+ |
|
| 88 |
+ ShutdownService shutdownService = new ShutdownService(queueServerService); |
|
| 89 |
+ Runtime.getRuntime().addShutdownHook(shutdownService); |
|
| 90 |
+ |
|
| 83 | 91 |
queueServerService.start(); |
| 84 | 92 |
} |
| 85 | 93 |
} |
... | ... | @@ -107,6 +115,10 @@ |
| 107 | 115 |
MmsWriteQueue mmsWriteQueue = new MmsWriteQueue(queueInfo); |
| 108 | 116 |
MmsReadQueue mmsReadQueue = new MmsReadQueue(queueInfo); |
| 109 | 117 |
QueueServerService queueServerService = new QueueServerService(svc, mmsWriteQueue, mmsReadQueue); |
| 118 |
+ |
|
| 119 |
+ ShutdownService shutdownService = new ShutdownService(queueServerService); |
|
| 120 |
+ Runtime.getRuntime().addShutdownHook(shutdownService); |
|
| 121 |
+ |
|
| 110 | 122 |
queueServerService.start(); |
| 111 | 123 |
} |
| 112 | 124 |
} |
... | ... | @@ -134,6 +146,10 @@ |
| 134 | 146 |
KakaoAlarmWriteQueue katWriteQueue = new KakaoAlarmWriteQueue(queueInfo); |
| 135 | 147 |
KakaoAlarmReadQueue katReadQueue = new KakaoAlarmReadQueue(queueInfo); |
| 136 | 148 |
QueueServerService queueServerService = new QueueServerService(svc, katWriteQueue, katReadQueue); |
| 149 |
+ |
|
| 150 |
+ ShutdownService shutdownService = new ShutdownService(queueServerService); |
|
| 151 |
+ Runtime.getRuntime().addShutdownHook(shutdownService); |
|
| 152 |
+ |
|
| 137 | 153 |
queueServerService.start(); |
| 138 | 154 |
} |
| 139 | 155 |
} |
... | ... | @@ -161,6 +177,10 @@ |
| 161 | 177 |
KakaoFriendWriteQueue kftWriteQueue = new KakaoFriendWriteQueue(queueInfo); |
| 162 | 178 |
KakaoFriendReadQueue kftReadQueue = new KakaoFriendReadQueue(queueInfo); |
| 163 | 179 |
QueueServerService queueServerService = new QueueServerService(svc, kftWriteQueue, kftReadQueue); |
| 180 |
+ |
|
| 181 |
+ ShutdownService shutdownService = new ShutdownService(queueServerService); |
|
| 182 |
+ Runtime.getRuntime().addShutdownHook(shutdownService); |
|
| 183 |
+ |
|
| 164 | 184 |
queueServerService.start(); |
| 165 | 185 |
} |
| 166 | 186 |
} |
... | ... | @@ -180,6 +200,10 @@ |
| 180 | 200 |
int port = serverConfig.getInt(serviceName + ".SERVICE_PORT"); |
| 181 | 201 |
// CollectBackServerService collectServerService = new CollectBackServerService(serviceName, serviceType, port); |
| 182 | 202 |
CollectServer collectServer = new CollectServer(serviceName, serviceType, port); |
| 203 |
+ |
|
| 204 |
+ ShutdownService shutdownService = new ShutdownService(collectServer); |
|
| 205 |
+ Runtime.getRuntime().addShutdownHook(shutdownService); |
|
| 206 |
+ |
|
| 183 | 207 |
collectServer.start(); |
| 184 | 208 |
} catch (Exception e) {
|
| 185 | 209 |
throw new RuntimeException(e); |
... | ... | @@ -196,6 +220,10 @@ |
| 196 | 220 |
int port = serverConfig.getInt(serviceName + ".SERVICE_PORT"); |
| 197 | 221 |
// CollectBackServerService collectServerService = new CollectBackServerService(serviceName, serviceType, port); |
| 198 | 222 |
CollectServer collectServer = new CollectServer(serviceName, serviceType, port); |
| 223 |
+ |
|
| 224 |
+ ShutdownService shutdownService = new ShutdownService(collectServer); |
|
| 225 |
+ Runtime.getRuntime().addShutdownHook(shutdownService); |
|
| 226 |
+ |
|
| 199 | 227 |
collectServer.start(); |
| 200 | 228 |
} catch (Exception e) {
|
| 201 | 229 |
throw new RuntimeException(e); |
... | ... | @@ -212,6 +240,10 @@ |
| 212 | 240 |
int port = serverConfig.getInt(serviceName + ".SERVICE_PORT"); |
| 213 | 241 |
// CollectBackServerService collectServerService = new CollectBackServerService(serviceName, serviceType, port); |
| 214 | 242 |
CollectServer collectServer = new CollectServer(serviceName, serviceType, port); |
| 243 |
+ |
|
| 244 |
+ ShutdownService shutdownService = new ShutdownService(collectServer); |
|
| 245 |
+ Runtime.getRuntime().addShutdownHook(shutdownService); |
|
| 246 |
+ |
|
| 215 | 247 |
collectServer.start(); |
| 216 | 248 |
} catch (Exception e) {
|
| 217 | 249 |
throw new RuntimeException(e); |
... | ... | @@ -228,6 +260,10 @@ |
| 228 | 260 |
int port = serverConfig.getInt(serviceName + ".SERVICE_PORT"); |
| 229 | 261 |
// CollectBackServerService collectServerService = new CollectBackServerService(serviceName, serviceType, port); |
| 230 | 262 |
CollectServer collectServer = new CollectServer(serviceName, serviceType, port); |
| 263 |
+ |
|
| 264 |
+ ShutdownService shutdownService = new ShutdownService(collectServer); |
|
| 265 |
+ Runtime.getRuntime().addShutdownHook(shutdownService); |
|
| 266 |
+ |
|
| 231 | 267 |
collectServer.start(); |
| 232 | 268 |
} catch (Exception e) {
|
| 233 | 269 |
throw new RuntimeException(e); |
... | ... | @@ -244,6 +280,10 @@ |
| 244 | 280 |
int port = serverConfig.getInt(serviceName + ".SERVICE_PORT"); |
| 245 | 281 |
// CollectBackServerService collectServerService = new CollectBackServerService(serviceName, serviceType, port); |
| 246 | 282 |
CollectServer collectServer = new CollectServer(serviceName, serviceType, port); |
| 283 |
+ |
|
| 284 |
+ ShutdownService shutdownService = new ShutdownService(collectServer); |
|
| 285 |
+ Runtime.getRuntime().addShutdownHook(shutdownService); |
|
| 286 |
+ |
|
| 247 | 287 |
collectServer.start(); |
| 248 | 288 |
} catch (Exception e) {
|
| 249 | 289 |
throw new RuntimeException(e); |
... | ... | @@ -258,6 +298,10 @@ |
| 258 | 298 |
String serviceName = "REPORTER"; |
| 259 | 299 |
int port = serverConfig.getInt(serviceName + ".SERVICE_PORT"); |
| 260 | 300 |
ReportServer reportServer = new ReportServer(serviceName, port); |
| 301 |
+ |
|
| 302 |
+ ShutdownService shutdownService = new ShutdownService(reportServer); |
|
| 303 |
+ Runtime.getRuntime().addShutdownHook(shutdownService); |
|
| 304 |
+ |
|
| 261 | 305 |
reportServer.start(); |
| 262 | 306 |
} catch (Exception e) {
|
| 263 | 307 |
throw new RuntimeException(e); |
... | ... | @@ -271,6 +315,10 @@ |
| 271 | 315 |
try {
|
| 272 | 316 |
String serviceName = "REPORT_QUEUE"; |
| 273 | 317 |
ReportQueueServer reportQueueServer = new ReportQueueServer(serviceName); |
| 318 |
+ |
|
| 319 |
+ ShutdownService shutdownService = new ShutdownService(reportQueueServer); |
|
| 320 |
+ Runtime.getRuntime().addShutdownHook(shutdownService); |
|
| 321 |
+ |
|
| 274 | 322 |
reportQueueServer.start(); |
| 275 | 323 |
} catch (Exception e) {
|
| 276 | 324 |
throw new RuntimeException(e); |
... | ... | @@ -284,6 +332,10 @@ |
| 284 | 332 |
try {
|
| 285 | 333 |
String serviceName = "HEALTH"; |
| 286 | 334 |
HealthCheckServer healthCheckServer = new HealthCheckServer(serviceName); |
| 335 |
+ |
|
| 336 |
+ ShutdownService shutdownService = new ShutdownService(healthCheckServer); |
|
| 337 |
+ Runtime.getRuntime().addShutdownHook(shutdownService); |
|
| 338 |
+ |
|
| 287 | 339 |
healthCheckServer.start(); |
| 288 | 340 |
} catch (Exception e) {
|
| 289 | 341 |
throw new RuntimeException(e); |
--- src/main/java/com/munjaon/server/server/service/CollectServer.java
+++ src/main/java/com/munjaon/server/server/service/CollectServer.java
... | ... | @@ -72,7 +72,10 @@ |
| 72 | 72 |
} |
| 73 | 73 |
|
| 74 | 74 |
private void closeCollectChannel() throws IOException {
|
| 75 |
- selector.close(); |
|
| 75 |
+ if (selector != null) {
|
|
| 76 |
+ selector.close(); |
|
| 77 |
+ selector = null; |
|
| 78 |
+ } |
|
| 76 | 79 |
} |
| 77 | 80 |
|
| 78 | 81 |
@Override |
--- src/main/java/com/munjaon/server/server/service/Service.java
+++ src/main/java/com/munjaon/server/server/service/Service.java
... | ... | @@ -16,10 +16,16 @@ |
| 16 | 16 |
protected LogUtil logger; |
| 17 | 17 |
private Long LAST_PROPERTY_LOAD_TIME = 0L; |
| 18 | 18 |
|
| 19 |
+ /** 서비스 종료여부를 체크하는 변수 */ |
|
| 20 |
+ boolean bEndProcess = false; |
|
| 21 |
+ |
|
| 19 | 22 |
protected boolean IS_SERVER_RUN; // 서버가 구동중인지 여부 |
| 20 | 23 |
protected boolean IS_READY_YN; // 서비스 구동준비가 완료되었는지 체크 |
| 21 | 24 |
protected boolean IS_RUN_YN; |
| 22 | 25 |
protected boolean IS_STOP_YN; |
| 26 |
+ |
|
| 27 |
+ /** SERVICE KILL SYGNAL IS RECEIVED */ |
|
| 28 |
+ protected boolean IS_KILL_YN = false; |
|
| 23 | 29 |
|
| 24 | 30 |
public Service() {}
|
| 25 | 31 |
public Service(String serviceName) {
|
... | ... | @@ -52,7 +58,7 @@ |
| 52 | 58 |
} |
| 53 | 59 |
|
| 54 | 60 |
public boolean isRun() {
|
| 55 |
- return IS_SERVER_RUN && IS_RUN_YN && !IS_STOP_YN; |
|
| 61 |
+ return IS_SERVER_RUN && IS_RUN_YN && !IS_STOP_YN && !IS_KILL_YN; |
|
| 56 | 62 |
} |
| 57 | 63 |
|
| 58 | 64 |
public boolean isReady() {
|
... | ... | @@ -99,7 +105,8 @@ |
| 99 | 105 |
|
| 100 | 106 |
@Override |
| 101 | 107 |
public void run() {
|
| 102 |
- while (true) {
|
|
| 108 |
+ while (!IS_KILL_YN) {
|
|
| 109 |
+ IS_STOP_YN = false; |
|
| 103 | 110 |
try {
|
| 104 | 111 |
/* 1. 서비스간의 dependency에 따른 체크 */ |
| 105 | 112 |
checkReady(); |
... | ... | @@ -122,10 +129,75 @@ |
| 122 | 129 |
/* 6. 3초간 sleep */ |
| 123 | 130 |
Thread.sleep(3000); |
| 124 | 131 |
} catch (Exception e) {
|
| 132 |
+ IS_STOP_YN = true; |
|
| 125 | 133 |
saveSystemLog(e); |
| 126 | 134 |
} finally {
|
| 127 | 135 |
if(logger != null) { logger.close(); logger = null; }
|
| 136 |
+ /* Exception 발생 대비 자원 해제 */ |
|
| 137 |
+ releaseResources(); |
|
| 128 | 138 |
} |
| 139 |
+ |
|
| 140 |
+ if(!IS_KILL_YN) {
|
|
| 141 |
+ try {
|
|
| 142 |
+ Thread.sleep(3000); |
|
| 143 |
+ } catch(Exception e) {
|
|
| 144 |
+ } |
|
| 145 |
+ } |
|
| 146 |
+ |
|
| 147 |
+ try {
|
|
| 148 |
+ stopService(); |
|
| 149 |
+ } catch (Exception e) {
|
|
| 150 |
+ |
|
| 151 |
+ } |
|
| 152 |
+ bEndProcess = true; |
|
| 153 |
+ } |
|
| 154 |
+ } |
|
| 155 |
+ |
|
| 156 |
+ public synchronized void Start() {
|
|
| 157 |
+ super.start(); |
|
| 158 |
+ } |
|
| 159 |
+ |
|
| 160 |
+ protected synchronized void Stop() {
|
|
| 161 |
+ IS_STOP_YN = true; |
|
| 162 |
+ saveSystemLog("Service Stoping...");
|
|
| 163 |
+ } |
|
| 164 |
+ |
|
| 165 |
+ protected synchronized void kill() {
|
|
| 166 |
+ if( !IS_KILL_YN ) saveSystemLog("Service Killing...");
|
|
| 167 |
+ |
|
| 168 |
+ IS_KILL_YN = true; |
|
| 169 |
+ } |
|
| 170 |
+ |
|
| 171 |
+ protected void startService() throws Exception {
|
|
| 172 |
+ saveLog("startService() called.");
|
|
| 173 |
+ } |
|
| 174 |
+ |
|
| 175 |
+ protected void stopService() throws Exception {
|
|
| 176 |
+ saveLog("stopService() called.");
|
|
| 177 |
+ } |
|
| 178 |
+ |
|
| 179 |
+ public void stopThread() {
|
|
| 180 |
+ bEndProcess = true; |
|
| 181 |
+ kill(); |
|
| 182 |
+ |
|
| 183 |
+ int i=0; |
|
| 184 |
+ while(true) {
|
|
| 185 |
+ try {
|
|
| 186 |
+ Thread.sleep(1*1000); |
|
| 187 |
+ } catch(InterruptedException e) {
|
|
| 188 |
+ e.printStackTrace(); |
|
| 189 |
+ } |
|
| 190 |
+ |
|
| 191 |
+ if(bEndProcess) {
|
|
| 192 |
+ break; |
|
| 193 |
+ } |
|
| 194 |
+ } |
|
| 195 |
+ |
|
| 196 |
+ this.interrupt(); |
|
| 197 |
+ try {
|
|
| 198 |
+ Thread.sleep(100); |
|
| 199 |
+ } catch(InterruptedException e) {
|
|
| 200 |
+ e.printStackTrace(); |
|
| 129 | 201 |
} |
| 130 | 202 |
} |
| 131 | 203 |
|
+++ src/main/java/com/munjaon/server/server/service/ShutdownService.java
... | ... | @@ -0,0 +1,15 @@ |
| 1 | +package com.munjaon.server.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?