A framework that Implements and facilitates reactive programming with a support for parallel processing and back pressure
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependency>
<groupId>com.github.mosup16</groupId>
<artifactId>flow-framework</artifactId>
<version>439d7dd0b4</version>
</dependency>
var data = IntStream.range(0, 1000)
.boxed()
.collect(Collectors.toList());
Function<Integer, Integer> func = integer -> {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
return integer + 1;
};
Flow.of(data)
.parallelMap(16, func)
.filter(integer -> integer > 40)
.map(integer -> "hi : " + integer)
.forEach(x -> System.out.println(Thread.currentThread().getId() + " :" + x));
.parallelMap(numOfThreads, LoadBalancingStrategy.ROUND_ROBIN , func)
also note that round robin is the default strategy.
.parallelMap(numOfThreads, LoadBalancingStrategy.LEAST_BUFFER_SIZE , func)
this strategy tries to pick the least busy thread with the least buffer size every time
var configs = new BackPressureConfigs(maxBufferSizePerThread, maxBufferedMessagesForAllThreads);
Flow.of(data, configs)
If the buffered messages of any thread reached maxBufferSizePerThread
,
then this thread will be overloaded and the load will be shifted away to other threads.
If all threads were overloaded or the count of all buffered messages for all threads reached maxBufferedMessagesForAllThreads
,
then the flow will be throttled and the DataSource
implementation will define the throttling behaviour.
Note that if any of maxBufferSizePerThread
or maxBufferedMessagesForAllThreads
variables was given a value less than 1,
this means that they will be ignored and considered as if they were infinity.