SlideShare a Scribd company logo
1 of 10
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.

More Related Content

What's hot

Transaction Management Tool
Transaction Management ToolTransaction Management Tool
Transaction Management Tool
Peeyush Ranjan
 
Servletand sessiontracking
Servletand sessiontrackingServletand sessiontracking
Servletand sessiontracking
vamsi krishna
 
ActiveRecord is Rotting Your Brian
ActiveRecord is Rotting Your BrianActiveRecord is Rotting Your Brian
ActiveRecord is Rotting Your Brian
Ethan Gunderson
 

What's hot (20)

Introduction to Unit Testing (Part 2 of 2)
Introduction to Unit Testing (Part 2 of 2)Introduction to Unit Testing (Part 2 of 2)
Introduction to Unit Testing (Part 2 of 2)
 
Transaction Management Tool
Transaction Management ToolTransaction Management Tool
Transaction Management Tool
 
Selenium inputs
Selenium inputsSelenium inputs
Selenium inputs
 
Reactive Programming no Android
Reactive Programming no AndroidReactive Programming no Android
Reactive Programming no Android
 
Reactive Programming on Android
Reactive Programming on AndroidReactive Programming on Android
Reactive Programming on Android
 
The Ring programming language version 1.7 book - Part 85 of 196
The Ring programming language version 1.7 book - Part 85 of 196The Ring programming language version 1.7 book - Part 85 of 196
The Ring programming language version 1.7 book - Part 85 of 196
 
Tugas 2
Tugas 2Tugas 2
Tugas 2
 
Fia fabila
Fia fabilaFia fabila
Fia fabila
 
201913001 khairunnisa progres_harian
201913001 khairunnisa progres_harian201913001 khairunnisa progres_harian
201913001 khairunnisa progres_harian
 
OSGi World Congress Workshop Exercise - P Kriens
OSGi World Congress Workshop Exercise - P KriensOSGi World Congress Workshop Exercise - P Kriens
OSGi World Congress Workshop Exercise - P Kriens
 
Asynchronous programming done right - Node.js
Asynchronous programming done right - Node.jsAsynchronous programming done right - Node.js
Asynchronous programming done right - Node.js
 
Aspdevice - Asp Fast Crud introdution
Aspdevice - Asp Fast Crud introdutionAspdevice - Asp Fast Crud introdution
Aspdevice - Asp Fast Crud introdution
 
Deferred
DeferredDeferred
Deferred
 
Deceptive simplicity of async and await
Deceptive simplicity of async and awaitDeceptive simplicity of async and await
Deceptive simplicity of async and await
 
Promises, promises, and then observables
Promises, promises, and then observablesPromises, promises, and then observables
Promises, promises, and then observables
 
Servletand sessiontracking
Servletand sessiontrackingServletand sessiontracking
Servletand sessiontracking
 
ActiveRecord is Rotting Your Brian
ActiveRecord is Rotting Your BrianActiveRecord is Rotting Your Brian
ActiveRecord is Rotting Your Brian
 
Async History - javascript
Async History - javascriptAsync History - javascript
Async History - javascript
 
13 networking, mobile services, and authentication
13   networking, mobile services, and authentication13   networking, mobile services, and authentication
13 networking, mobile services, and authentication
 
Introduction to Service Workers | Matteo Manchi
Introduction to Service Workers | Matteo ManchiIntroduction to Service Workers | Matteo Manchi
Introduction to Service Workers | Matteo Manchi
 

Viewers also liked

2010-02-09 Reactor Pattern & Event Driven Programming
2010-02-09 Reactor Pattern & Event Driven Programming2010-02-09 Reactor Pattern & Event Driven Programming
2010-02-09 Reactor Pattern & Event Driven Programming
Lin Jen-Shin
 
Mocv Dmaterialgrowth
Mocv DmaterialgrowthMocv Dmaterialgrowth
Mocv Dmaterialgrowth
guestda8318
 
Fly weight pattern #dezapatan
Fly weight pattern #dezapatanFly weight pattern #dezapatan
Fly weight pattern #dezapatan
kuidaoring
 
Centrifuge
CentrifugeCentrifuge
Centrifuge
nafaz114
 
Crystal Growth & Mocvd Experience3
Crystal Growth & Mocvd Experience3Crystal Growth & Mocvd Experience3
Crystal Growth & Mocvd Experience3
Philjer
 
XP寺子屋 デザインパターン入門
XP寺子屋 デザインパターン入門XP寺子屋 デザインパターン入門
XP寺子屋 デザインパターン入門
takepu
 
Classification & Selection of Reactors
Classification & Selection of ReactorsClassification & Selection of Reactors
Classification & Selection of Reactors
Gulfam Raza
 

Viewers also liked (20)

2010-02-09 Reactor Pattern & Event Driven Programming
2010-02-09 Reactor Pattern & Event Driven Programming2010-02-09 Reactor Pattern & Event Driven Programming
2010-02-09 Reactor Pattern & Event Driven Programming
 
Mocv Dmaterialgrowth
Mocv DmaterialgrowthMocv Dmaterialgrowth
Mocv Dmaterialgrowth
 
Fly weight pattern #dezapatan
Fly weight pattern #dezapatanFly weight pattern #dezapatan
Fly weight pattern #dezapatan
 
Design for Single Reactions
Design for Single ReactionsDesign for Single Reactions
Design for Single Reactions
 
Strategy パターンと開放/閉鎖原則に見るデザインパターンの有用性
Strategy パターンと開放/閉鎖原則に見るデザインパターンの有用性Strategy パターンと開放/閉鎖原則に見るデザインパターンの有用性
Strategy パターンと開放/閉鎖原則に見るデザインパターンの有用性
 
Reactor Design 4
Reactor Design 4Reactor Design 4
Reactor Design 4
 
Centrifuge
CentrifugeCentrifuge
Centrifuge
 
GoF のデザインパターンじゃないけど、よくあるパターン
GoF のデザインパターンじゃないけど、よくあるパターンGoF のデザインパターンじゃないけど、よくあるパターン
GoF のデザインパターンじゃないけど、よくあるパターン
 
Crystal Growth & Mocvd Experience3
Crystal Growth & Mocvd Experience3Crystal Growth & Mocvd Experience3
Crystal Growth & Mocvd Experience3
 
デザインパターン
デザインパターンデザインパターン
デザインパターン
 
Part III. Metal-Organic Chemical Vapor Deposition
Part III. Metal-Organic Chemical Vapor DepositionPart III. Metal-Organic Chemical Vapor Deposition
Part III. Metal-Organic Chemical Vapor Deposition
 
Reactor Design 2
Reactor Design 2Reactor Design 2
Reactor Design 2
 
Reactor Design 5
Reactor Design 5Reactor Design 5
Reactor Design 5
 
Chapter 6
Chapter 6Chapter 6
Chapter 6
 
M.Tech Presentation(Design, Growth & Fabrication of Solar Cell)
M.Tech Presentation(Design, Growth & Fabrication of Solar Cell)M.Tech Presentation(Design, Growth & Fabrication of Solar Cell)
M.Tech Presentation(Design, Growth & Fabrication of Solar Cell)
 
conversion and reactor sizing
conversion and reactor sizingconversion and reactor sizing
conversion and reactor sizing
 
Introduction and Molar Balances - Reactor Engineering Course Block 1
Introduction and Molar Balances - Reactor Engineering Course Block 1Introduction and Molar Balances - Reactor Engineering Course Block 1
Introduction and Molar Balances - Reactor Engineering Course Block 1
 
XP寺子屋 デザインパターン入門
XP寺子屋 デザインパターン入門XP寺子屋 デザインパターン入門
XP寺子屋 デザインパターン入門
 
Practical OOP In Java
Practical OOP In JavaPractical OOP In Java
Practical OOP In Java
 
Classification & Selection of Reactors
Classification & Selection of ReactorsClassification & Selection of Reactors
Classification & Selection of Reactors
 

Similar to Reactor Design Pattern

I need an explaining for each step in this code and the reason of it-.docx
I need an explaining for each step in this code and the reason of it-.docxI need an explaining for each step in this code and the reason of it-.docx
I need an explaining for each step in this code and the reason of it-.docx
hendriciraida
 
I need you to modify and change the loop in this code without changing.docx
I need you to modify and change the loop in this code without changing.docxI need you to modify and change the loop in this code without changing.docx
I need you to modify and change the loop in this code without changing.docx
hendriciraida
 

Similar to Reactor Design Pattern (18)

Winform
WinformWinform
Winform
 
Rxjs marble-testing
Rxjs marble-testingRxjs marble-testing
Rxjs marble-testing
 
Reactive server with netty
Reactive server with nettyReactive server with netty
Reactive server with netty
 
Rxjs swetugg
Rxjs swetuggRxjs swetugg
Rxjs swetugg
 
java sockets
 java sockets java sockets
java sockets
 
Qt Rest Server
Qt Rest ServerQt Rest Server
Qt Rest Server
 
Василий Сорокин, Простой REST сервер на Qt с рефлексией
Василий Сорокин, Простой REST сервер на Qt с рефлексиейВасилий Сорокин, Простой REST сервер на Qt с рефлексией
Василий Сорокин, Простой REST сервер на Qt с рефлексией
 
Rxjs ngvikings
Rxjs ngvikingsRxjs ngvikings
Rxjs ngvikings
 
Expert JavaScript tricks of the masters
Expert JavaScript  tricks of the mastersExpert JavaScript  tricks of the masters
Expert JavaScript tricks of the masters
 
I need an explaining for each step in this code and the reason of it-.docx
I need an explaining for each step in this code and the reason of it-.docxI need an explaining for each step in this code and the reason of it-.docx
I need an explaining for each step in this code and the reason of it-.docx
 
Angular2 rxjs
Angular2 rxjsAngular2 rxjs
Angular2 rxjs
 
Managing State in React Apps with RxJS by James Wright at FrontCon 2019
Managing State in React Apps with RxJS by James Wright at FrontCon 2019Managing State in React Apps with RxJS by James Wright at FrontCon 2019
Managing State in React Apps with RxJS by James Wright at FrontCon 2019
 
Device Simulator with Akka
Device Simulator with AkkaDevice Simulator with Akka
Device Simulator with Akka
 
Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket Programming
 
I need you to modify and change the loop in this code without changing.docx
I need you to modify and change the loop in this code without changing.docxI need you to modify and change the loop in this code without changing.docx
I need you to modify and change the loop in this code without changing.docx
 
Network
NetworkNetwork
Network
 
Building multi lingual and empatic bots - Sander van den Hoven - Codemotion A...
Building multi lingual and empatic bots - Sander van den Hoven - Codemotion A...Building multi lingual and empatic bots - Sander van den Hoven - Codemotion A...
Building multi lingual and empatic bots - Sander van den Hoven - Codemotion A...
 
V8
V8V8
V8
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 

Reactor Design Pattern

  • 1. Reactor Design Pattern (Using java nio) By: Allan S. Lim
  • 2. Selector Selector selector = Selector.open();
  • 3. Selector ssc ServerSocketChannelssc = ServerSocketChannel.open(); ssc.configureBlocking( false ); ServerSocketss = ssc.socket(); InetSocketAddress address = new InetSocketAddress(port); ss.bind( address );
  • 4. register Selector ssc accept SelectionKey key = ssc.register( selector, SelectionKey.OP_ACCEPT );
  • 5. 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. } }
  • 6. 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 );
  • 7. 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(); }
  • 8. register accept handler Selector ssc accept read sc read handler write sc write handler It’s the same process with the write handler.
  • 9. register accept handler Selector ssc accept read sc read handler write sc write handler You can have an array of handlers.
  • 10. 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.