Skip to content

Instantly share code, notes, and snippets.

@razorcd
Created August 20, 2018 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save razorcd/51593c0e6efcc36af3d2755a32ab05cf to your computer and use it in GitHub Desktop.
Save razorcd/51593c0e6efcc36af3d2755a32ab05cf to your computer and use it in GitHub Desktop.
Flux: custom emitter simulation
/**
* Custom emitter.
*/
@GetMapping(value = "/custom-emitter", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> getCustomEmitter() {
EmitterProcessor<String> hotSource = EmitterProcessor.create();
Flux<String> hotFlux = hotSource.publish().autoConnect();
// hotFlux.subscribe(d -> System.out.println("Subscriber 1 to Hot Source received: "+d));
// hotFlux.subscribe(d -> System.out.println("Subscriber 2 to Hot Source received: "+d));
// simulating different data producer:
new Thread(() -> {
int i = 0;
do {
System.out.print(".");
hotSource.onNext("A"+String.valueOf(i++)); // it can add any data to the flux from anywhere
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
} while (true);
}).start();
return hotFlux;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment