Cold Publisher
Creating publisher which emits update every second:
Above flux will only start emitting elements post subscription only, and for multiple subscriptions it will replay the elements from start.
Hot Publisher
Creating published which doesn't wait for subscribers to subscribe, instead keeps on emitting elements and subscribers will be able catch elements which are emitted post subscription as below:
Creating publisher which emits update every second:
Flux.interval(Duration.ofSeconds(1L))
.map(element->{
return new Update(element.toString(),UUID.randomUUID().toString());
}).doOnNext(onNext->{
print("Publishing new element with key "+onNext.getKey());
});
Note: complete code is availeble at following git repository git-repoAbove flux will only start emitting elements post subscription only, and for multiple subscriptions it will replay the elements from start.
Hot Publisher
Creating published which doesn't wait for subscribers to subscribe, instead keeps on emitting elements and subscribers will be able catch elements which are emitted post subscription as below:
hotUpdate=DirectProcessor.create();
new Thread(() -> {
int state=0;
do {
print("generator with state "+state);
hotUpdate.onNext(new Update(Integer.toString(state),UUID.randomUUID().toString()));
hotUpdate.delaySequence(Duration.ofSeconds(3));
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
/*
* if (state == 1000) { hotUpdate.onComplete(); break; }
*/ hotUpdate.doOnCancel(()->{
print("doOnCancel event on hot Source");
hotUpdate.onComplete();
});
state++;
print("Is Terminated "+hotUpdate.isTerminated());
if(hotUpdate.hasCompleted()) {
break;
}
}while(!hotUpdate.isTerminated());
}).start();
Comments
Post a Comment