Skip to content

Latest commit

 

History

History
530 lines (339 loc) · 14.5 KB

File metadata and controls

530 lines (339 loc) · 14.5 KB

RX-JAVA in Practice

As a Knowledge Sharing session at Networked Assets
By JAROSŁAW WATRAL


What RxJava is

And what it isn't.

+++

Image-Relative

+++

RxJava is a Java VM implementation of Reactive Extensions: a library for composing asynchronous and event-based programs by using observable sequences.

+++

Image-Relative

+++

Who uses RXJAVA?

Image-Relative


[OFFTOP] Will the name RXJAVA stay? ;-)

+++

+++

Everything's working great - let's order some stickers!

Image-Relative

+++

But then "all the evil" happened

+++

And the bad guys arrived

+++

Image-Relative

+++

The only sensible reaction...

+++

Image-Relative

+++

Fortunately, Engineers always find easy solutions

+++

Image-Relative

+++

And that's how Vavr.io was born

+++

Should we expect Rx Vavr?

Let's ask Oracle's Legal Department ;-)


Story of Netflix

See this article for details.

+++

Embrace Concurrency

Note: Server-side concurrency is needed to effectively reduce network chattiness. Without concurrent execution on the server, a single “heavy” client request might not be much better than many “light” requests because each network request from a device naturally executes in parallel with other network requests. If the server-side execution of a collapsed “heavy” request does not achieve a similar level of parallel execution it may be slower than the multiple “light” requests even accounting for saved network latency.

+++

Java Futures are Expensive to Compose

Note: Java Futures are straight-forward to use for a single level of asynchronous execution but they start to add non-trivial complexity when they’re nested (prior to Java 8 CompletableFuture).
Conditional asynchronous execution flows become difficult to optimally compose (particularly as latencies of each request vary at runtime) using Futures. It can be done of course, but it quickly becomes complicated (and thus error prone) or prematurely blocks on ‘Future.get()’, eliminating the benefit of asynchronous execution.

+++

Callbacks Have Their Own Problems

Note: Callbacks offer a solution to the tendency to block on Future.get() by not allowing anything to block. They are naturally efficient because they execute when the response is ready. Similar to Futures though, they are easy to use with a single level of asynchronous execution but become unwieldy with nested composition.

+++

Some of the goals of Netflix were:

  • Stay close to the original Rx.Net implementation while adjusting naming conventions and idioms to Java
  • All contracts of Rx should be the same
  • Target the JVM not a language. New language adapters can be contributed.
  • Support Java 6 (to include Android support) and higher with an eventual goal to target a build for Java 8 with its lambda support. (Update: Java 8 support was achieved without a separate build)

+++

Let's hear what they have to say:

+++

VideoService example

YouTube Video

+++

  getListOfLists(userId).mapMany({ VideoList list ->
    list.getVideos() // for each VideoList we want to fetch the videos
      .take(10) // we only want the first 10 of each list
      .flatMap({ Video video -> 
        def m = video.getMetadata().map({ // for each video we want to fetch metadata
          Map<String, String> md -> 
          return [title: md.get("title"), // transform to the data and format we want
              length: md.get("duration")]
        })
        def b = video.getBookmark(userId).map({ 
          position -> return [bookmark: position]
        })
        def r = video.getRating(userId).map({ 
          VideoRating rating -> 
          return [rating: 
            [actual: rating.getActualStarRating(),
             average: rating.getAverageStarRating(),
             predicted: rating.getPredictedStarRating()]]
        })
        return Observable.zip(m, b, r, {
                metadata, bookmark, rating -> 
            return [id: video.videoId] << metadata << bookmark << rating
        })
      })   
  })

Note: /**

  • Demonstrate how Rx is used to compose Observables together
  • such as how a web service would to generate a JSON response.
  • The simulated methods for the metadata represent different
  • services that are often backed by network calls.
  • This will return a sequence of dictionaries such as this:
  • [id:1000, title:video-1000-title, length:5428, bookmark:0,
  • rating:[actual:4, average:3, predicted:0]] */

Cool links

Markdown Slides

Press Down key for details. | See GitPitch Wiki for details.

+++

Use GitHub Flavored Markdown

For Slide Content Creation


The same tool you use to create project READMEs and Wikis for your Git repos.


Code Slides

Press Down key for examples. | See GitPitch Wiki for details.

+++

Use Markdown Code Blocks


And enjoy code syntax highlighting for dozens of languages powered by highlight.js.

+++

// JavaScript Code Block

$('button').click(function(){
    $('h1, h2, p').addClass('blue')
    $('div').removeClass('important')
    $('h3').toggleClass('error')
    $('#foo').attr('alt', 'Lorem Ipsum')
});

+++

// Scala Code Block

HashMap params = HashMap(n -> 10, mean -> 5)

// Define executable for R stats#rnorm function call.
OCPUTask task = OCPU.R()
                    .pkg("stats")
                    .function("rnorm")
                    .input(params.asJava)
                    .library()

+++

// Go Code Block

package main

import "fmt"

func swap(x, y string) (string, string) {
    return y, x
}

func main() {
    a, b := swap("hello", "world")
    fmt.Println(a, b)
}

GIST Slides

Press Down key for examples. | See GitPitch Wiki for details.

+++

GitHub GIST

Building Blocks For Any Presentation


Enjoy 100% reusable code snippets, excellent syntax highlighting, code indentation and styling.

+++?gist=8da53731fd54bab9d5c6

+++?gist=28ee3d19ddef9d51b15adbdfe9ed48da


Image Slides

[ Inline ]

Press Down key for examples. | See GitPitch Wiki for details.

+++

Make A Visual Statement


Use inline images to lend a visual punch to your slideshow presentations.

+++

Inline Image at Absolute URL

Image-Absolute

the Private Investocat by jeejkang

+++

Inline Image at GitHub Repo Relative URL

Image-Absolute

the Octocat-De-Los-Muertos by cameronmcefee

+++

Animated GIFs Work Too!

Image-Relative

the Daftpunktocat-Guy by jeejkang


Image Slides

[ Background ]

Press Down key for examples. | See GitPitch Wiki for details.

+++

Make A Bold Visual Statement


Use high-resolution background images for maximum impact.

+++?image=https://d1z75bzl1vljy2.cloudfront.net/kitchen-sink/victory.jpg

+++?image=https://d1z75bzl1vljy2.cloudfront.net/kitchen-sink/127.jpg


Video Slides

[ Inline ]

Press Down key for examples. | See GitPitch Wiki for details.

+++

Bring Your Presentations Alive


Embed YouTube, Vimeo, MP4 and WebM inline on any slide.

+++

YouTube Video

+++

Vimeo Video

+++

MP4 Video


Video Slides

[ Background ]

Press Down key for examples. | See GitPitch Wiki for details.

+++

Maximize The Viewer Experience


Go fullscreen with MP4 and WebM videos.

+++?video=http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4


Math Notation Slides

Press Down key for examples. | See GitPitch Wiki for details.

+++

Beautiful Math Rendered Beautifully


Use TeX, LaTeX and MathML markup powered by MathJax.

+++

$$\sum_{i=0}^n i^2 = \frac{(n^2+n)(2n+1)}{6}$$

+++

\begin{align} \dot{x} & = \sigma(y-x) \\ \dot{y} & = \rho x - y - xz \\ \dot{z} & = -\beta z + xy \end{align}

+++

The Cauchy-Schwarz Inequality

\[ \left( \sum_{k=1}^n a_k b_k \right)^{\!\!2} \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right) \]

+++

The probability of getting (k) heads when flipping (n) coins is:

\[P(E) = {n \choose k} p^k (1-p)^{ n-k} \]

+++

In-line Mathematics

This expression \(\sqrt{3x-1}+(1+x)^2\) is an example of an inline equation.


Slide Fragments

Press Down key for examples. | See GitPitch Wiki for details.

+++

Reveal Slide Concepts Piecemeal


Step through slide content in sequence to slowly reveal the bigger picture.

+++

  • Java
  • Groovy |
  • Kotlin |
  • Scala |
  • The JVM rocks! |

+++

Firstname Lastname Age
Jill Smith 25
Eve Jackson 94
John Doe 43

PITCHME.yaml Settings

Press Down key for examples. | See GitPitch Wiki for details.

+++

Stamp Your Own Look and Feel


Set a default theme, custom logo, custom css, background image, and preferred code syntax highlighting style.

+++

Customize Slideshow Behavior


Enable auto-slide with custom slide intervals, presentation looping, and RTL flow.


Slideshow Keyboard Controls

Press Down key for examples. | See GitPitch Wiki for details.

+++

Try Out These Great Features Now!


Mode On Key Off Key
Fullscreen F Esc
Overview O O
Blackout B B
Help ? Esc

GitPitch Social

Press Down key for examples. | See GitPitch Wiki for details.

+++

Slideshows Designed For Sharing


  • View any slideshow at its public URL
  • Promote any slideshow using a GitHub badge
  • Embed any slideshow within a blog or website
  • Share any slideshow on Twitter, LinkedIn, etc
  • Print any slideshow as a PDF document
  • Download and present any slideshow offline

GO FOR IT.

JUST ADD PITCHME.md ;)