Skip to content

Instantly share code, notes, and snippets.

@jbrisbin
Last active October 2, 2015 21:10
Show Gist options
  • Save jbrisbin/8b060119acc69f5e6dfd to your computer and use it in GitHub Desktop.
Save jbrisbin/8b060119acc69f5e6dfd to your computer and use it in GitHub Desktop.
Simple Broadcast Stream example using #ProjectReactor
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