Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

New java9 feature Reactive Stream responsive programming how to use API

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use Reactive Stream responsive programming API, the new feature of java9". In daily operation, I believe many people have doubts about how to use Reactive Stream responsive programming API, the new feature of java9. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use java9 new feature Reactive Stream responsive programming API". Next, please follow the editor to study!

1. Java9 Reactive Stream API

Java 9 provides a set of interfaces that define responsive flow programming. All of these interfaces are defined in the java.util.concurrent.Flow class as static internal interfaces.

Here are some important roles and concepts in Java responsive programming, let's briefly understand

Publishers (Publisher) are producers of potentially unlimited number of ordered data elements. It publishes a certain number of data elements to the current subscriber based on the subscription received.

The subscriber (Subscriber) subscribes and receives data elements from the publisher. After establishing a subscription relationship with the publisher, the publisher sends a subscription token (subscription) to the subscriber, and the subscriber can request the publisher to publish the number of data elements according to his processing power.

A subscription token (subscription) represents the subscription relationship established between the subscriber and the publisher. When a subscription relationship is established, the publisher delivers it to the subscriber. Subscribers use subscription tokens to interact with publishers, such as requesting the number of data elements or unsubscribing.

Second, Java responsive programming four interfaces 2.1.Subscriber Interface (subscriber subscription interface) public static interface Subscriber {public void onSubscribe (Subscription subscription); public void onNext (T item); public void onError (Throwable throwable); public void onComplete ();}

OnSubscribe: after the publisher accepts the subscriber's subscription action, it is called before publishing any subscription messages. The newly created Subscription subscription token object is passed to the subscriber in this method.

OnNext: the handler of the next data item to be processed

OnError: called when a publisher or subscription encounters an unrecoverable error

OnComplete: called when no subscriber calls (including the onNext () method) occur.

2.2.Subscription Interface (subscription token interface)

The subscription token object is passed through the Subscriber.onSubscribe () method

Public static interface Subscription {public void request (long n); public void cancel ();}

Request (long n) is the key method behind the concept of non-blocking back pressure. Subscribers use it to request more than n consumer items. In this way, the subscriber controls how much data it can currently receive. Cancel () subscribers take the initiative to cancel their subscriptions and will not receive any data messages after cancellation.

2.3.Publisher Interface (publisher interface) @ FunctionalInterfacepublic static interface Publisher {public void subscribe (Subscriber)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report