Before learning webflux we need to understand a concept
What is the back pressure (back press)
To publish subscribe model that we previously used for our previous data consumption patterns are only supplied by the provider to sustainable consumption If you have been to send a message, it can only have been consumer
We make a detailed metaphor for back pressure
For example, our every household, there is a faucet. Water Company is equivalent to a publisher who our family is a subscriber, water is the data, before the pattern of our subscribers is a passive acceptance of the concept
Back pressure is equivalent to our house to install a faucet, we need to put him to open No time to put him to close
reactive stream
Responsive flow. This is jdk9 set of standards introduced, he was able to achieve very good back pressure, but I Quguan network, I found jdk9 has ended. We look at it jdk11
jdk11 interface to a flow which has four methods
1.publisher is publisher
subscribe: that is, subscribers and generate a relationship
2.subscribe is subscriber
onSubscribe: the signing of a subscription relationship Incoming subscription
onNext (): a received data
onError (): is wrong
onComplete (): complete
3.Subscription interface is the key to implement these methods in which the request back pressure is tell the publisher how many resources I need, where the publisher would publish the amount of resources
4.Processor can only do publishers, subscribers can do, specifically for data processing of intermediate links
Simple example let's run down
Every publisher told me after processing can also handle the data is how much
public static void main(String[] args) throws InterruptedException { //1.定义发布者 SubmissionPublisher<Integer> publisher = new SubmissionPublisher<>(); //2. 定义订阅者 Flow.Subscriber<Integer> subscriber = new Flow.Subscriber<Integer>() { private Flow.Subscription subscription; int total = 0; @Override public void onSubscribe(Flow.Subscription subscription) { //保存订阅关系 this= .subscription Subscription; // request a data subscription.request (. 1 ); } @Override public void OnNext (Item Integer) { System.out.println ( "received:" + Item); Total ++ ; the System.out .println ( "the number of accepted is:" + Total); the this .subscription.request (1 ); // or reach a certain number tell the publisher does not accept data IF (Total == 10 ) { the this .subscription.cancel (); System.out.println ( "enough to accept data" ); } } @Override public void the onError (the Throwable Throwable) { Throwable.printStackTrace (); // throws an exception return the this .subscription.cancel (); } @Override public void the onComplete () { System.out.println ( "data processing finished." ); } }; // 3 publish and subscribe to establish subscription relationship publisher.subscribe (Subscriber); // 4. production data for ( int I = 0; I <100; ++ I ) { publisher.submit(i); } //5.关闭发布者 publisher.close(); Thread.currentThread().join(5000); }
processor
public static void main (String [] args) throws InterruptedException { // 1. publisher defined SubmissionPublisher <Integer> = Publisher new new SubmissionPublisher <> (); // 2. Define a data processor filtered, and converted to a string type MyProcessor myProcessor = new new MyProcessor (); // 3. publisher relationship with the processor publisher.subscribe (myProcessor); // 4. define the final subscriber Flow.Subscriber <String> = subscriber new new Flow.Subscriber <String > () { Private Flow.Subscription Subscription; @Override public void onSubscribe (Flow.Subscription Subscription) { // save subscription relationship the this .subscription = Subscription; // request a data subscription.request (. 1 ); } the LinkedList <String> List = new new the LinkedList <> (); @Override public void OnNext (String Item) { list.add (Item); the this .subscription.request (1 ); // or reach a certain number tell the publisher does not accept data IF System.out.println (Item); (list.size () == 10 ) { the this .subscription.cancel (); System.out.println ( "enough to accept data" ); the this .onComplete (); } } @Override public void the onError (the Throwable Throwable) { Throwable.printStackTrace (); // throws an exception return the this .subscription.cancel (); } @Override public void the onComplete () { System.out.println ( "data processing finished . "+ list.toString ()) ; } }; // . 5 and a final processor subscriber relationship myProcessor.subscribe (Subscriber); // 4. Production data for ( int I = 0; I <100; I ++ ) { Publisher. Submit (I); } // 5. The closed publisher publisher.close (); . Thread.currentThread () the Join ( 5000 ); } static class MyProcessor the extends SubmissionPublisher <String> the implements Flow.Processor <Integer, String> { Private Subscription Flow.Subscription; @Override public void onSubscribe(Flow.Subscription subscription) { this.subscription = subscription; subscription.request(1); } @Override public void onNext(Integer item) { // System.out.println("processor-> 处理器接收到的数据.."+item); if(item % 2 ==0){ this.submit("转->" +item); } this.subscription.request(1); } @Override public void onError(Throwable throwable) { Throwable.printStackTrace (); the this.subscription.cancel (); } @Override public void the onComplete () { System.out.println ( "Processor processor has processed!" ); } }
Inside the operating mechanism
publiser.submit (): is a blocking method
Subscribers have a pool. When the pool is full submit () method will be blocked. This will not go to the production data
subscription default buffer capacity is 256.