GOTO 2013 • Functional Reactive Programming with RxJava • Ben Christensen

By: GOTO Conferences

376   6   42550

Uploaded on 04/08/2014

This presentation was recorded at GOTO Aarhus 2013
http://gotocon.com

Ben Christensen - Software Engineer at Netflix

ABSTRACT
Rxjava is a library for composing asynchronous and event-based programs using observable sequences for the Java VM that supports Java 6+, Clojure, Scala, Groovy, JRuby and Java 8 lambdas.

Learn how the Netflix API uses RxJava (http://techblog.netflix.com/2013/02/r...) to implement highly concurrent web services against asynchronous data sources without blocking, synchronization or thread-safety concerns by using declarative functional reactive composition.

Come see what functional reactive programming looks like, how it fits into Java, what use cases it addresses and real-world examples of how it can become a tool in your application development.

https://twitter.com/gotocon
https://www.facebook.com/GOTOConference
http://gotocon.com

Comments (2):

By anonymous    2017-09-20

Futures

Futures were introduced in Java 5 (2004). They are objects that promise to hold the result of an operation once that operation completes. For example when a task (i.e Runnable or Callable) is submitted to an executor. The caller can use the future object to check whether the operation isDone(), or wait for it to finish using get().

Example:

/**
* A task that sleeps for a second, then returns 1
**/
public static class MyCallable implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        Thread.sleep(1000);
        return 1;
    }

}

public static void main(String[] args) throws Exception{
    ExecutorService exec = Executors.newSingleThreadExecutor();
    Future<Integer> f = exec.submit(new MyCallable());

    System.out.println(f.isDone()); //False

    System.out.println(f.get()); //Waits until the task is done, then prints 1
}

CompletableFutures

CompletableFutures were introduced in Java 8 (2014). Completable futures are in fact an evolution of regular Futures, inspired by Google's Listenable Futures, part of the Guava libary. They are Futures that also allow you to string tasks together in a chain. You can use them to tell some worker thread to "go do some task X, and when you're done, go do this other thing using the result of X". Here's a simple example:

/**
* A supplier that sleeps for a second, and then returns one
**/
public static class MySupplier implements Supplier<Integer> {

    @Override
    public Integer get() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            //Do nothing
        }
        return 1;
    }
}

/**
* A (pure) function that adds one to a given Integer
**/
public static class PlusOne implements Function<Integer, Integer> {

    @Override
    public Integer apply(Integer x) {
        return x + 1;
    }
}

public static void main(String[] args) throws Exception {
    ExecutorService exec = Executors.newSingleThreadExecutor();
    CompletableFuture<Integer> f = CompletableFuture.supplyAsync(new MySupplier(), exec);
    System.out.println(f.isDone()); // False
    CompletableFuture<Integer> f2 = f.thenApply(new PlusOne());
    System.out.println(f2.get()); // Waits until the "calculation" is done, then prints 2
}

RxJava

RxJava is whole library for reactive programming created by Netflix. At a glance, it will appear to be similar to Java 8's streams. It is, except it's much more powerful.

Similarly to Futures, RxJava can be used to string together a bunch of synchronous or asynchronous actions to produce one or more meaningful results. However unlike Futures, which are single-use, RxJava works on streams of zero or more items including never-ending streams with an infinite number of items. It's also much more flexible and fluent thanks to an unbelievably rich set of operators.

Also, unlike Java 8's streams, RxJava has a back-pressure mechanism (see excellent explanation here), which allows it it handle push streams where the producer "pushes" the next item into the pipeline, instead of the pipeline pulling each next item from the producer when the pipeline is ready to process it.

The downside of Rx is that despite the pretty good documentation, it's a challenging library to learn due to the paradigm shift involved. Rx code can also be a nightmare to debug, especially if multiple threads are involved, and even worse - if back-pressure is needed. If you want to get into it, there's a whole page of various tutorials on the official website, plus the official documentation and Javadoc. There's also the SO documentation. You can also take a look at some of the videos such as this one which gives a brief intro into Rx and also talks about the differences between Rx and Futures.

Bonus: Java 9 Reactive Streams

Java 9's Reactive Streams aka Flow API are a set of Interfaces implemented by various reactive streams libraries such as RxJava 2, Akka Streams, and Vertx. They allow these reactive libraries to interconnect, while preserving the all important back-pressure.

Original Thread

Popular Videos 165

Submit Your Video

If you have some great dev videos to share, please fill out this form.