All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.lemfi.kest.rabbitmq.executor.RabbitMQQueueCreationExecution.kt Maven / Gradle / Ivy

There is a newer version: 0.8.1
Show newest version
package com.github.lemfi.kest.rabbitmq.executor

import com.github.lemfi.kest.core.logger.LoggerFactory
import com.github.lemfi.kest.core.model.Execution
import com.github.lemfi.kest.rabbitmq.builder.QueueAndBinding
import com.rabbitmq.client.ConnectionFactory
import java.net.URLEncoder

internal class RabbitMQQueueCreationExecution(
    private val queueAndBinding: QueueAndBinding,
    private val connection: String,
    private val vhost: String,
) : Execution() {

    private val encodedVhost: String = URLEncoder.encode(vhost, Charsets.UTF_8)

    override fun execute() {

        LoggerFactory.getLogger("RABBITMQ-Kest")
            .info(
                """
                |Queue creation:
                |
                |vhost: $vhost
                |name: ${queueAndBinding.queue} 
                |
                |${
                    if (queueAndBinding.exchange != null) {
                        """bound to exchange: 
                |          exchange: ${queueAndBinding.exchange} 
                |          routing key: ${queueAndBinding.routingKey}""".trimMargin()
                    } else {
                        """bound to default ("") exchange"""
                    }
                }""".trimMargin()
            )

        ConnectionFactory().also {
            it.setUri("$connection/$encodedVhost")
        }
            .newConnection("kest connection")
            .createChannel()
            .apply {
                queueDeclare(queueAndBinding.queue, false, false, true, mutableMapOf())
                if (queueAndBinding.exchange != null && queueAndBinding.routingKey != null)
                    queueBind(queueAndBinding.queue, queueAndBinding.exchange, queueAndBinding.routingKey)
            }
            .connection.close()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy