Created
August 20, 2018 16:10
-
-
Save razorcd/51593c0e6efcc36af3d2755a32ab05cf to your computer and use it in GitHub Desktop.
Flux: custom emitter simulation
This file contains hidden or 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
/** | |
* 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