티스토리 뷰

 

java로 graceful shutdown 구현하기 2탄이다.

 

https://flowingmooon.tistory.com/5

 

java로 Graceful shutdown 구현하기

Graceful shutdown이란, 말 그대로 '우아한 끝내기'이다. 프로그램은 잘 돌아가는 것도 중요하지만 잘 죽는 것(?) 또한 중요하다. 어떤 코드를 수행하다 종료 명령이 떨어졌을 때 (ctrl + c) 하던 작업을 잘 마무..

flowingmooon.tistory.com

 

 

 

graceful shutdown을 구현하고 나서 이것이 잘 돌아가는지 확인하려면 ctrl+c (kill signal)을 보내야 한다.

만약 linux 였다면 ctrl+c 를 입력하면 되겠지만 eclipse에서는 ctrl+c가 먹히질 않는다.

해결책은 다음과 같다.

먼저 main()에 아래 코드를 추가해준다.

 

 

 

 

그리고 eclipse 상위 탭에서

Run - Run Configurations에 들어간다.

그리고 Environment에서 다음과 같은 variable을 만들어 준다.

 

 

그리고 run을 하고 console 창을 클릭, enter를 치면 프로그램이 종료된다.

linux에서 ctrl+c를 입력한 것과 동일한 효과를 줄 수 있다.

 

 

 

Console창에서 엔터키를 입력하면 shutdown이 시작되는 것을 볼 수 있다.

 

 

 

 

 

위 콘솔 창을 보면 종료 signal을 받은 이후에도 프로그램이 하던 일을 모두 수행한 후 프로그램을 종료하는 것을 볼 수 있다.

즉, graceful shutdown이 이루어지고 있는 것이다!

 

 

 

 

 

혹시 몰라서 main() 코드를 첨부한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package dajaba;
 
 
public class DajabaBot {
 
    //public static void startWork(String dataBaseDriverPath, String dataBaseName,Logger logger, DajabaThread exThread) {
    //}
    
    public static void main(String[] args) {
        
        Logger logger = Logger.getLogger(DajabaBot.class);
        Setting setter = new Setting();
        try {
            setter.getPropValues();
            logger.info("setting is done");
        } catch (IOException e) {
            logger.error("sth wrong in reading configFile", e);
            return;
        }
        Thread DT = new Thread(new DajabaThread(setter.getProgramCycle(),setter.getDatabaseDriverPath(), setter.getDatabaseName(), setter.getDatabasePath(),setter.getHtmlFilePath(),logger), "DajabaBot");
        DT.start();
    
        
         if (Boolean.parseBoolean(System.getenv("RUNNING_IN_ECLIPSE"))) {        
            System.out.println("You're using Eclipse; click in this console and " +
                    "press ENTER to call System.exit() and run the shutdown routine.");
            try {
                System.in.read();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.exit(0);
         
        
        }
        
        // ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        // scheduler.scheduleAtFixedRate(new DajabaThread(setter, logger), 0,
        // setter.getProgramCycle(), TimeUnit.SECONDS);
        
    
 
    }    
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

 

 

github

https://github.com/bluewink/19winter_internship

 

참고 출처

https://stackoverflow.com/questions/547169/how-to-get-shutdown-hook-to-execute-on-a-process-launched-from-eclipse/559720

 

'PROJECT' 카테고리의 다른 글

java로 Graceful shutdown 구현하기  (0) 2020.03.30
AWS로 나만의 서버 만들기 (feat. EC2, PuTTY)  (0) 2020.03.28
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함