Search in sources :

Example 1 with Message

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

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));
}
Also used : PathVariables(io.eventuate.tram.commands.consumer.PathVariables) Message(io.eventuate.tram.messaging.common.Message) CommandMessage(io.eventuate.tram.commands.consumer.CommandMessage) CommandMessage(io.eventuate.tram.commands.consumer.CommandMessage) Test(org.junit.Test)

Example 3 with Message

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);
}
Also used : MessageConsumer(io.eventuate.tram.messaging.consumer.MessageConsumer) Message(io.eventuate.tram.messaging.common.Message) Command(io.eventuate.tram.commands.common.Command) ChannelMapping(io.eventuate.tram.commands.common.ChannelMapping) MessageProducer(io.eventuate.tram.messaging.producer.MessageProducer) Test(org.junit.Test)

Example 4 with Message

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();
}
Also used : Message(io.eventuate.tram.messaging.common.Message)

Aggregations

Message (io.eventuate.tram.messaging.common.Message)4 MessageConsumer (io.eventuate.tram.messaging.consumer.MessageConsumer)2 Test (org.junit.Test)2 JSonMapper (io.eventuate.javaclient.commonimpl.JSonMapper)1 EventuateKafkaConsumer (io.eventuate.local.java.kafka.consumer.EventuateKafkaConsumer)1 ChannelMapping (io.eventuate.tram.commands.common.ChannelMapping)1 Command (io.eventuate.tram.commands.common.Command)1 CommandMessage (io.eventuate.tram.commands.consumer.CommandMessage)1 PathVariables (io.eventuate.tram.commands.consumer.PathVariables)1 MessageImpl (io.eventuate.tram.messaging.common.MessageImpl)1 MessageHandler (io.eventuate.tram.messaging.consumer.MessageHandler)1 MessageProducer (io.eventuate.tram.messaging.producer.MessageProducer)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