Reactor Design Pattern
Upcoming SlideShare
Loading in...5
×
 

Reactor Design Pattern

on

  • 4,509 views

Explaining reactor design pattern using java nio.

Explaining reactor design pattern using java nio.

Statistics

Views

Total Views
4,509
Views on SlideShare
3,810
Embed Views
699

Actions

Likes
4
Downloads
51
Comments
0

34 Embeds 699

http://liminescence.blogspot.com 502
http://liminescence.blogspot.in 36
http://liminescence.blogspot.co.uk 23
http://liminescence.blogspot.ca 19
http://liminescence.blogspot.de 17
http://liminescence.blogspot.kr 12
http://liminescence.blogspot.cz 11
http://liminescence.blogspot.fr 10
http://liminescence.blogspot.co.il 9
http://www.blogger.com 6
http://liminescence.blogspot.ru 4
http://liminescence.blogspot.fi 4
http://liminescence.blogspot.com.au 4
http://liminescence.blogspot.se 4
http://liminescence.blogspot.com.br 4
http://liminescence.blogspot.ch 3
http://liminescence.blogspot.jp 3
http://liminescence.blogspot.nl 3
http://liminescence.blogspot.ro 3
http://liminescence.blogspot.com.es 3
http://liminescence.blogspot.ie 3
http://liminescence.blogspot.hk 2
http://liminescence.blogspot.tw 2
http://liminescence.blogspot.hu 2
http://liminescence.blogspot.sk 1
http://liminescence.blogspot.dk 1
http://liminescence.blogspot.com.tr 1
http://liminescence.blogspot.pt 1
http://translate.googleusercontent.com 1
http://liminescence.blogspot.com.ar 1
http://liminescence.blogspot.sg 1
http://www.slashdocs.com 1
http://liminescence.blogspot.it 1
http://liminescence.blogspot.co.nz 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Reactor Design Pattern Reactor Design Pattern Presentation Transcript

    • Reactor Design Pattern
      (Using java nio)
      By:
      Allan S. Lim
    • Selector
      Selector selector = Selector.open();
    • Selector
      ssc
      ServerSocketChannelssc = ServerSocketChannel.open();
      ssc.configureBlocking( false );
      ServerSocketss = ssc.socket();
      InetSocketAddress address = new InetSocketAddress(port);
      ss.bind( address );
    • register
      Selector
      ssc
      accept
      SelectionKey key = ssc.register( selector, SelectionKey.OP_ACCEPT );
    • register
      accept
      handler
      Selector
      ssc
      accept
      int num = selector.select();
      Set selectedKeys = selector.selectedKeys();
      Iterator it = selectedKeys.iterator();
      while (it.hasNext()) {
      SelectionKey key = (SelectionKey)it.next();
      if ((key.readyOps() & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT) {
      // Accept the new connection and send to a handler.
      }
      }
    • register
      accept
      handler
      Selector
      ssc
      accept
      read
      sc
      read
      handler
      ServerSocketChannelssc = (ServerSocketChannel)key.channel();
      SocketChannel sc = ssc.accept();
      sc.configureBlocking( false );
      SelectionKeynewKey = sc.register( selector, SelectionKey.OP_READ );
    • register
      accept
      handler
      Selector
      ssc
      accept
      read
      sc
      read
      handler
      if ((key.readyOps() & SelectionKey.OP_ACCEPT) == SelectionKey.OP_ACCEPT) {
      // Accept the new connection and send to a handler.
      } else if ((key.readyOps() & SelectionKey.OP_READ) == SelectionKey.OP_READ) {
      // Read the data and send the handler.
      SocketChannelsc = (SocketChannel)key.channel();
      }
    • register
      accept
      handler
      Selector
      ssc
      accept
      read
      sc
      read
      handler
      write
      sc
      write
      handler
      It’s the same process with the write handler.
    • register
      accept
      handler
      Selector
      ssc
      accept
      read
      sc
      read
      handler
      write
      sc
      write
      handler
      You can have an array of handlers.
    • C
      L
      I
      E
      N
      T
      S
      register
      accept
      handler
      Selector
      ssc
      accept
      read
      sc
      read
      handler
      write
      sc
      write
      handler
      For each client event, the selector will notify the handler, and return to listening to event again.