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