Your SlideShare is downloading. ×
0
Reactor Design Pattern
Reactor Design Pattern
Reactor Design Pattern
Reactor Design Pattern
Reactor Design Pattern
Reactor Design Pattern
Reactor Design Pattern
Reactor Design Pattern
Reactor Design Pattern
Reactor Design Pattern
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Reactor Design Pattern

4,393

Published on

Explaining reactor design pattern using java nio.

Explaining reactor design pattern using java nio.

Published in: Education
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,393
On Slideshare
0
From Embeds
0
Number of Embeds
31
Actions
Shares
0
Downloads
63
Comments
0
Likes
4
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Reactor Design Pattern<br />(Using java nio)<br />By: <br />Allan S. Lim<br />
  • 2. Selector<br />Selector selector = Selector.open();<br />
  • 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. register<br />Selector<br />ssc<br />accept<br />SelectionKey key = ssc.register( selector, SelectionKey.OP_ACCEPT );<br />
  • 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. 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. 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. 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. 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. 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 />

×