Skip to content

Instantly share code, notes, and snippets.

@tonvanbart
Created November 11, 2016 10:27
Show Gist options
  • Save tonvanbart/ab8deb99ec8660ba4d184a2f8890170a to your computer and use it in GitHub Desktop.
Save tonvanbart/ab8deb99ec8660ba4d184a2f8890170a to your computer and use it in GitHub Desktop.
Example using projectreactor.io : create a Flux to enable clients to subscribe to your method call.
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;
/**
* Created by ton on 10/11/16.
*/
public class Example {
private List<FluxSink<String>> handlers = new ArrayList<>();
public Flux<String> getMessagesAsStream() {
Flux<String> result = Flux.create(sink -> {
handlers.add(sink);
sink.setCancellation(() -> handlers.remove(sink));
});
return result;
}
public void handleMessage(String message) {
handlers.forEach(han -> han.next(message));
}
public static void main(String[] args) {
Example example = new Example();
example.getMessagesAsStream().subscribe(req -> System.out.println("req = " + req));
example.getMessagesAsStream().subscribe(msg -> System.out.println(msg.toUpperCase()));
example.handleMessage("een");
example.handleMessage("twee");
example.handleMessage("drie");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment