2019年2月15日金曜日

Reactive Programming with Java 9

In Java 9 and later, a function called Flow API is provided. With this, it is possible to perform Reactive Programming with only the Java JDK. As a result, it is possible to appropriately control events occurring asynchronously in a distributed system or multithreadwithout performing low level programming. I found a nice commentary on this. It is as follows:

“The Reactive Magazine Publisher” by Moisés Macero

This is an easy-to-understand explanation using meaningful examples that actually operate, and there are many values ​​to learn. For example, consider a magazine delivery system consisting of Publisher and Customers (Subscribers, Consumers). There, some customers receive magazines very slowly. Then, since the delivery box of Publisher overflows, the next magazine will need to be dropped from that box. In this way, Publisher will react according to the Subscriber's situation.

There are several ways to create such a system or simulator. However, here it is meaningful to high level programming using the Publisher-Subscriber pattern in the above Java JDK. This example allows you to observe the behavior of the system when parameters such as Subscribers' receipt speed, Publisher's box capacity, etc. are changed. If the subscriber is slower than the publisher, it functions like a pull-based system. Conversely, if the subscriber is fast it will be like a push-based system.

I made a visualization to make the behavior of this example easier to understand. The results are shown below. One turtle shown in the figure means one magazine. Please see the execution progress by matching the output text with the figure!
(also see the short video below.)



Please enlarge and see the diagram.






As a conclusion of this article, the author says:
“You shouldn’t be in a rush of applying this pattern in a real project, but it’s great when you know it since it might happen in future that you find a problem for which Reactive Programming fits perfectly: then you’ll gain a lot. “

I agree with this. Thanks again to Moisés Macero!

0 件のコメント:

コメントを投稿