[英]How to return a Reactive Flux that contains a Reactive Mono and Flux?
[英]How to push message to upstream using reactive Flux/Mono whenever they are ready than polling in interval for status?
嘗試在消息可用/准備好並在刷新后關閉連接時將消息推送到上游,而不是使用 spring 反應通量間隔輪詢消息。
@GetMapping(value = "/getValue/{randomId}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> statusCheck(@PathVariable("randomId") @NonNull String randomId) {
return Flux.<String>interval(Duration.ofSeconds(3))
.map(status -> {
if (getSomething(randomId).
equalsIgnoreCase("value"))
return "value";
return "ping";
}).take(Duration.ofSeconds(60)).timeout(Duration.ofSeconds(60));
}
Kafka 偵聽器在獲取時更新地圖中的 randomId 值,getSomething 方法檢查地圖中間隔的 randomId 值。 因此,我想在偵聽器接收到消息時將消息推送到客戶端,而不是檢查間隔並將數據存儲在地圖中。
這聽起來像是一個Flux.create()
請求:
return Flux.<String>create(emitter -> {
if (getSomething(randomId).equalsIgnoreCase("value")) {
sink.next("value");
}
else {
sink.next("ping");
}
});
/**
* Programmatically create a {@link Flux} with the capability of emitting multiple
* elements in a synchronous or asynchronous manner through the {@link FluxSink} API.
* This includes emitting elements from multiple threads.
* <p>
* <img class="marble" src="doc-files/marbles/createForFlux.svg" alt="">
* <p>
* This Flux factory is useful if one wants to adapt some other multi-valued async API
* and not worry about cancellation and backpressure (which is handled by buffering
* all signals if the downstream can't keep up).
* <p>
* For example:
*
* <pre><code>
* Flux.<String>create(emitter -> {
*
* ActionListener al = e -> {
* emitter.next(textField.getText());
* };
* // without cleanup support:
*
* button.addActionListener(al);
*
* // with cleanup support:
*
* button.addActionListener(al);
* emitter.onDispose(() -> {
* button.removeListener(al);
* });
* });
* </code></pre>
*
* @reactor.discard The {@link FluxSink} exposed by this operator buffers in case of
* overflow. The buffer is discarded when the main sequence is cancelled.
*
* @param <T> The type of values in the sequence
* @param emitter Consume the {@link FluxSink} provided per-subscriber by Reactor to generate signals.
* @return a {@link Flux}
* @see #push(Consumer)
*/
public static <T> Flux<T> create(Consumer<? super FluxSink<T>> emitter) {
我基於此 stackoverflow Spring 5 Web Reactive - Hot Publishing - How to use EmitterProcessor 將 MessageListener 橋接到事件流答案構建了解決方案,使用 EmitterProcessor 在消息可用時對其進行熱發布。
這是示例代碼
@GetMapping(value = "/getValue/{randomId}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> statusCheck(@PathVariable("randomId") @NonNull String randomId) {
EmitterProcessor<String> emitterProcessor = EmitterProcessor.create();
Flux<String> autoConnect = emitterProcessor.publish().autoConnect();
FluxSink<String> sink = emitterProcessor.sink();
//storing randomId and processor sink details
randomIdMap.putIfAbsent(randomId, emitterProcessor);
/** This will return ping status to notify client as
connection is alive until the randomId message received. **/
sendPingStatus(sink, randomId);
}
下面的方法顯示了如何在消息到達 kafka 消費者並關閉通量連接時將消息推送到客戶端。
@KafkaListener(topics = "some-subscription-id",
containerFactory = "kafkaListenerContainerFactory")
public void pushMessage(SomeMessage message, Acknowledgment acknowledgment) {
EmitterProcessor emitter = randomIdMap.get("randomId");
if (emitter != null ) {
emitter.onNext(message);
emitter.onComplete();
randomIdMap.remove("randomId");
acknowledgment.acknowledge();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.