Updated rabbitmq

master
Alex 2021-06-25 21:43:34 +02:00
parent 371197a5dd
commit ff9de3baa5
1 changed files with 27 additions and 2 deletions

View File

@ -9,6 +9,8 @@ import (
)
var Conn *amqp.Connection
var MailQueue amqp.Queue
var Channel *amqp.Channel
func getConnectionString() string {
cfg := &config.Cfg.RabbitMq
@ -31,12 +33,14 @@ func Init() {
log.Fatalln("Failed to open a channel", err)
}
defer ch.Close()
//defer ch.Close()
Channel = ch
log.Debug("RabbitMQ connected")
q, err := ch.QueueDeclare(
"hello", // name
"mails", // name
false, // durable
false, // delete when unused
false, // exclusive
@ -48,6 +52,8 @@ func Init() {
log.Fatalln("Failed to declare a queue", err)
}
MailQueue = q
body := "Hello World!"
err = ch.Publish(
"", // exchange
@ -63,3 +69,22 @@ func Init() {
log.Fatalln("Failed to publish a message", err)
}
}
func Publish(body string) {
log.Infoln("qu name", MailQueue.Name)
log.Infoln("publish", Channel, MailQueue)
err := Channel.Publish(
"", // exchange
MailQueue.Name, // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "application/json",
Body: []byte(body),
})
if err != nil {
log.Fatalln("Failed to publish a message", err)
}
}