英文:
Golang - RabbitMq : channel/connection is not open
问题
我是你的中文翻译助手,以下是你要翻译的内容:
我刚开始学习Go语言,我想重构我的代码,将RabbitMQ的初始化放在main函数之外的另一个函数中。所以我使用了一个结构体指针(包含了所有初始化的RabbitMQ信息),并将其传递给发送消息的函数。但是它告诉我:发布消息失败:异常(504)原因:"channel/connection is not open"。
结构体:
type RbmqConfig struct {
q amqp.Queue
ch *amqp.Channel
conn *amqp.Connection
rbmqErr error
}
初始化函数:
func initRabbitMq() *RbmqConfig {
config := &RbmqConfig{}
config.conn, config.rbmqErr = amqp.Dial("amqp://guest:guest@localhost:5672/")
failOnError(config.rbmqErr, "Failed to connect to RabbitMQ")
defer config.conn.Close()
config.ch, config.rbmqErr = config.conn.Channel()
failOnError(config.rbmqErr, "Failed to open a channel")
defer config.ch.Close()
config.q, config.rbmqErr = config.ch.QueueDeclare(
"<my_queue_name>",
true, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
failOnError(config.rbmqErr, "Failed to declare a queue")
return config
}
主函数:
config := initRabbitMq()
fmt.Println("queue name: ", config.q.Name)
sendMessage(config, <message_to_send>)
发送消息的函数:
func sendMessage(config *RbmqConfig, <message_to_send>) {
config.rbmqErr = config.ch.Publish(
"", // exchange
config.q.Name, // routing key
false, // mandatory
false,
amqp.Publishing{
DeliveryMode: amqp.Persistent,
ContentType: "text/plain",
Body: []byte(<message_to_send>),
})
failOnError(config.rbmqErr, "Failed to publish a message")
}
如果有人有任何想法,那将非常有帮助。提前谢谢你!
英文:
I'm new to golang, and I would like to refactorate my code so that the rabbitmq initialization is in another function that main. So I use a struct pointer (containing all the rabbitmq infos initilized) and pass it to the send function, but it tells me : Failed to publish a message: Exception (504) Reason: "channel/connection is not open"
struct :
type RbmqConfig struct {
q amqp.Queue
ch *amqp.Channel
conn *amqp.Connection
rbmqErr error
}
the init function :
func initRabbitMq() *RbmqConfig {
config := &RbmqConfig{}
config.conn, config.rbmqErr = amqp.Dial("amqp://guest:guest@localhost:5672/")
failOnError(config.rbmqErr, "Failed to connect to RabbitMQ")
defer config.conn.Close()
config.ch, config.rbmqErr = config.conn.Channel()
failOnError(config.rbmqErr, "Failed to open a channel")
defer config.ch.Close()
config.q, config.rbmqErr = config.ch.QueueDeclare(
"<my_queue_name>",
true, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
failOnError(config.rbmqErr, "Failed to declare a queue")
return config
}
main :
config := initRabbitMq()
fmt.Println("queue name : ", config.q.Name)
sendMessage(config, <message_to_send>)
in send message :
func sendMessage(config *RbmqConfig, <message_to_send>) {
config.rbmqErr = config.ch.Publish(
"", // exchange
config.q.Name, // routing key
false, // mandatory
false,
amqp.Publishing{
DeliveryMode: amqp.Persistent,
ContentType: "text/plain",
Body: []byte(<message_to_send>),
})
failOnError(config.rbmqErr, "Failed to publish a message")
If someone has any idea, that would be very helpful. Thank you in advance
答案1
得分: 15
在你的init
函数中,你写了defer config.conn.Close()
,这意味着当函数返回时会执行该语句。也就是说,无论何时init
函数完成,你的连接都会被关闭,这导致了未打开的连接。
你需要在main
函数或者你希望关闭连接的其他地方延迟关闭连接。
英文:
Inside your init
, you wrote defer config.conn.Close()
, which will be executed when the function return. That is to say, whenever init
finished, your connection will be closed, which causes unopen connection.
You need to defer the connection closing in main, or somewhere you want it to be closed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论