我正在尝试使用从com.rabbitmq.client.*
导入的ConnectionFactory连接到RabbitMQ,我有一个运行在docker容器中的RabbitMQ服务器。
我使用此命令docker run -d --hostname my-rabbit --name myRabbit -p 8080:15672 -p 5672:5672 rabbitmq:3-management
启动了服务器。我能够很好地查看RabbitMQ管理页面。用户和密码均为默认的guest
。
这是我连接到Rabbit的代码:
private fun connectToRabbitMQIfNeeded() {
if (!isConnected(false)) {
val factory = ConnectionFactory()
factory.host = "localhost"
Log.d(TAG, "username: ${factory.username}, password: ${factory.password}, host: ${factory.host}")
try {
val connection = factory.newConnection()
mChannel = connection.createChannel()
} catch(ex: IOException) {
Log.d(TAG, "Error connecting to rabbit: ${ex.message}")
ex.printStackTrace()
}
}
}
这是我的日志:
11-14 19:45:10.235 20254-20325/com.ds.base D/RabbitMQConnector: username: guest, password: guest, host: localhost
11-14 19:45:10.238 20254-20325/com.ds.base D/RabbitMQConnector: Error connecting to rabbit: failed to connect to /127.0.0.1 (port 5672) after 60000ms: isConnected failed: ECONNREFUSED (Connection refused)