Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Reactor Design Pattern

5,869 views

Published on

Explaining reactor design pattern using java nio.

Published in: Education
  • Dating for everyone is here: ❶❶❶ http://bit.ly/39sFWPG ❶❶❶
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Dating direct: ❤❤❤ http://bit.ly/39sFWPG ❤❤❤
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Reactor Design Pattern

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

×