You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: page-controller/README.md
-4Lines changed: 0 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,10 +13,6 @@ tags:
13
13
- Web development
14
14
---
15
15
16
-
## Also known as
17
-
18
-
* Dispatcher
19
-
20
16
## Intent
21
17
22
18
The Page Controller pattern is intended to handle requests for a specific page or action within a web application, processing input, and determining the appropriate view for rendering the response.
Copy file name to clipboardExpand all lines: reactor/README.md
+98-10Lines changed: 98 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,26 +3,114 @@ title: Reactor
3
3
category: Concurrency
4
4
language: en
5
5
tag:
6
-
- Performance
7
-
- Reactive
6
+
- Asynchronous
7
+
- Event-driven
8
+
- Fault tolerance
9
+
- Messaging
10
+
- Reactive
11
+
- Scalability
12
+
- Synchronization
13
+
- Thread management
8
14
---
9
15
16
+
## Also known as
17
+
18
+
* Dispatcher
19
+
* Notifier
20
+
10
21
## Intent
11
-
The Reactor design pattern handles service requests that are delivered concurrently to an application by one or more clients. The application can register specific handlers for processing which are called by reactor on specific events. Dispatching of event handlers is performed by an initiation dispatcher, which manages the registered event handlers. Demultiplexing of service requests is performed by a synchronous event demultiplexer.
22
+
23
+
Handle service requests that are delivered concurrently to a service handler by one or more inputs.
24
+
25
+
## Explanation
26
+
27
+
Real-world example
28
+
29
+
> Imagine a busy restaurant kitchen where multiple orders come in from different tables at the same time. Instead of each chef handling one order at a time, there is a head chef who acts as the dispatcher. The head chef receives all the orders and decides which chef will handle which part of each order, ensuring that all chefs are utilized efficiently. This way, the kitchen can handle many orders simultaneously, ensuring that dishes are prepared quickly and efficiently without any one chef becoming a bottleneck. This setup is analogous to the Reactor pattern, where the head chef dispatches tasks (events) to various chefs (event handlers) to process multiple tasks concurrently.
30
+
31
+
In plain words
32
+
33
+
> The Reactor pattern efficiently handles multiple concurrent service requests by dispatching them to appropriate event handlers using a single or a limited number of threads.
34
+
35
+
Wikipedia says
36
+
37
+
> The reactor software design pattern is an event handling strategy that can respond to many potential service requests concurrently. The pattern's key component is an event loop, running in a single thread or process, which demultiplexes incoming requests and dispatches them to the correct request handler.
38
+
39
+
**Programmatic Example**
40
+
41
+
The Reactor design pattern is a concurrency model that efficiently handles multiple simultaneous I/O operations using a single or a limited number of threads. It is particularly useful in scenarios where an application needs to handle multiple clients sending service requests concurrently.
42
+
43
+
In the given code, the Reactor pattern is implemented using Java's NIO (Non-blocking I/O) framework. The key components of this pattern in the code are:
44
+
45
+
1.`NioReactor`: This class acts as the Synchronous Event De-multiplexer and Initiation Dispatcher. It waits for events on multiple channels registered to it in an event loop and dispatches them to the appropriate handlers.
46
+
47
+
2.`AbstractNioChannel`: This class acts as a Handle that is registered to the reactor. When any events occur on a handle, the reactor calls the appropriate handler.
48
+
49
+
3.`ChannelHandler`: This class acts as an Event Handler, which is bound to a channel and is called back when any event occurs on any of its associated handles. Application logic resides in event handlers.
50
+
51
+
Here is a simplified example of how these components interact:
In this example, the `NioReactor` is created with a `ThreadPoolDispatcher` which uses 2 threads for dispatching events. Two channels, a `NioServerSocketChannel` and a `NioDatagramChannel`, are registered with the reactor. These channels are associated with a `LoggingHandler` which handles the events that occur on these channels. Finally, the reactor is started, and it begins to listen for events on the registered channels.
72
+
73
+
When an event occurs on a channel, the reactor's event loop detects it and dispatches the event to the `LoggingHandler` associated with that channel. The `LoggingHandler` then processes the event.
12
74
13
75
## Class diagram
76
+
14
77

15
78
16
79
## Applicability
17
-
Use Reactor pattern when
18
80
19
-
* A server application needs to handle concurrent service requests from multiple clients.
20
-
* A server application needs to be available for receiving requests from new clients even when handling older client requests.
21
-
* A server must maximize throughput, minimize latency and use CPU efficiently without blocking.
81
+
* Use the Reactor pattern when you need to handle multiple simultaneous I/O operations efficiently.
82
+
* Ideal for applications requiring high scalability and low-latency, such as web servers and networking frameworks.
83
+
84
+
## Known Uses
85
+
86
+
* Netty: An asynchronous event-driven network application framework for rapid development of maintainable high-performance protocol servers and clients.
87
+
* Akka: A toolkit and runtime for building concurrent, distributed, and fault-tolerant applications on the JVM.
88
+
* Java NIO (New I/O): Provides non-blocking I/O operations, allowing a single thread to manage multiple channels.
89
+
90
+
## Consequences
91
+
92
+
Benefits:
93
+
94
+
* Improves application performance by efficiently handling multiple simultaneous connections.
95
+
* Reduces resource consumption by using a small number of threads to handle many I/O operations.
96
+
* Enhances scalability by allowing applications to serve many clients with minimal threads.
97
+
98
+
Trade-offs:
99
+
100
+
* Increased complexity in managing state and event handling.
101
+
* Debugging and maintaining asynchronous code can be challenging.
102
+
* Potential difficulty in ensuring thread safety and avoiding race conditions.
103
+
104
+
## Related Patterns
105
+
106
+
*[Observer](https://java-design-patterns.com/patterns/observer/): Reactor uses the Observer pattern for handling events where event handlers are notified of changes.
107
+
* Proactor: Similar to Reactor but handles asynchronous I/O completion rather than readiness.
108
+
*[Command](https://java-design-patterns.com/patterns/command/): Encapsulates a request as an object, allowing parameterization and queuing of requests.
22
109
23
110
## Credits
24
111
25
112
*[Douglas C. Schmidt - Reactor](https://www.dre.vanderbilt.edu/~schmidt/PDF/Reactor.pdf)
26
-
*[Pattern Oriented Software Architecture Volume 2: Patterns for Concurrent and Networked Objects](https://www.amazon.com/gp/product/0471606952/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0471606952&linkCode=as2&tag=javadesignpat-20&linkId=889e4af72dca8261129bf14935e0f8dc)
27
-
*[Doug Lea - Scalable IO in Java](http://gee.cs.oswego.edu/dl/cpjslides/nio.pdf)
28
-
*[Netty](http://netty.io/)
113
+
*[Java Concurrency in Practice](https://amzn.to/4aRMruW)
114
+
*[Pattern-Oriented Software Architecture Volume 2: Patterns for Concurrent and Networked Objects](https://amzn.to/3UgC24V)
115
+
*[Reactive Programming with RxJava: Creating Asynchronous, Event-Based Applications](https://amzn.to/4dNTLJC)
116
+
*[Scalable IO in Java - Doug Lea](http://gee.cs.oswego.edu/dl/cpjslides/nio.pdf)
0 commit comments