$30 off During Our Annual Pro Sale. View Details »

Get ready for java.util.concurrent.Flow!

Get ready for java.util.concurrent.Flow!

Slides from the presentation regarding Reactive Streams implementations and Reactive Streams standard introduced in Java 9. This presentation was shown during JDD Conference on the 3rd of October 2017. I've also presented this talk during JUG meetup in Kraków on 4th of December 2017, infoMEET conference in Wrocław on 16th of December 2017 and Hackin' Gliwice mini-conference in Gliwice on 15th of March 2018.

Piotr Wittchen

October 03, 2017
Tweet

More Decks by Piotr Wittchen

Other Decks in Programming

Transcript

  1. View Slide







  2. View Slide

  3. List list = Arrays.asList(1, 2, 3, 4, 5);
    for (Integer n : list) {
    if (n % 2 == 0) {
    operate(n);
    }
    }

    View Slide









  4. View Slide

  5. View Slide

  6. Publisher pushes data to Subscriber
    onSubscribe
    onNext
    onError
    onComplete

    View Slide

  7. View Slide

  8. List list = Arrays.asList(1, 2, 3, 4, 5);
    for (Integer n : list) {
    if (n % 2 == 0) {
    operate(n);
    }
    }

    View Slide

  9. List list = Arrays.asList( , , , , );
    for (Integer n : list) {
    if (n % 2 == 0) {
    operate(n);
    }
    }
    1 2 4
    3 5

    View Slide

  10. 1 2 4
    3 5
    filter(n -> n % 2 == 0)
    2 4

    View Slide

  11. 1 2 4
    3 5
    Flowable.fromArray( , , , , )
    .filter(n -> n % 2 == 0)
    .subscribe(n -> operate(n));

    View Slide

  12. Flowable.fromArray(1, 2, 3, 4, 5)
    .filter(n -> n % 2 == 0)
    .subscribe(n -> operate(n));

    View Slide

  13. View Slide

  14. View Slide

  15. ● RxJava 1



    ● RxJava 2



    View Slide

  16. ● Observable


    ● Completable


    ● Flowable


    ● Single

    ● Maybe

    View Slide

  17. Observable.range(1, 10)
    .map(i -> i * 100)
    .doOnNext(i -> printNumberWithThreadInfo("emitting", i))
    .map(i -> i * 10)
    .subscribeOn(Schedulers.computation())
    .observeOn(Schedulers.newThread())
    .subscribe(integer -> printNumberWithThreadInfo("received", integer));

    View Slide

  18. Observable.range(1, 10)
    .map(i -> i * 100)
    .doOnNext(i -> printNumberWithThreadInfo("emitting", i))
    .map(i -> i * 10)
    .subscribeOn(Schedulers.computation())
    .observeOn(Schedulers.newThread())
    .subscribe(integer -> printNumberWithThreadInfo("received", integer));
    observeOn(...)
    observeOn(...) subscribeOn(...)

    View Slide

  19. View Slide

  20. List list = new ArrayList<>();
    new Thread(() -> ((Callback) () -> {
    list.add("one");
    ((Callback) () -> {
    list.add("two");
    ((Callback) () -> {
    list.add("three");
    System.out.println(list.stream().collect(Collectors.joining(" ")));
    }).execute();
    }).execute();
    }).execute()).start();




    View Slide

  21. Flowable
    .fromCallable(() -> "one")
    .flatMap(name -> Flowable.fromCallable(() -> name.concat(" two")))
    .flatMap(name -> Flowable.fromCallable(() -> name.concat(" three")))
    .subscribe(System.out::println);

    View Slide

  22. flowable.subscribe(new Subscriber() {
    @Override public void onSubscribe(Subscription s) {
    }
    @Override public void onNext(Object o) {
    }
    @Override public void onError(Throwable throwable) {
    System.out.println(throwable.getMessage());
    }
    @Override public void onComplete() {
    }
    });
    Subscriber

    View Slide

  23. Map words = new HashMap();
    Flowable
    .fromArray(1, 2, 3, 4, 5)
    .flatMap(i -> Flowable.just(i * 2))
    .map(words::get)
    .throttleWithTimeout(1, TimeUnit.SECONDS)
    .lastElement()
    .subscribe(System.out::println);

    View Slide

  24. sample( ) throttleLast( ) throttleFirst( )
    throttleWithTimeout( ) debounce( )
    BUFFER LATEST DROP
    Flowable

    View Slide

  25. View Slide

  26. View Slide

  27. View Slide

  28. Flux.just(1, 2, 3, 4)
    .subscribe(System.out::println);
    Mono.just("foo")
    .subscribe(System.out::println);
    Flowable
    Single

    View Slide

  29. List list = new ArrayList<>();
    ActorSystem actorSystem = ActorSystem.create();
    ActorMaterializer mat = ActorMaterializer.create(actorSystem);
    Source source = Source.range(1, 5);
    source.runForeach(list::add, mat);

    View Slide

  30. Flowable flowable = Flowable.just(1, 2, 3, 4);
    Flux flux = Flux.just(5, 6, 7, 8);
    flowable
    .mergeWith(flux)
    .sorted()
    .subscribe(System.out::println);
    io.reactivex.Flowable reactor.core.publisher.Flux
    org.reactivestreams.Publisher java.util.concurrent.Flow.Publisher

    View Slide




  31. ● provide interfaces
    are their implementations


    View Slide

  32. Publisher pushes data to Subscriber
    onSubscribe
    onNext
    onError
    onComplete
    java.util.concurrent.Flow

    View Slide

  33. Flow.Publisher publisher = subscriber -> {
    … // to be implemented
    };
    publisher.subscribe(new Flow.Subscriber<>() {
    @Override public void onSubscribe(Flow.Subscription subscription) {
    ...
    }
    @Override public void onNext(Integer item) {
    ...
    }
    @Override public void onError(Throwable throwable) {
    ...
    }
    @Override public void onComplete() {
    ...
    }
    });
    Publisher
    Subscriber

    View Slide

  34. Just for a presentation demo it’s better to use
    production ready libraries
    has to pass TCK (Technology
    Compatibility Kit) tests to keep RS ecosystem healthy!

    View Slide

  35. View Slide

  36. View Slide

  37. ● part of Java 9 language
    ● interfaces for reactive applications without implementations
    ● external production ready implementations RxJava Reactor Akka Stream
    ● have to be compatible with TCK
    ● data transformation
    ● callback hell
    ● asynchronous operations
    ● efficient consumption of the hardware resources
    ● error handling API
    ● can be used anywhere
    ● clean maintainable concise modern

    View Slide

  38. Thank you for the attention!
    https://github.com/pwittchen/java-flow-experiments
    https://speakerdeck.com/pwittchen

    View Slide