Skip to content

Instantly share code, notes, and snippets.

@matteo-grella
Created May 13, 2019 13:02
Show Gist options
  • Save matteo-grella/a271dbe68f5e19960c10bd45b235de4a to your computer and use it in GitHub Desktop.
Save matteo-grella/a271dbe68f5e19960c10bd45b235de4a to your computer and use it in GitHub Desktop.
Create a RabbitMQ Connection
import com.rabbitmq.client.Connection
import com.rabbitmq.client.ConnectionFactory
/**
* Create a RabbitMQ Connection.
*
* @param host the host
* @param port the port
* @param username the username (can be null)
* @param password the username (can be null)
*
* return a new channel
*/
fun buildConnection(host: String, port: Int, username: String?, password: String?): Connection {
val factory = ConnectionFactory()
factory.host = host
factory.port = port
if (username != null && password != null) {
factory.username = username
factory.password = password
}
factory.isAutomaticRecoveryEnabled = true // isn't automatic recovery enabled by default?
factory.requestedHeartbeat = 1800 // http://www.rabbitmq.com/heartbeats.html
return factory.newConnection()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment