Open dartartem opened 3 years ago
Here is a suggestion:
BiConsumer<Void, Throwable> callback
as an argument
(ignored, null)
on success or (null, throwable)
on failureA Java CompletableFuture is something that contains a value or a Throwable.
Therefore:
BiConsumer<Void, Throwable> callback
as a parameter. These functions should return a CompletableFuture.reactive
in a standard Java way@cer yes, I planned to propose something similar, I just accidentally created partially filed issue. I updated it.
Also, I think it's important to define the goal here:
public interface ReactiveMessageConsumer {
MessageSubscription subscribe(String subscriberId, Set<String> channels, ReactiveMessageHandler handler);
String getId();
void close();
}
public interface ReactiveMessageHandler extends Function<Message, Mono<Void> {
}
Reactive event handling:
public DomainEventDispatcher(String eventDispatcherId, ReactiveDomainEventHandlers domainEventHandlers, MessageConsumer messageConsumer, DomainEventNameMapping domainEventNameMapping) {
public ReactiveDomainEventHandlers domainEventHandlers() {
return ReactiveDomainEventHandlersBuilder
.forAggregateType("io.eventuate.examples.tram.ordersandcustomers.customers.domain.Customer")
.onEvent(CustomerCreditReservedEvent.class, this::handleCustomerCreditReservedEvent)
...
.build();
}
private Mono<Void> handleCustomerCreditReservedEvent(DomainEventEnvelope<CustomerCreditReservedEvent> domainEventEnvelope) {
...
}
Goals & Background:
The goal of reactive processing is handle data when it is ready, without waiting for readiness. In other words do not block processing (thread) for waiting IO operations.
There are 2 ways to make eventuate tram consumer reactive.
1) Replace kafka consumer by reactive version. https://projectreactor.io/docs/kafka/release/reference/
Advantages (according to docs):
Disadvantages:
Thoughts:
We already have back-pressure handling and functional style is not important for internal details of eventuate kafka consumer (eventuate-messaging-kafka project)
2) Adapt existing API for reactive processing. What do we have now:
MessageConsumerKafkaImpl uses simple java consumer for event handling:
public KafkaSubscription subscribe(String subscriberId, Set<String> channels, KafkaMessageHandler handler) {
link
link
Internally is wrapped up by EventuateKafkaConsumerMessageHandler
handler has callbacks to notify message processor that message handled:
It is necessary to commit offsets of processed events
by consumer
It is necessary because consumer uses polling loop that does not wait finishing of record handling.
So, to integrate reactive tram consumer is necessary only to modify KafkaMessageHandler by something that can be processed in background with notification that processing is finished (to commit offsets).
It is relatively simple and ensures reactive processing.