Search in sources :

Example 1 with EventuateKafkaConsumer

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();
}
Also used : Logger(org.slf4j.Logger) MessageImpl(io.eventuate.tram.messaging.common.MessageImpl) LoggerFactory(org.slf4j.LoggerFactory) EventuateKafkaConsumer(io.eventuate.local.java.kafka.consumer.EventuateKafkaConsumer) MessageHandler(io.eventuate.tram.messaging.consumer.MessageHandler) Autowired(org.springframework.beans.factory.annotation.Autowired) Set(java.util.Set) JSonMapper(io.eventuate.javaclient.commonimpl.JSonMapper) Message(io.eventuate.tram.messaging.common.Message) ArrayList(java.util.ArrayList) List(java.util.List) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) BiConsumer(java.util.function.BiConsumer) MessageConsumer(io.eventuate.tram.messaging.consumer.MessageConsumer) Message(io.eventuate.tram.messaging.common.Message) EventuateKafkaConsumer(io.eventuate.local.java.kafka.consumer.EventuateKafkaConsumer) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) BiConsumer(java.util.function.BiConsumer)

Example 2 with EventuateKafkaConsumer

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);
}
Also used : EventuateKafkaConsumer(io.eventuate.local.java.kafka.consumer.EventuateKafkaConsumer) SerializedEvent(io.eventuate.javaclient.commonimpl.SerializedEvent)

Aggregations

EventuateKafkaConsumer (io.eventuate.local.java.kafka.consumer.EventuateKafkaConsumer)2 JSonMapper (io.eventuate.javaclient.commonimpl.JSonMapper)1 SerializedEvent (io.eventuate.javaclient.commonimpl.SerializedEvent)1 Message (io.eventuate.tram.messaging.common.Message)1 MessageImpl (io.eventuate.tram.messaging.common.MessageImpl)1 MessageConsumer (io.eventuate.tram.messaging.consumer.MessageConsumer)1 MessageHandler (io.eventuate.tram.messaging.consumer.MessageHandler)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Set (java.util.Set)1 BiConsumer (java.util.function.BiConsumer)1 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)1