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


NIO Reactor PatternÀÇ »ç¿ë
 
ºÐ»êȯ°æ ½Ã½ºÅÛ¿¡¼­ ±¸ÃàµÇ´Â ¼­¹ö´Â Ŭ¶óÀ̾ðÆ®ÀÇ µ¿½Ã´Ù¹ßÀûÀÎ ¿äûÀ» ¾î¶»°Ô ó¸®ÇؾßÇÒ±î¿ä? ±Û½ê¿ä. ±×°ÍÀ» ó¸®Çϱâ À§ÇÑ ¿©·¯°¡Áö ¹æ¹ýÁß¿¡ ÇϳªÀÎ Reactor Pattern À» ¼Ò°³ÇÕ´Ï´Ù. ±×¸®°í NIO ¿¡¼­ Reactor ¸¦ ¾î¶»°Ô Çö½ÇÀûÀ¸·Î ±¸ÇöÇÏ°í ÀÖ´ÂÁöµµ »ì¦ ¿³º¸±â·Î ÇÏÁÒ. ( 2003/09/04 ) 264
Written by ienvyou - ÃÖÁö¿õ
1 of 1
 

¢º¹Ì¸®º¸±â

ºÐ»êȯ°æ ½Ã½ºÅÛ¿¡¼­ ±¸ÃàµÇ´Â ¼­¹ö´Â Ŭ¶óÀ̾ðÆ®ÀÇ µ¿½Ã´Ù¹ßÀûÀÎ ¿äûÀ» ¾î¶»°Ô ó¸®ÇؾßÇÒ±î¿ä?
±Û½ê¿ä. ±×°ÍÀ» ó¸®Çϱâ À§ÇÑ ¿©·¯°¡Áö ¹æ¹ýÁß¿¡ ÇϳªÀÎ Reactor Pattern À» ¼Ò°³ÇÕ´Ï´Ù.
±×¸®°í NIO ¿¡¼­ Reactor ¸¦ ¾î¶»°Ô Çö½ÇÀûÀ¸·Î ±¸ÇöÇÏ°í ÀÖ´ÂÁöµµ »ì¦ ¿³º¸±â·Î ÇÏÁÒ. 

¢º½ºÅ͵𳻿ë

- Reactor Pattern À» ÀÌÇØÇÑ´Ù.
- NIO ¿¡¼­ Reactor Pattern À» ¾î¶»°Ô ±¸ÇöÇß´ÂÁö¸¦ ¾Ë¾Æº»´Ù

1Àå Reactor Pattern

¢º¹®Á¦Á¡ ºÐ»êȯ°æÀÇ ¼­¹öµéÀº Ŭ¶óÀ̾ðÆ®µéÀÇ ¿äûÀ» È¿À²ÀûÀ¸·Î 󸮸¦ ÇؾßÇÕ´Ï´Ù. ÇϳªÀÇ ¿äûÀ» 󸮽ð£ÀÌ Á» °É¸®´Âµ¿¾È ´Ù¸¥ ¿äûµéÀ» ´ë±â½ÃÅ°´Â ÇÑÀÌ À־ 󸮸¦ ÇؾßÇÕ´Ï´Ù. 󸮼ӵµ´Â ÃÖ´ëÇÑ »¡¸®ÇÏ°í ´ë±â½Ã°£Àº ÃÖ¼ÒÇÑ Çؾ߸¸ ÁÁÀº ¼­¹ö°ÚÁÒ. ¾Æ¸¶µµ¡¦ »õ·Î¿î ¼­ºñ½ºµéÀ» Ãß°¡ÇÔ¿¡ À־ ±âÁ¸ÀÇ ÄÚµåµéÀÇ º¯µ¿ÀÌ ¾ø¾î¾ß ÇÕ´Ï´Ù. ±×¸®°í º´Çà󸮳ª µ¿½Ãó¸® Á¤Ã¥À» »ç¿ëÇÔ¿¡ À־µµ °£´ÜÇØ¾ß ÇÕ´Ï´Ù. ¢º±âº»±¸Á¶ POSA2 (Pattern Oriented Software Architecture 2) ¿¡ ÀÌ·¯ÇÑ ¹®Á¦Á¡À» ÇØ°áÇϱâ À§ÇѰ͵éÁß¿¡ Çϳª·Î Reactor Pattern À» ¼³¸íÇسù½À´Ï´Ù. ±âº»ÀûÀÎ Á¤ÀÇ ³»¿ëÀ» º¸¸é ¾Æ·¡¿Í °°½À´Ï´Ù.
  1. Reactor ´Â ¾îÇø®ÄÉÀ̼ǿ¡ µ¶¸³ÀûÀÎ À̺¥Æ® Demultiplexing °ú 󸮷ÎÁ÷À» ±¸ÇöÇÑ´Ù.
  2. Demultiplexing Àº ÇϳªÀÇ Àü¼ÛÅë·Î·Î ÀԷ¹ÞÀº Á¤º¸¸¦ ´ÙÁßÀÇ Ãâ·Â¹æ¹ýÁß Çϳª¸¦ ¼±ÅÃÇؼ­ Ãâ·ÂÇÏ´Â ¹æ½ÄÀ» ¸»ÇÕ´Ï´Ù. Áï, Event Source ·ÎºÎÅÍ event ¸¦ ¹Þ¾Æ Reactor ¿¡ µî·ÏµÇ¾î ÀÖ´Â ÀûÀýÇÑ Event Handler ¸¦ ÀÌ¿ëÇÏ¿© ó¸®ÇÏ°Ô µÇ´Â ¹æ½ÄÀÔ´Ï´Ù.
  3. ¼­·Î ´Ù¸¥ ŸÀÔÀÇ Event Handler µéÀº °¢ÀÚÀÇ ¼­ºñ½º¸¦ ±¸ÇöÇÏ°í ÀÖ½À´Ï´Ù.
  4. Acceptor ´Â »õ·Î¿î ¿äûÀÌ µé¾î¿ÔÀ» ¶§ ÇØ´ç ¿äûÀ» ó¸®ÇÒ ¼ö ÀÖ´Â Event Handler ¸¦ »ý¼ºÇÕ´Ï´Ù.
¢ºÀÛµ¿¼ø¼­ ¾Æ·¡ÀÇ ±×¸²Àº Reactor Pattern ÀÇ ±¸Á¶ ¹× µ¿ÀÛÇÏ´Â ¹æ½ÄÀ» µµ½ÄÈ­ ÇÑ°ÍÀÔ´Ï´Ù. ±×¸²À» º¸°í ¼ø¼­¸¦ ¼³¸íµå¸®°Ú½À´Ï´Ù. [Âü°í1] Reactor µ¿ÀÛ ¼ø¼­µµ ¨ç Reactor ¸¦ »ý¼ºÇÕ´Ï´Ù. ¨è Main Program Àº Accptor µéÀ» »ý¼ºÇÕ´Ï´Ù. ¨é ¸ðµç »ý¼ºµÈ Acceptor µéÀº Reactor ¿¡ ±×µéÀÌ ´ã´çÇÒ event Á¾·ù¿Í ÇÔ²² Reactor ¿¡ µî·ÏÇÕ´Ï´Ù. ¨ê Event Source ´Â »õ·Î¿î event °¡ ¹ß»ýÇÒ ¶§±îÁö listen »óÅ·Π´ë±âÇÕ´Ï´Ù. ¨ë »õ·Î¿î Connection event °¡ Event Source ¿¡ µµÂøÀ» ÇÕ´Ï´Ù. ¨ì Event Source ´Â Reactor ¸¦ notify ½Ãŵ´Ï´Ù. ¨í Reactor ´Â ÀÌ event ¸¦ ó¸®Çϱâ À§ÇÑ Acceptor ¸¦ notify ½Ãŵ´Ï´Ù. ¨î Acceptor ´Â ÀÚ½ÅÀÌ Control ÇÏ´Â »õ·Î¿î Event Handler ¸¦ Thread ·Î »ý¼ºÇÕ´Ï´Ù. ¨ï »õ·Î »ý¼ºµÈ Thread ¸¦ ±×µéÀÌ ´ã´çÇÒ event Á¾·ù¿Í ÇÔ²² Reacotr ¿¡ µî·ÏÇÕ´Ï´Ù. ¨ð µî·ÏµÈ Thread ¿Í ÀÏÄ¡ÇÏ´Â event °¡ ¹ß»ýÇÑ´Ù°í °¡Á¤ÇÕ´Ï´Ù. ¨ñ ÀÌ event ¸¦ ó¸®Çϱâ À§Çؼ­ Reactor ¸¦ ÅëÇÏ¿© ÇØ´ç event ¸¦ ó¸® °¡´ÉÇÑ ¸ðµç µî·ÏµÈ Evnet Handler ¿¡°Ô event ¸¦ Àü´ÞÇÏ¿© ó¸®ÇÕ´Ï´Ù. ¢ºÀåÁ¡ ¸ðµç ¼­ºñ½ºµéÀº °¢°¢ µ¶¸³ÀûÀ¸·Î ½ÇÇà°¡´ÉÇÏ´Ù. ¿À·£ ó¸® ½Ã°£À» ¿äÇÏ´Â ¸¹Àº ¼­ºñ½ºµéÀÇ Ã³¸®¸¦ °ü¸®ÇÏ´Â Thread °ü¸®°¡ Àü¹ÝÀûÀÎ ¼º´ÉÀúÇϸ¦ °¡Á®¿ÀÁö ¾Ê½À´Ï´Ù. ±×¸®°í »õ·Î¿î ¼­ºñ½ºÀÇ Ãß°¡°¡ ½±½À´Ï´Ù. Thread °ü¸®°¡ ½±½À´Ï´Ù. ÁöÁ¤µÈ Acceptor ¸¦ ÅëÇÏ¿© ¿ä±¸¿¡ µû¶ó »ý¼ºµÇ¾îÁö±â ¶§¹®ÀÔ´Ï´Ù. ±×¸®°í Áß¾Ó Thread Manager ¿¡ ÀÇÇؼ­ À¯ÁöµÇ±â ¶§¹®ÀÔ´Ï´Ù. Connection ´ç Thread ÀÇ À̵¿µµ ½±½À´Ï´Ù.

2Àå NIO ¿¡¼­ÀÇ Reactor Pattern ÀÌ¿ë

¢ºNIO ¿¡¼­ÀÇ Reactor ¿ªÇÒÀÇ Selector NIO ¿¡¼­ Reactor ÀÇ ¿ªÇÒÀ» Çϴ Ŭ·¡½º°¡ ÀÖ´Ù. ÀÌ°ÍÀÌ ¹Ù·Î Selector Ŭ·¡½ºÀÌ´Ù. 1Àå¿¡¼­ º¸¾ÒµíÀÌ Reactor Pattern ¿¡´Â ¿©·¯°¡Áö Çʼö ¿ä¼ÒµéÀÌ Àִµ¥ ±×°ÍÀº Reactor, Acceptor, Event Handler ±×¸®°í event ¿´´Ù. ¢ºSelector Class ±¸Á¶ Selector Class ´Â ³»ºÎÀûÀ¸·Î 3°¡ÁöÀÇ Key Set À» °¡Áö°í ÀÖ´Ù. ¨çKey Set ÇϳªÀÇ Selector ¿¡ µî·ÏµÇ¾îÁø channel µéÀ» ´ëÇ¥ÇÏ´Â key µéÀÇ ÁýÇÕÀÌ´Ù. ¿©±â¼­ chnnel À̶ó´Â °ÍÀº Acceptor, Event Handler µéÀ» ÃÑĪÇϴ°ÍÀ¸·Î º¸¸é µÈ´Ù. keys() ¶ó´Â ¸Þ¼­µå¸¦ ÀÌ¿ëÇÏ¿© ¾òÀ»¼ö ÀÖ´Ù. chnnel ÀÇ register() method ¸¦ ÀÌ¿ëÇÑ´Ù. ¨èselected-key set select() ¸Þ¼­µå¿¡ ÀÇÇؼ­ ¾ò¾îÁø key µéÀÇ ÁýÇÕÀÌ´Ù. ÀÌ°ÍÀÇ À§ÀÇ Key set ÀÇ subset ÀÌ¶ó º¸¸é µÇ°í, Selector °¡ demultiplexing À» ÇÏ´Â key °ªµéÀ̶ó°í º¸¸é µÈ´Ù. notify ½ÃŲ event ¸¦ ó¸®ÇÏ´Â Event Handler (channel) µéÀÇ key °ªÀÌ´Ù. selectedKeys() ¶ó´Â ¸Þ¼­µå¸¦ ÀÌ¿ëÇÏ¿© ¾òÀ» ¼ö ÀÖ´Ù. ¨écancelled-key set Ãë¼ÒµÈ key µéÀÇ ÁýÇÕÀÌ´Ù. ÀÌ°ÍÀº Á÷Á¢ Á¢¼ÓÀÌ ºÒ°¡´ÉÇÏ´Ù. cancel() method ¿¡ ÀÇÇϰųª chnnel À» close ÇÏ¸é µÈ´Ù ¢ºReactor Pattern À» ÀÌ¿ëÇÑ NIO Package ÄÚµå »ùÇà ´ÙÀ½ÀÌ ÄÚµå »ùÇÃÀº À̹μö¾¾°¡ Á¦°øÇÑ ¼Ò½º¸¦ °¡Áö°í ¼³¸íÀ» ÇÕ´Ï´Ù. ±×¸®°í À§ÀÇ ±×¸²À» º¯°æÇÑ°ÍÀ» ´Ù½Ã ÷ºÎÇÕ´Ï´Ù.

public void initServer() {
try { 
    // 1. Reactor ¸¦ »ý¼ºÇÕ´Ï´Ù.
    selector = Selector.open();
        // 2. Accptor ¸¦ »ý¼ºÇÕ´Ï´Ù
    serverSocketChannel = ServerSocketChannel.open();
    // 3. ¸ðµç »ý¼ºµÈ Acceptor ´Â Reactor ¿¡ ±×µéÀÌ ´ã´çÇÒ event Á¾·ù¿Í ÇÔ²² Reactor ¿¡ 
    µî·ÏÇÕ´Ï´Ù. ÇöÀç OP_ACCEPT ´Â Ŭ¶óÀ̾ðÆ®°¡ Á¢¼ÓÀ» ½ÃµµÇÒ¶§ ¹ß»ýÇÏ´Â À̺¥Æ®ÀÔ´Ï´Ù.
    serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
    ......
    .....
}

public void startServer() {
try {
        while (true) {
    //  Event Source ´Â »õ·Î¿î event °¡ ¹ß»ýÇÒ ¶§±îÁö listen
    »óÅ·Π´ë±âÇÕ´Ï´Ù.
    // »õ·Î¿î Connection event °¡ Event Source ¿¡ µµÂøÀ» ÇÕ´Ï´Ù.
    // Event Source ´Â Reactor ¸¦ notify ½Ãŵ´Ï´Ù
            selector.select();
                        // selected-key set À» ÃßÃâ
            Iterator it = selector.selectedKeys().iterator();
            while (it.hasNext()) {
            SelectionKey key = (SelectionKey) it.next();
           // »õ·Î¿î Connection event °¡ Event Source ¿¡ µµÂøÀ» ÇÕ´Ï´Ù
            // Event Source ´Â Reactor ¸¦ notify ½Ãŵ´Ï´Ù
        if (key.isAcceptable()) {
        // Reactor ´Â ÀÌ event ¸¦ ó¸®Çϱâ À§ÇÑ Acceptor ¸¦ 
        notify ½Ãŵ´Ï´Ù
                ServerSocketChannel server = (ServerSocketChannel) key.channel();
                // Acceptor ´Â ÀÚ½ÅÀÌ Control ÇÏ´Â »õ·Î¿î Event Handler ¸¦ Thread ·Î »ý¼ºÇÕ´Ï´Ù
                SocketChannel sc = server.accept();
                // »õ·Î »ý¼ºµÈ Thread ¸¦ ±×µéÀÌ ´ã´çÇÒ event Á¾·ù¿Í ÇÔ²² Reacotr ¿¡ µî·ÏÇÕ´Ï´Ù.
                registerChannel(selector, sc, SelectionKey.OP_READ);
        
                // µî·ÏµÈ Thread ¿Í ÀÏÄ¡ÇÏ´Â event °¡ ¹ß»ýÇÑ´Ù°í °¡Á¤ÇÕ´Ï´Ù
                } else if (key.isReadable()) {
                // ÀÌ event ¸¦ ó¸®Çϱâ À§Çؼ­ Reactor ¸¦ ÅëÇÏ¿© ÇØ´ç 
                event ¸¦ ó¸® °¡´ÉÇÑ ¸ðµç µî·ÏµÈ Evnet Handler 
                ¿¡°Ô event ¸¦ Àü´ÞÇÏ¿© ó¸®ÇÕ´Ï´Ù.
                service(key);
                }
// À̺¥Æ® 󸮰¡ ³¡³ª¸é ´ë±â»óÅ·Π°¡±âÀü¿¡ 
//²À selected-key set À» remove ÇؾßÇÑ´Ù. ±×·¸Áö 
//¾ÊÀ¸¸é ´ÙÀ½¿¡ ´Ù½Ã notify µÇ´õ¶óµµ
//±âÁ¸ÀÇ selected-key set ÀÌ ¼³Á¤µÇ¾î Àֱ⶧¹®¿¡ 
//¿ÀÀÛµ¿À» ÇÑ´Ù.
                it.remove();
                   }								
              }      
        } catch (Exception e) {
        } 
    }

[Âü°í2] Âü°í1À» nio package class ¿Í mapping ÀÌ»óÀ¸·Î reactor¿¡ ´ëÇÏ¿© ¾Ë¾Æº¸¾Ò´Ù.
 
1
References
 
Copyright ¨Ï 2003 www.javapattern.info & www.jlook.com, an jLOOK co.,LTD