Kill shutdown Thread 적용

This commit is contained in:
jangdongsin 2024-12-28 22:03:59 +09:00
parent 2ac5d34bec
commit 3d1f4a9307
4 changed files with 145 additions and 3 deletions

View File

@ -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);

View File

@ -72,7 +72,10 @@ public class CollectServer extends Service {
}
private void closeCollectChannel() throws IOException {
selector.close();
if (selector != null) {
selector.close();
selector = null;
}
}
@Override

View File

@ -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();
}
}

View File

@ -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();
}
}