Site Search :
Standard Enterprise XML Methodology Pattern Setting Tunning Other
Article Contributors
GuestBook
Javapattern Maven
XSourceGen Dev
JetSpeed Test
JLook Image
jLook Family Site


Shutdown hookŬ·¡½º ÀÛ¼º¹æ¹ý ¹× »ùÇÃ
 
½Ã½ºÅÛÀÇ ºñÁ¤»óÁ¾·á½Ã¿¡(Ctrl+C) ¾ÖÇø®ÄÉÀ̼ÇÀÌ ¹«¾ð°¡ ó¸®ÈÄ Á¾·áµÇ¾î¾ß ÇÒ °æ¿ì ±× ¹æ¾È¿¡ ´ëÇÏ¿© ÀÛ¼ºÇÒ ¼ö ÀÖ´Â ¹æ¹ý°ú »ùÇÃÄڵ带 º¸¿©ÁÖµµ·Ï ÇÑ´Ù. ( 2003/07/28 ) 244
Written by ienvyou - ÃÖÁö¿õ
1 of 1
 

À̹ø ¾ÆƼŬ¿¡¼­´Â shutdown hook¿¡ ´ëÇÑ ±ÛÀ» ½áº¸°íÀÚ ÇÑ´Ù. ¿ì¼± °³¿äºÎÅÍ ¸»ÇÏÀÚ¸é
´ç½ÅÀÌ ¿­³ª°Ô ¾ÖÇø®ÄÉÀ̼ÇÀ» ¸¸µé¾ú´Ù°í °¡Á¤À» Çغ¸ÀÚ.
»ç¿ëÀÚ°¡ µµ½ºÃ¢¿¡¼­ ¾ÖÇø®ÄÉÀ̼ÇÀ» ½ÇÇà½ÃÅ°°í µ¿ÀÛ½ÃÅ°°í Àִµ¥, ÀÌ »ç¿ëÀÚ°¡
°©ÀÚ±â ÀÌ°Ô ¾Æ´Ï´Ù ½Í¾î¼­ ¾ÖÇø®ÄÉÀ̼ÇÀ» Á×ÀÌ·Á°í ÇÑ´Ù. 
±Ùµ¥ ºÐ¸í ³»°¡ ¸Þ´º¿¡ Á¾·á¶ó´Â Ç׸ñÀ» Çϳª Ãß°¡ÇØÁÖ¾ú°Å´Ã..
Ctrl+C¸¦ ´­·¯¹ö¸®°í¾ß ¸»¾Ò´Ù.

¹®Á¦´Â ¹«¾úÀΰí? ³ª´Â ÇØ´ç»ç¿ëÀÚÀÇ ³»¿ë¿¡ ´ëÇÑ backward±â´ÉÀ» ÁÖ°í ½Í¾î¼­ 
±× »ç¶÷ÀÌ ÇÏ°í ÀÖ´Â Çൿ¿¡ ´ëÇÑ rollback°¡´ÉÇÑ ·Î±×¸¦ ½×°í »ç¿ëÀÚ°¡ ¾ÖÇø®ÄÉÀ̼ÇÀ»
Á¾·á½ÃÅ°¸é ±×¶§ ÇÊ¿ä¾ø´Â µ¥ÀÌÅ͵éÀ» ¸ðµÎ ¾ø¾Ö¹ö¸®¸®¶ó ¸¶À½À» ¸Ô°í ÀÖ´ø Â÷¿´´Ù.
±×·±µ¥...

À§ÀÇ °°Àº »óȲÀÌ ¹ß»ýÀ» ÇÑ´Ù¸é ´ç½ÅÀº ¾î¶»°Ô ÇÒ°ÍÀΰ¡? 
¼­ºí¸´ ÄÁÅ×À̳ʳª AppletÀÇ °æ¿ìµµ ½ÇÁ¦ ¶óÀÌÇÁ»çÀÌŬ¿¡ ÀÇÇÏ¿© µ¿ÀÛÇÏ¿© ÇØ´ç Ŭ·¡½º°¡
¸Þ¸ð¸®¿¡¼­ unloadµÇ¾îÁö´Â ½ÃÁ¡¿¡ destroy¸Þ¼ÒµåµéÀ» È£ÃâÇϵµ·Ï ÇÏ°í Àִµ¥,
À§ÀÇ ctrl+c¶ó´Â ÇàÀ§´Â ¾Æ¿¹ daemon thread±îÁö "³Ê Á×¾î¶ù~!" Çϸ鼭 º¸³»¹ö¸®´Â ÄÉÀ̽º´Ï ¸»ÀÌ´Ù.

¸¸¾à Àú·¯ÇÑ ¾ÖÇø®ÄÉÀ̼ÇÀ» ½ºÀ®À¸·Î ¸¸µç´Ù¸é actionPerformed À̺¥Æ®¸¦ °É¾î¼­ ¾î¶»°Ôµç
󸮸¦ Çϰڴµ¥ ÀÏ¹Ý text based applicationÀÇ °æ¿ì´Â ´õ´õ¿íÀÌ ¹®Á¦°¡ ¾Æ´Òµí ½Í´Ù.

¢º ÀÚ¹Ù¿¡¼­ÀÇ shutdown eventÀÇ µÎ°¡Áö

ÀÚ¹Ù¿¡¼­ virtual machineÀÌ shutdownÀ» ÀνÄÇÏ´Â ¹æ¹ýÀ¸·Î´Â µÎ°¡Áö°¡ Àִµ¥ 

1. ù¹ø°·Î´Â ¾ÖÇø®ÄÉÀ̼ÇÀÌ System.exit()¸Þ¼Òµå¸¦ ÅëÇÏ¿© Á¤»óÀûÀ¸·Î Á¾·áÇÏ´Â °æ¿ì°¡ ÀÖ°í, 

2. µÎ¹ø°·Î´Â »ç¿ëÀÚ°¡ Á÷Á¢ Ctrl+C¸¦ Å°º¸µå¿¡ ÀÔ·ÂÇÔÀ¸·Î¼­ ÇöÀç ½ÇÇàÁßÀÎ ¾ÖÇø®ÄÉÀ̼ÇÀ»
closingÇÏÁö ¾Ê°í virtual machineÀ» ºñÁ¤»óÀûÀ¸·Î °­Á¦Á¾·á½ÃÅ°´Â °æ¿ìÀÌ´Ù.


°£´ÜÇÏ°Ô shutdown hookÀ» ÀÛ¼ºÇÒ ¼ö ÀÖ´Â ¹æ¹ýÀº ´ÙÀ½°ú °°´Ù.

1. java.lang.Thread¸¦ »ó¼Ó¹Þ´Â Ŭ·¡½º¸¦ Çϳª ÀÛ¼ºÇÑ´Ù.
2. ThreadÀÇ run¸Þ¼Òµå¸¦ ±¸ÇöÇÒ ¶§ ´ç½ÅÀÌ shutdown½Ã¿¡ ó¸®ÇØ¾ß ÇÒ Àϵ鿡 ´ëÇÏ¿© ±â¼úÇÑ´Ù.
3. ÇØ´ç shutdown hookŬ·¡½º¸¦ »ý¼ºÇÑ´Ù.
4. RuntimeŬ·¡½ºÀÇ addShutdownHook()¸Þ¼Òµå¸¦ ÀÌ¿ëÇÏ¿© µî·ÏÇÑ´Ù.
    Runtime.getRuntime().addShutdownHook(shutdownHook);

¢º ShutdownHook SampleÁ¤ÀÇ

¿©±â¼­ ¿ì¼± ¾ÖÇø®ÄÉÀ̼ÇÀ» ¸¸µé °æ¿ì¸¦ °¡Á¤ÇÏÀÚ. 
º¸ÅëÀÇ ¼­ºí¸´ ÄÁÅ×À̳ʴ »ç¿ëÀÚ¿¡ ´ëÇÑ ¼¼¼ÇÀ» ÀÚ½ÅÀÇ ·ÎÄÃÆÄÀÏ·Î ÀúÀåÇÏ¿© °ü¸®ÇÏ°í ÀÖ´Ù.
·¹Áø°°Àº ¿£ÁøÀ» º¸´õ¶óµµ sessionÀ̶ó´Â µð·ºÅ丮¿¡ file serialize¸¦ ÅëÇÏ¿© ÀúÀåÇÏ°í ¿£ÁøÀÌ
Á¾·áµÇ°Å³ª ¼¼¼ÇÀÇ ½Ã°£ÀÌ ¸¸·áµÇ°Ô µÇ¸é ±× ÀúÀåµÈ temporaryÆÄÀϵéÀ» »èÁ¦ÇÏ´Â ·çƾÀ»
°¡Áö°í ÀÖ´Ù.
±×·±µ¥ ¸¸¾à¿¡ ±×·¯ÇÑ ¿£ÁøµéÀÌ ºñÁ¤»óÀûÀÎ Á¾·á¿¡ ÇØ´ç ÆÄÀϵéÀ» »èÁ¦ÇÏÁö ¸øÇÏ°í
½×¾ÆµÎ°Ô µÈ´Ù¸é, ¶ÇÇÑ Á¢¼ÓÀÌ ¸¹Àº ±×·¯ÇÑ ¼­¹ö¿´´Ù¸é ÆÄÀϽýºÅÛÀÇ »çÀÌÁî°¡ ºñ´ëÇÏ°Ô
ºÒ¾î³ª°Ô µÉ °ÍÀº ºÒÀ» º¸µí »·ÇÑ ÀϾƴѰ¡? 

³î»õ~ÀÇ »ùÇÃÄÚµå °¡Á¤À» ºñ½ÁÇÏ´Ù.
¾ÖÇø®ÄÉÀ̼ÇÀÌ ½ÃÀÛµÇ¸é »ç¿ëÀÚÀÇ °íÀ¯½Äº°ÆÄÀÏÀ» Çϳª ¸¸µé¾î¼­ ±â·ÏÇÏ°í ÀÖ´Ù°¡ 
¾î¶°ÇÑ ¹æ¹ýÀ¸·Î¶óµµ ÇÁ·Î±×·¥ÀÌ Á¾·áµÇ°Ô µÇ¸é ±× ÆÄÀÏÀ» »èÁ¦Çϵµ·Ï ÇÒ°ÍÀÌ´Ù.

ÀÚ ±×·³ À§ÀÇ ¼ø¼­¿¡ ÀÇÇÏ¿© ÇϳªÇϳª ÀÛ¼ºÀ» Çغ¸µµ·Ï ÇÏÀÚ.

¢º ShutdownHookDemo.java


    import java.io.*;

    public class ShutdownHookDemo {
    private String dir = ".";
    private String filename = "session_tmp.txt";

    /**
    *  ¾ÖÇø®ÄÉÀ̼ÇÀÌ ½ÃÀ۵Ǹé shutdownÀ» »ý¼ºÇÑÈÄ µî·ÏÇÑ´Ù.
    */
    public void start() {
        System.out.println("Demo");
        ShutdownHook shutdownHook = new ShutdownHook();
        Runtime.getRuntime().addShutdownHook(shutdownHook);
    }

    /**
    *  1. ½ÇÇàÀÇ ¼ø¼­´Â ¸ÕÀú tempoaryÆÄÀÏÀÇ »ý¼º
    *  2. RuntimeŬ·¡½º¿¡ shutdown hookÀ» µî·Ï
    *  3. »ç¿ëÀÚÀÇ ÀÔ·ÂÀ» ±â´Ù¸²(¹«ÇÑ·çÇÁ)
    */
    public static void main(String[] args) {
        ShutdownHookDemo demo = new ShutdownHookDemo();
        demo.setup();
        demo.start();

        try {
            while(true){
                System.in.read();
            }
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
    /**
    *	ShutdownHookŬ·¡½º¸¦ innerŬ·¡½º·Î »ç¿ëÇÔÀ¸·Î¼­ ShutdownHookDemoŬ·¡½ºÀÇ
    *	¸â¹öº¯¼ö¿¡ ´ëÇÑ ÂüÁ¶¸¦ °¡´ÉÇϵµ·Ï ¸¸µç´Ù.
    */
    private class ShutdownHook extends Thread {
        public void run() {
            shutdown();
        }
    }

    /**
    *  ShutdownÀÇ µÇ´Â ½ÃÁ¡¿¡ »ý¼ºµÇ¾ú´ø temporaryÆÄÀÏÀ» »èÁ¦ÇÑ´Ù.
    */
    private void shutdown() {
        System.out.println("start shutdown progress.. please wait");
        // delete the temp file
        File file = new File(dir, filename);

        if (file.exists()) {
            System.out.println("Deleting temporary file.");
            file.delete();
        }

    }
    /**
    *  ÇÁ·Î±×·¥ÃÖÃÊ ¼öÇà½Ã¿¡ ÀÓ½ÃÆÄÀÏÀ» »ý¼ºÇس½´Ù.
    */
    private void setup(){
    // create a temp file
        File file = new File(dir, filename);

        try {
            System.out.println("Creating temporary file");
            file.createNewFile();
        }
        catch (IOException e) {
            System.out.println("Failed creating temporary file.");
        }
    }


    }

À§¿¡¼­ º°¹Ý ´Ù¸£°Ô º¼°Ç ¾ø´Ù. ÇÁ·Î±×·¥ÀÌ ½ÃÀ۵Ǹé while(true){ System.in.read(); } ó·³ ¹«ÇÑ·çÇÁ¸¦ µ¹¸ç »ç¿ëÀÚÀÇ Å°º¸µå ÀÔ·ÂÀ» ±â´Ù¸®°Ô µÇ´Âµ¥, ctrl+c°¡ ÀԷµDZâÀü¿¡´Â °è¼Ó ÀԷ¸¸À» ¹Þ°Ô µÈ´Ù. ½ÇÁ¦ shutdown hookŬ·¡½º´Â ¾Æ·¡¿Í °°ÀÌ °£´ÜÇÏ°Ô ¸¸µé¼ö ÀÖÀ¸¸ç private class ShutdownHook extends Thread { public void run() { shutdown(); } } ´Ü¼øÈ÷ Ŭ·¡½º°¡ Runtime.getRuntime().addShutdownHook(shutdownHook); ¿¡ ÀÇÇÏ¿© µî·Ï¸¸ µÇ¾î ÀÖÀ¸¸é ÀÚµ¿À¸·Î ó¸®µÇ°Ô µÈ´Ù. ½ÇÁ¦·Î Å×½ºÆ®¸¦ Çغ¼°æ¿ì ÇÁ·Î±×·¥À» ¼öÇà½ÃÅ°´Â ½ÃÁ¡¿¡ session_tmp.txtÆÄÀÏÀÌ µð·ºÅ丮¿¡ »ý±â°Ô µÈ´Ù. °è¼Ó Å×½ºÆ®ÇÏ´Ù°¡ ½ÇÁ¦ ctrl+C¸¦ Å°º¸µå¿¡ ÀÔ·ÂÇßÀ» °æ¿ì shutdown progress¶ó´Â ¸Þ½ÃÁö¿Í ÇÔ²² ÇØ´ç ÀÓ½ÃÆÄÀÏÀº ¾ø¾îÁö°Ô µÉ°ÍÀÌ´Ù. °£´ÜÇÏ°Ô Å×½ºÆ® Çغ¼ ¼ö ÀÖÀ¸¸ç À§ÀÇ ÇÁ·Î±×·¥À» Á¶±Ý¸¸ ÀÀ¿ëÇصµ ´ç½ÅÀº ¾ÆÁÖ ÈǸ¢ÇÑ ¾ÖÇø®ÄÉÀ̼ÇÀ» ¸¸µé¼ö ÀÖÀ» °ÍÀÌ´Ù.
 
1
References
 
Copyright ¨Ï 2003 www.javapattern.info & www.jlook.com, an jLOOK co.,LTD