Search in sources :

Example 1 with MessageHandler

use of io.eventuate.tram.messaging.consumer.MessageHandler 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 MessageHandler

use of io.eventuate.tram.messaging.consumer.MessageHandler in project eventuate-tram-core by eventuate-tram.

the class AbstractTramIntegrationTest method shouldDoSomething.

@Test
public void shouldDoSomething() throws InterruptedException {
    String destination = "Destination-" + System.currentTimeMillis();
    String subscriberId = "SubscriberId-" + System.currentTimeMillis();
    CountDownLatch latch = new CountDownLatch(1);
    MessageHandler handler = message -> {
        System.out.println("Got message=" + message);
        latch.countDown();
    };
    messageConsumer.subscribe(subscriberId, Collections.singleton(destination), handler);
    messageProducer.send(destination, MessageBuilder.withPayload("Hello").build());
    assertTrue("Expected message", latch.await(10, TimeUnit.SECONDS));
}
Also used : MessageProducer(io.eventuate.tram.messaging.producer.MessageProducer) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) MessageHandler(io.eventuate.tram.messaging.consumer.MessageHandler) Autowired(org.springframework.beans.factory.annotation.Autowired) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) MessageConsumer(io.eventuate.tram.messaging.consumer.MessageConsumer) Collections(java.util.Collections) MessageBuilder(io.eventuate.tram.messaging.producer.MessageBuilder) MessageHandler(io.eventuate.tram.messaging.consumer.MessageHandler) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

MessageConsumer (io.eventuate.tram.messaging.consumer.MessageConsumer)2 MessageHandler (io.eventuate.tram.messaging.consumer.MessageHandler)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 JSonMapper (io.eventuate.javaclient.commonimpl.JSonMapper)1 EventuateKafkaConsumer (io.eventuate.local.java.kafka.consumer.EventuateKafkaConsumer)1 Message (io.eventuate.tram.messaging.common.Message)1 MessageImpl (io.eventuate.tram.messaging.common.MessageImpl)1 MessageBuilder (io.eventuate.tram.messaging.producer.MessageBuilder)1 MessageProducer (io.eventuate.tram.messaging.producer.MessageProducer)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Set (java.util.Set)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1 BiConsumer (java.util.function.BiConsumer)1 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)1 Assert.assertTrue (org.junit.Assert.assertTrue)1