Search in sources :

Example 1 with StoredEvent

use of com.mt.common.domain.model.domain_event.StoredEvent in project mt-auth by publicdevop2019.

the class RabbitMqEventStreamService method subscribe.

@Override
public void subscribe(String subscribedApplicationName, boolean internal, @Nullable String fixedQueueName, Consumer<StoredEvent> consumer, String... topics) {
    String routingKeyWithoutTopic = subscribedApplicationName + "." + (internal ? "internal" : "external") + ".";
    String queueName;
    boolean autoDelete = false;
    if (fixedQueueName != null) {
        queueName = fixedQueueName;
    } else {
        // auto delete random generated queue
        autoDelete = true;
        long id = CommonDomainRegistry.getUniqueIdGeneratorService().id();
        String s;
        if (topics.length == 1) {
            s = MqHelper.handlerOf(appName, topics[0]);
        } else {
            s = MqHelper.handlerOf(appName, "combined_events");
        }
        queueName = Long.toString(id, 36) + "_" + s;
    }
    try {
        Channel channel = connectionSub.createChannel();
        channel.queueDeclare(queueName, true, false, autoDelete, null);
        // @todo find out proper prefetch value, this requires test in prod env
        channel.basicQos(10);
        checkExchange(channel);
        for (String topic : topics) {
            channel.queueBind(queueName, EXCHANGE_NAME, routingKeyWithoutTopic + topic);
        }
        channel.basicConsume(queueName, autoAck, (consumerTag, delivery) -> {
            log.trace("mq message received");
            String s = new String(delivery.getBody(), StandardCharsets.UTF_8);
            StoredEvent event = CommonDomainRegistry.getCustomObjectSerializer().deserialize(s, StoredEvent.class);
            log.debug("handling {} with id {}", ClassUtility.getShortName(event.getName()), event.getId());
            try {
                consumer.accept(event);
                long deliveryTag = delivery.getEnvelope().getDeliveryTag();
                channel.basicAck(deliveryTag, false);
            } catch (Exception ex) {
                log.error("error during consume, catch error to maintain connection", ex);
            }
            log.trace("mq message consumed");
        }, consumerTag -> {
        });
    } catch (IOException e) {
        log.error("unable create queue for {} with routing key {} and queue name {}", subscribedApplicationName, routingKeyWithoutTopic, queueName, e);
    }
}
Also used : Channel(com.rabbitmq.client.Channel) StoredEvent(com.mt.common.domain.model.domain_event.StoredEvent) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException)

Example 2 with StoredEvent

use of com.mt.common.domain.model.domain_event.StoredEvent in project mt-auth by publicdevop2019.

the class RabbitMQEventStreamService method subscribe.

@Override
public void subscribe(String subscribedApplicationName, boolean internal, @Nullable String fixedQueueName, Consumer<StoredEvent> consumer, String... topics) {
    String routingKeyWithoutTopic = subscribedApplicationName + "." + (internal ? "internal" : "external") + ".";
    String queueName;
    if (fixedQueueName != null) {
        queueName = fixedQueueName;
    } else {
        long id = CommonDomainRegistry.getUniqueIdGeneratorService().id();
        queueName = Long.toString(id, 36);
    }
    DeliverCallback deliverCallback = (consumerTag, delivery) -> {
        log.trace("mq message received");
        String s = new String(delivery.getBody(), StandardCharsets.UTF_8);
        StoredEvent event = CommonDomainRegistry.getCustomObjectSerializer().deserialize(s, StoredEvent.class);
        log.debug("handling {} with id {}", ClassUtility.getShortName(event.getName()), event.getId());
        try {
            consumer.accept(event);
        } catch (Exception ex) {
            log.error("error during consume, catch error to maintain connection", ex);
        }
        log.trace("mq message consumed");
    };
    try {
        Channel channel = connectionSub.createChannel();
        channel.queueDeclare(queueName, true, false, false, null);
        checkExchange(channel);
        for (String topic : topics) {
            channel.queueBind(queueName, EXCHANGE_NAME, routingKeyWithoutTopic + topic);
        }
        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
        });
    } catch (IOException e) {
        log.error("unable create queue for {} with routing key {} and queue name {}", subscribedApplicationName, routingKeyWithoutTopic, queueName, e);
    }
}
Also used : DeliverCallback(com.rabbitmq.client.DeliverCallback) SagaEventStreamService(com.mt.common.domain.model.domain_event.SagaEventStreamService) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) CommonDomainRegistry(com.mt.common.domain.CommonDomainRegistry) Resource(javax.annotation.Resource) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) Connection(com.rabbitmq.client.Connection) StandardCharsets(java.nio.charset.StandardCharsets) StoredEvent(com.mt.common.domain.model.domain_event.StoredEvent) Value(org.springframework.beans.factory.annotation.Value) Consumer(java.util.function.Consumer) Slf4j(lombok.extern.slf4j.Slf4j) Component(org.springframework.stereotype.Component) Environment(org.springframework.core.env.Environment) ClassUtility(com.mt.common.domain.model.clazz.ClassUtility) EXCHANGE_NAME(com.mt.common.CommonConstant.EXCHANGE_NAME) MQHelper(com.mt.common.domain.model.domain_event.MQHelper) Channel(com.rabbitmq.client.Channel) Nullable(javax.annotation.Nullable) Channel(com.rabbitmq.client.Channel) DeliverCallback(com.rabbitmq.client.DeliverCallback) StoredEvent(com.mt.common.domain.model.domain_event.StoredEvent) IOException(java.io.IOException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException)

Aggregations

StoredEvent (com.mt.common.domain.model.domain_event.StoredEvent)2 Channel (com.rabbitmq.client.Channel)2 IOException (java.io.IOException)2 TimeoutException (java.util.concurrent.TimeoutException)2 EXCHANGE_NAME (com.mt.common.CommonConstant.EXCHANGE_NAME)1 CommonDomainRegistry (com.mt.common.domain.CommonDomainRegistry)1 ClassUtility (com.mt.common.domain.model.clazz.ClassUtility)1 MQHelper (com.mt.common.domain.model.domain_event.MQHelper)1 SagaEventStreamService (com.mt.common.domain.model.domain_event.SagaEventStreamService)1 Connection (com.rabbitmq.client.Connection)1 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)1 DeliverCallback (com.rabbitmq.client.DeliverCallback)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Consumer (java.util.function.Consumer)1 Nullable (javax.annotation.Nullable)1 Resource (javax.annotation.Resource)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Value (org.springframework.beans.factory.annotation.Value)1 Environment (org.springframework.core.env.Environment)1 Component (org.springframework.stereotype.Component)1