16
16
* https://www.quora.com/Whats-the-difference-between-push-and-pull-protocols
17
17
* http://blog.amitinside.com/Java-Iterator-Pattern-vs-Stream-vs-RxObservable/
18
18
* http://reactivex.io/RxJava/2.x/javadoc/io/reactivex/BackpressureStrategy.html
19
+ * http://reactivex.io/RxJava/2.x/javadoc/io/reactivex/schedulers/Schedulers.html
19
20
20
21
## preface
21
22
The main goal of this project is to explore basic features of
@@ -26,18 +27,44 @@ The main goal of this project is to explore basic features of
26
27
* ** Processor** (** SubmissionPublisher** )
27
28
28
29
## introduction
30
+ ### general overview
31
+ * reactive programming (idea formulated by Eric Meijer)
32
+ * the applications we developer, the programs we create must
33
+ be really responsive and be able to react to stimuli in a
34
+ system
29
35
* the main objective of the reactive programming is NOT to be as fast as possible
30
36
but to use resources (CPU, memory, ...) in the most efficient manner
37
+ * core idea behind reactive is to release resource whenever possible
38
+ * reactive programming is functional programming+
31
39
* before java 8: completely imperative + object oriented
32
40
* please refer: https://github.com/mtumilowicz/java12-introduction-to-functional-programming-workshop
33
41
* imperative: tell me what tell me how
34
42
* declarative: tell me what and NOT how
43
+ * functional (style): declarative + higher-order function
44
+ * functional programming: function composition + lazy evaluation
45
+ * functional programming and exceptions are mutually exclusive
46
+ * handling exceptions imperative style: if you are driving a car and
47
+ have flat tire - the most illogical thing to do is to reverse back
48
+ * you should exit safely
35
49
* Michael Feather: OO makes code understandable by encapsulating moving parts. FP makes code understandable by
36
50
minimizing moving parts.
37
51
* I moving part: immutability
38
52
* II moving part: control flow (in imperative - we are going up and down to follow the flow)
39
- * functional (style): declarative + higher-order function
40
- * functional programming: function composition + lazy evaluation
53
+ ### manifesto
54
+ * https://www.reactivemanifesto.org/
55
+ * OOP four pillars: abstraction, encapsulation, inheritance, polymorphism
56
+ * reactive four pillars
57
+ * responsive
58
+ * infinite-scrolling
59
+ * providing responsiveness
60
+ * efficiency is attained not by doing tasks faster, but
61
+ by avoiding those that shouldn't be done in the first place
62
+ * resilient: make failure first-class citizen (it is okay
63
+ to fail)
64
+ * elastic - the only reasonable direction to scale is horizontally
65
+ * message driven - do not expose your database instead
66
+ export your data
67
+ ### concurrency context
41
68
* shared mutability
42
69
```
43
70
Thread th = new Thread(new Runnable() {
@@ -52,6 +79,20 @@ minimizing moving parts.
52
79
of concurrent code
53
80
* with stream: the structure of sequential code is the same as the structure of
54
81
concurrent code
82
+ * how many threads should you create?
83
+ * computation intensive <= # of cores
84
+ * `Schedulers.computation()`
85
+ * IO intensive = `NCPU * UCPU * (1 + W/C)`
86
+ * NCPU is the number of cores, available through `Runtime.getRuntime().availableProcessors()`
87
+ * UCPU is the target CPU utilization (between 0 and 1)
88
+ * W/C is the ratio of wait time to compute time
89
+ * `Schedulers.io()`
90
+ * number of threads is therefore strictly limited
91
+ * by memory also
92
+ * example of reactive application
93
+ * excel: if you modify one cell, it propagates to other cells
94
+ * google docs - hundreds of people use it simultaneously
95
+ ### java 8 streams
55
96
* stream is not a data structure it is an abstraction of functions (with a data source: network, file, etc.)
56
97
* Martin Fowler: [Collection Pipeline Pattern](https://martinfowler.com/articles/collection-pipeline/)
57
98
* it is actually a collection of functions
@@ -60,21 +101,56 @@ of concurrent code
60
101
* single pipeline (a single terminal operation)
61
102
* cannot split into two
62
103
* no exceptions handling
63
- * reactive programming (idea formulated by Eric Meijer)
64
- * the applications we developer, the programs we create must
65
- be really responsive and be able to react to stimuli in a
66
- system
104
+ ### reactive streams
105
+ * vs java 8 streams
106
+
107
+ |java streams |reactive streams |
108
+ |---|---|
109
+ |pipeline |pipeline |
110
+ |push data |push/pull data |
111
+ |lazy |lazy |
112
+ |0, 1, oo |0, 1, oo |
113
+ |data only |3 channels: data, error, complete |
114
+ |exceptions: good luck |deal with it downstream (error is just another form of data) |
115
+ |sequential vs parallel |synch vs async |
116
+ |single pipeline (one terminal operation) |multiple subscribers |
117
+ * vs CompletableFuture
118
+ |reactive streams |CompletableFuture/Promises |
119
+ |---|---|
120
+ |0, 1, oo |0, 1 |
121
+ |3 channels |2 channels (data, error) |
122
+ * nonblocking backpressure
123
+ * BUFFER - buffers all onNext values until the downstream consumes it
124
+ * DROP - drops the most recent onNext value if the downstream can't keep up
125
+ * ERROR - signals a MissingBackpressureException in case the downstream can't keep up
126
+ * LATEST - keeps only the latest onNext value, overwriting any previous value if the downstream can't keep up
127
+ * MISSING - OnNext events are written without any buffering or dropping
128
+ * hot vs cold
129
+ * cold = every subscriber starts fresh subscription
130
+ * like iterator, if you start again you start from the
131
+ beginning
132
+ * hot = start from a point in time, like match online transmission
133
+ ### past and future
67
134
* times when you have to go to bank and talk with a person,
68
135
times when you have to go to travel agency to buy tickets
69
136
* in the past companies made products for their employees to use
70
137
and make those employees (nobody cares what they think) available to us the customers
71
- * now companies build product for real-users, IOT
72
- * In push protocols, the client opens a connection to the server and keeps it constantly active. The server will
73
- send (push) all new events to the client using that single always-on connection. In other words, the server PUSHes
74
- the new events to the client.
75
- * In pull protocols, the client periodically connects to the server, checks for and gets (pulls) recent events and
76
- then closes the connection and disconnects from the server. The client repeats this whole procedure to get updated
77
- about new events. In this mode, the clients periodically PULLs the new events from the server.
138
+ * now companies build product for real-users, IOT
139
+ ### push vs pull protocols
140
+ * push protocols:
141
+ * the client opens a connection to the server and keeps it constantly active
142
+ * the server will send (push) all new events to the client using that single always-on connection
143
+ * in other words, the server PUSHes the new events to the client
144
+ * pull protocols:
145
+ * the client periodically connects to the server, checks for and gets (pulls) recent events and
146
+ then closes the connection and disconnects from the server
147
+ * the client repeats this whole procedure to get updated about new events
148
+ * in this mode, the clients periodically PULLs the new events from the server
149
+ * Observable vs Observer pattern
150
+ * it's that, plus
151
+ * signal end of data stream
152
+ * propagate error
153
+ * evaluation may be synchronous, asynchronous or lazy
78
154
```
79
155
private static void pullExample() {
80
156
final List<String > list = Lists.newArrayList("Java", "C", "C++", "PHP", "Go");
@@ -94,87 +170,10 @@ private static void pushExample() {
94
170
observable.subscribe(System.out::println, System.out::println, () -> System.out.println("We are done!"));
95
171
}
96
172
```
97
- * https://www.reactivemanifesto.org/
98
- * OOP four pillars: abstraction, encapsulation, inheritance, polymorphism
99
- * reactive four pillars
100
- * responsive
101
- * infinite-scrolling
102
- * providing responsiveness
103
- * efficiency is attained not by doing tasks faster, but
104
- by avoiding those that shouldn't be done in the first place
105
- * resilient: make failure first-class citizen (it is okay
106
- to fail)
107
- * elastic - the only reasonable direction to scale is horizontally
108
- * message driven - do not expose your database instead
109
- export your data
110
- * how many threads should you create?
111
- * computation intensive <= # of cores
112
- * IO intensive = ...
113
- * http://reactivex.io/RxJava/2.x/javadoc/io/reactivex/schedulers/Schedulers.html
114
- * Schedulers.io(), Schedulers.computation()
115
- * https://github.com/mtumilowicz/java12-nio-non-blocking-polling-server-workshop
116
- * number of threads is therefore strictly limited
117
- * by memory also
118
- * example of reactive application
119
- * excel: if you modify one cell, it propagates to other cells
120
- * google docs - hundreds of people use it simultaneously
121
-
122
- |java streams |reactive streams |
123
- |---|---|
124
- |pipeline |pipeline |
125
- |push data |push/pull data |
126
- |lazy |lazy |
127
- |0, 1, oo |0, 1, oo |
128
- |data only |3 channels: data, error, complete |
129
- |exceptions: good luck |deal with it downstream (error is just another form of data) |
130
- |sequential vs parallel |synch vs async |
131
- |single pipeline (one terminal operation) |multiple subscribers |
132
-
133
- |reactive streams |CompletableFuture/Promises |
134
- |---|---|
135
- |0, 1, oo |0, 1 |
136
- |3 channels |2 channels (data, error) |
137
-
138
- * nonblocking backpressure
139
- * BUFFER - Buffers all onNext values until the downstream consumes it.
140
- * DROP - Drops the most recent onNext value if the downstream can't keep up.
141
- * ERROR - Signals a MissingBackpressureException in case the downstream can't keep up.
142
- * LATEST - Keeps only the latest onNext value, overwriting any previous value if the downstream can't keep up.
143
- * MISSING - OnNext events are written without any buffering or dropping.
144
-
145
- * The actual goal of having them included in the JDK is to provide something called a Service Provider Interface
146
- (or SPI) layer. This should eventually serve as a unification layer for different components that have reactive
147
- and streaming nature, but may expose their own custom APIs, and thus not be able to interoperate with other similar
148
- implementations
149
-
150
- * Observable vs Observer pattern
151
- * it's that, plus
152
- * signal end of data stream
153
- * propagate error
154
- * evaluation may be synchronous, asynchronous or lazy
155
- * hot vs cold
156
- * cold = every subscriber starts fresh subscription
157
- * like iterator, if you start again you start from the
158
- beginning
159
- * hot = start from a point in time, like match online transmission
160
- * circuit breakers
161
- * you cannot wait to process to get an exception, fail fast
162
- * if you are busy circuit breaker should notify the client
163
- instead of you throwing an error
164
- * functional programming and exceptions are mutually exclusive
165
- * handling exceptions imperative style: if you are driving a car and
166
- have flat tire - the most illogical thing to do is to reverse back
167
- * you should exit safely
168
- * you could block data, buffer on client side, buffer on producer side,
169
- and backpressure
170
- * backpressure: info w przeciwnym kierunku, wolniejszy downstream informuje
171
- szybszy upstream
172
- * backpressure musi być nieblokujący
173
173
* model domeny uwzglednia wolnych konsumentow
174
174
* np. Twitter API - informuje o tym ze za wolno pobieramy, jak
175
175
nie przyspieszymy to polaczenie zostanie zerwane
176
176
* https://rxmarbles.com/
177
- * core idea behind reactive is to release resource whenever possible
178
177
179
178
## definitions
180
179
* **[Flow.Publisher](https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/Flow.Publisher.html)** -
@@ -246,6 +245,12 @@ because we want our `Subscriber` talk to only one `Publisher` -
246
245
`Publisher` so you have to cancel the incoming one (if we already have
247
246
one, we don't accept any furthers).
248
247
248
+ * the actual goal of having them included in the JDK is to provide something called a Service Provider Interface
249
+ (or SPI) layer
250
+ * this should eventually serve as a unification layer for different components that have reactive
251
+ and streaming nature, but may expose their own custom APIs, and thus not be able to interoperate with other similar
252
+ implementations
253
+
249
254
## tests
250
255
We test it by running:
251
256
* `RunAnswer`
0 commit comments