Last active
October 2, 2015 21:10
-
-
Save jbrisbin/8b060119acc69f5e6dfd to your computer and use it in GitHub Desktop.
Simple Broadcast Stream example using #ProjectReactor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static { | |
// Only done once, statically, and shared across this classloader | |
Environment.initialize(); | |
} | |
// Create a Stream subclass we can sink values into | |
Broadcaster<String> b = Broadcaster.create(); | |
b | |
// dispatch onto a Thread other than 'main' | |
.dispatchOn(Environment.cachedDispatcher()) | |
// transform input to UC | |
.map(String::toUpperCase) | |
// only let certain values pass through | |
.filter(s -> s.startsWith("HELLO")) | |
// produce demand | |
.consume(s -> System.out.println(Thread.currentThread() + ": " + s)); | |
// Sink values into this Broadcaster | |
b.onNext("Hello World!"); | |
// This won't print | |
b.onNext("Goodbye World!"); | |
// Must wait for tasks in other threads to complete | |
Thread.sleep(500); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment