From 89b5b232a84db34063d3ea56ea44ab4fd2729cf0 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 16 Sep 2023 23:05:52 +0200 Subject: [PATCH] WIP: Mono durch Sink.unsafe().one() ersetzt -- TODO: Cold vs. Hot Sinks noch man neu recherchieren... --- .../implementation/kafka/InfoChannel.java | 99 ++++++++++--------- 1 file changed, 51 insertions(+), 48 deletions(-) diff --git a/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/InfoChannel.java b/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/InfoChannel.java index 4711dbd9..f08a8d85 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/InfoChannel.java +++ b/src/main/java/de/juplo/kafka/chat/backend/implementation/kafka/InfoChannel.java @@ -15,6 +15,7 @@ import org.apache.kafka.clients.producer.RecordMetadata; import org.apache.kafka.common.errors.WakeupException; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; +import reactor.core.publisher.Sinks; import java.net.URI; import java.time.*; @@ -115,64 +116,66 @@ public class InfoChannel implements Runnable { EventShardAssigned to = EventShardAssigned.of(shard, instanceUri); - return Mono.create(sink -> - { - ProducerRecord record = - new ProducerRecord<>( - topic, - Integer.toString(shard), - to); + Sinks.One sink = Sinks.unsafe().one(); - producer.send(record, ((metadata, exception) -> + ProducerRecord record = + new ProducerRecord<>( + topic, + Integer.toString(shard), + to); + + producer.send(record, ((metadata, exception) -> + { + if (metadata != null) { - if (metadata != null) - { - log.info("Successfully sent shard assigned event for shard: {}", shard); - sink.success(metadata); - } - else - { - // On send-failure - log.error( - "Could not send shard assigned event for shard {}: {}", - shard, - exception); - sink.error(exception); - } - })); - }); + log.info("Successfully sent shard assigned event for shard: {}", shard); + sink.emitValue(metadata, Sinks.EmitFailureHandler.FAIL_FAST); + } + else + { + // On send-failure + log.error( + "Could not send shard assigned event for shard {}: {}", + shard, + exception); + sink.emitError(exception, Sinks.EmitFailureHandler.FAIL_FAST); + } + })); + + return sink.asMono(); } Mono sendShardRevokedEvent(int shard) { EventShardRevoked to = EventShardRevoked.of(shard, instanceUri); - return Mono.create(sink -> - { - ProducerRecord record = - new ProducerRecord<>( - topic, - Integer.toString(shard), - to); + Sinks.One sink = Sinks.unsafe().one(); - producer.send(record, ((metadata, exception) -> + ProducerRecord record = + new ProducerRecord<>( + topic, + Integer.toString(shard), + to); + + producer.send(record, ((metadata, exception) -> + { + if (metadata != null) { - if (metadata != null) - { - log.info("Successfully sent shard revoked event for shard: {}", shard); - sink.success(metadata); - } - else - { - // On send-failure - log.error( - "Could not send shard revoked event for shard {}: {}", - shard, - exception); - sink.error(exception); - } - })); - }); + log.info("Successfully sent shard revoked event for shard: {}", shard); + sink.emitValue(metadata, Sinks.EmitFailureHandler.FAIL_FAST); + } + else + { + // On send-failure + log.error( + "Could not send shard revoked event for shard {}: {}", + shard, + exception); + sink.emitError(exception, Sinks.EmitFailureHandler.FAIL_FAST); + } + })); + + return sink.asMono(); } -- 2.20.1