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();
}
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));
}
Aggregations