use of io.eventuate.local.java.kafka.consumer.EventuateKafkaConsumer in project eventuate-tram-core by eventuate-tram.
the class MessageConsumerKafkaImpl method subscribe.
@Override
public void subscribe(String subscriberId, Set<String> channels, MessageHandler handler) {
BiConsumer<ConsumerRecord<String, String>, BiConsumer<Void, Throwable>> kcHandler = (record, callback) -> {
Message m = toMessage(record);
// TODO If we do that here then remove TT from higher-levels
transactionTemplate.execute(ts -> {
if (duplicateMessageDetector.isDuplicate(subscriberId, m.getId())) {
logger.trace("Duplicate message {} {}", subscriberId, m.getId());
callback.accept(null, null);
return null;
}
try {
logger.trace("Invoking handler {} {}", subscriberId, m.getId());
handler.accept(m);
} catch (Throwable t) {
logger.trace("Got exception {} {}", subscriberId, m.getId());
logger.trace("Got exception ", t);
callback.accept(null, t);
return null;
}
logger.trace("handled message {} {}", subscriberId, m.getId());
callback.accept(null, null);
return null;
});
};
EventuateKafkaConsumer kc = new EventuateKafkaConsumer(subscriberId, kcHandler, new ArrayList<>(channels), bootstrapServers);
consumers.add(kc);
kc.start();
}
use of io.eventuate.local.java.kafka.consumer.EventuateKafkaConsumer in project eventuate-local by eventuate-local.
the class EventuateKafkaAggregateSubscriptions method subscribe.
@Override
public CompletableFuture<?> subscribe(String subscriberId, Map<String, Set<String>> aggregatesAndEvents, SubscriberOptions subscriberOptions, Function<SerializedEvent, CompletableFuture<?>> handler) {
List<String> topics = aggregatesAndEvents.keySet().stream().map(AggregateTopicMapping::aggregateTypeToTopic).collect(toList());
EventuateKafkaConsumer consumer = new EventuateKafkaConsumer(subscriberId, (record, callback) -> {
SerializedEvent se = toSerializedEvent(record);
if (aggregatesAndEvents.get(se.getEntityType()).contains(se.getEventType())) {
handler.apply(se).whenComplete((result, t) -> {
callback.accept(null, t);
});
} else {
callback.accept(null, null);
}
}, topics, eventuateLocalAggregateStoreConfiguration.getBootstrapServers());
addConsumer(consumer);
consumer.start();
return CompletableFuture.completedFuture(null);
}
Aggregations