Search in sources :

Example 1 with EXCHANGE_NAME

use of com.mt.common.CommonConstant.EXCHANGE_NAME 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

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 StoredEvent (com.mt.common.domain.model.domain_event.StoredEvent)1 Channel (com.rabbitmq.client.Channel)1 Connection (com.rabbitmq.client.Connection)1 ConnectionFactory (com.rabbitmq.client.ConnectionFactory)1 DeliverCallback (com.rabbitmq.client.DeliverCallback)1 IOException (java.io.IOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 TimeoutException (java.util.concurrent.TimeoutException)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