(Reactor) 3 operators---response spring's word spell device

Source: Internet
Author: User
Tags pack

This series of articles index the "Response Spring's word Wizard"
Previously summary Reactor 3 Quick Start | Responsive Flow Specification

2.5 Reactor 3 Operators

Although operator is not required in the Response Flow specification (hereinafter referred to as the "operator"), the reactor also provides a very rich operator, as is the response development library such as Rxjava.

2.5.1 Rich operator

Some of the commonly used operators are introduced in the preceding articles in this series. But that's just the tip of the iceberg, Reactor 3 provides a rich operator, if you want an introduction, that space to go, to give people to fish than to give people to fishing, we can learn the following ways to understand the application of the operator scenario, familiar with their use method:

    1. Attached 2 is a translation of the section "How to choose the right operator" in the Reactor 3 reference document, which describes how to select the appropriate operator.
    2. Refer to the explanations of flux and mono in Javadoc.
    3. If you want to try out the various operators by the way of actual combat, we strongly recommend the official Lite-rx-api-hands-on project from reactor. After you get the project, all you have to do is use the operator to complete the "TODO" code so that all the @Test green lights are OK. After completing these tests, you will be able to know your chest for common operators.
    4. In addition, in the daily development process, through the IDE can also be consulted at any time, such as IntelliJ:

Because project reactor's core development team also has a Daniel from Rxjava, and reactor itself in the development process also borrowed from the majority of Rxjava operator name (for Rxjava in a small number of well-named operators are optimized), Therefore, for the familiar Rxjava friends, the use of reactor basic lack of learning costs. Similarly, after studying reactor, it is no problem to use Rxjava again.

2.5.2 "Pack" operator

In our development process, in order to keep the code concise, we typically encapsulate a series of operations that we use frequently in a method for invocation.

Reactor also provides a similar "pack and go" approach to the operator.

1) using the transform operator

transformYou can package a chain of operations into a single functional type. This function restores the encapsulated operator and accesses it to the calling location during the assembly period transform . This is the same as the effect of adding the encapsulated operator directly to the chain. Examples are as follows:

    @Test    public void testTransform() {        Function<Flux<String>, Flux<String>> filterAndMap =                f -> f.filter(color -> !color.equals("orange"))                        .map(String::toUpperCase);        Flux.fromIterable(Arrays.asList("blue", "green", "orange", "purple"))                .doOnNext(System.out::println)                .transform(filterAndMap)                .subscribe(d -> System.out.println("Subscriber to Transformed MapAndFilter: "+d));    }

In this example, the filterAndMap function is packaged with the filter operator and map then handed to the transform to be assembled into the operation chain. The output is as follows:

blueSubscriber to Transformed MapAndFilter: BLUEgreenSubscriber to Transformed MapAndFilter: GREENorangepurpleSubscriber to Transformed MapAndFilter: PURPLE

2) using the Compose operator

The compose operator is similar to transform and can encapsulate several operators into a single function. The main difference is that this function works for each subscriber . This means that it can generate a different chain of operations for each subscription. As an example:

  public void Testcompose () {Atomicinteger ai = new Atomicinteger (); Function<flux<string>, flux<string>> Filterandmap = f, {if (ai.incrementandget () = = 1)            {return F.filter (color-!color.equals ("orange")). Map (String::touppercase);        } return F.filter (Color-!color.equals ("purple")). Map (String::touppercase);        };                        flux<string> Composedflux = flux.fromiterable (Arrays.aslist ("Blue", "green", "orange", "Purple"))        . Doonnext (System.out::p rintln). Compose (Filterandmap);        Composedflux.subscribe (D-SYSTEM.OUT.PRINTLN ("Subscriber 1 to composed Mapandfilter:" + D));    Composedflux.subscribe (D-SYSTEM.OUT.PRINTLN ("Subscriber 2 to composed Mapandfilter:" + D)); }

In this example, the Filterandmap function has a ai state value called self-increment. Each time a method is called to subscribe subscribe, it compose causes a ai self-increment, so that the chain of operations for two subscriptions is different. The output is as follows:

blueSubscriber 1 to Composed MapAndFilter :BLUEgreenSubscriber 1 to Composed MapAndFilter :GREENorangepurpleSubscriber 1 to Composed MapAndFilter :PURPLEblueSubscriber 2 to Composed MapAndFilter: BLUEgreenSubscriber 2 to Composed MapAndFilter: GREENorangeSubscriber 2 to Composed MapAndFilter: ORANGEpurple

In other words, the compose functional formula in the package can be stateful (stateful):

The transform functional formula of packing is stateless. will be compose replaced by transform re-execution, found that the two subscription operation chain is the same, will be filtered out orange .

(Reactor) 3 operators---response spring's word spell device

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.