use of io.eventuate.tram.messaging.common.Message 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.common.Message in project eventuate-tram-core by eventuate-tram.
the class AbstractTramCommandsAndEventsIntegrationTest method shouldDoSomething.
@Test
public void shouldDoSomething() throws InterruptedException {
String messageId = commandProducer.send("customerService", "/customers/10", new MyTestCommand(), myReplyConsumer.getReplyChannel(), Collections.emptyMap());
Message m = myReplyConsumer.messages.poll(30, TimeUnit.SECONDS);
assertNotNull(m);
assertEquals(messageId, m.getRequiredHeader(ReplyMessageHeaders.IN_REPLY_TO));
System.out.println("Received m=" + m);
verify(myTestCommandHandler).myHandlerMethod(any(CommandMessage.class), any(PathVariables.class));
}
use of io.eventuate.tram.messaging.common.Message in project eventuate-tram-core by eventuate-tram.
the class CommandDispatcherTest method shouldDispatchCommand.
@Test
public void shouldDispatchCommand() {
String commandDispatcherId = "fooId";
CommandDispatcherTestTarget target = spy(new CommandDispatcherTestTarget());
ChannelMapping channelMapping = mock(ChannelMapping.class);
MessageConsumer messageConsumer = mock(MessageConsumer.class);
MessageProducer messageProducer = mock(MessageProducer.class);
CommandDispatcher dispatcher = new CommandDispatcher(commandDispatcherId, defineCommandHandlers(target), channelMapping, messageConsumer, messageProducer);
String customerId = "customer0";
String resource = "/customers/" + customerId;
Command command = new TestCommand();
String replyTo = "replyTo-xxx";
String channel = "myChannel";
Message message = CommandProducerImpl.makeMessage(channel, resource, command, replyTo, singletonMap(Message.ID, "999"));
dispatcher.messageHandler(message);
verify(target).reserveCredit(any(CommandMessage.class), any(PathVariables.class));
verify(messageProducer).send(any(), any());
verifyNoMoreInteractions(messageProducer, target);
}
use of io.eventuate.tram.messaging.common.Message in project eventuate-tram-core by eventuate-tram.
the class CommandProducerImpl method send.
@Override
public String send(String channel, String resource, Command command, String replyTo, Map<String, String> headers) {
Message message = makeMessage(channel, resource, command, replyTo, headers);
messageProducer.send(channelMapping.transform(channel), message);
return message.getId();
}
Aggregations