Skip to content

Commit

Permalink
correctly connect to RabbitMQ
Browse files Browse the repository at this point in the history
  • Loading branch information
PssbleTrngle committed Oct 18, 2024
1 parent 98cbde5 commit 1fad006
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.possible_triangle.atheneum_connector

import com.rabbitmq.client.ConnectionFactory
import net.minecraftforge.common.ForgeConfigSpec
import net.minecraftforge.fml.config.ModConfig
import thedarkcolour.kotlinforforge.forge.LOADING_CONTEXT
Expand Down Expand Up @@ -31,10 +32,14 @@ object Config {
private val rabbitmqUser = builder.define("user", "guest")
private val rabbitmqPass = builder.define("password", "guest")
private val rabbitmqHost = builder.define("host", "localhost")
private val rabbitmqPort = builder.define("port", "5672")
private val rabbitmqPort = builder.defineInRange("port", 5672, 0, Int.MAX_VALUE)

val rabbitMQUrl
get() = "amqp://${rabbitmqUser.get()}:${rabbitmqPass.get()}@${rabbitmqHost.get()}:${rabbitmqPort.get()}/"
fun configureRabbitMQ() = ConnectionFactory().apply {
username = rabbitmqUser.get()
password = rabbitmqPass.get()
host = rabbitmqHost.get()
port = rabbitmqPort.get()
}

init {
builder.pop()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ object RabbitMQ : RecoveryListener, ShutdownListener {
private lateinit var channel: Channel

fun connect() {
AtheneumConnector.LOG.info("Connecting to RabbitMQ")
val factory = Config.SERVER.configureRabbitMQ()

val connection = ConnectionFactory().newConnection(Config.SERVER.rabbitMQUrl)
AtheneumConnector.LOG.info("Connecting to RabbitMQ at amqp://${factory.host}:${factory.port}")

val connection = factory.newConnection()

connection.addShutdownListener(this)

Expand Down Expand Up @@ -64,7 +66,7 @@ object RabbitMQ : RecoveryListener, ShutdownListener {
}

override fun shutdownCompleted(exception: ShutdownSignalException) {
if(exception.isInitiatedByApplication) return
if (exception.isInitiatedByApplication) return
AtheneumConnector.LOG.warn("RabbitMQ has shutdown, reconnecting... ({})", exception.message)
}

Expand Down

0 comments on commit 1fad006

Please sign in to comment.