initial commit
This commit is contained in:
53
dev/rabbitmq/sendMessage.go
Executable file
53
dev/rabbitmq/sendMessage.go
Executable file
@@ -0,0 +1,53 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
// "github.com/streadway/amqp"
|
||||
"github.com/rabbitmq/amqp091-go"
|
||||
)
|
||||
|
||||
// Here we set the way error messages are displayed in the terminal.
|
||||
func failOnError(err error, msg string) {
|
||||
if err != nil {
|
||||
log.Fatalf("%s: %s", msg, err)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Here we connect to RabbitMQ or send a message if there are any errors connecting.
|
||||
conn, err := amqp091.Dial("amqp://guest:guest@localhost:32189/")
|
||||
failOnError(err, "Failed to connect to RabbitMQ")
|
||||
defer conn.Close()
|
||||
|
||||
ch, err := conn.Channel()
|
||||
failOnError(err, "Failed to open a channel")
|
||||
defer ch.Close()
|
||||
|
||||
// We create a Queue to send the message to.
|
||||
q, err := ch.QueueDeclare(
|
||||
"ALL-DCS-queue", // name
|
||||
true, // durable
|
||||
false, // delete when unused
|
||||
false, // exclusive
|
||||
false, // no-wait
|
||||
nil, // arguments
|
||||
)
|
||||
failOnError(err, "Failed to declare a queue")
|
||||
|
||||
// We set the payload for the message.
|
||||
body := "Hallo Diederick, let's go hack!"
|
||||
err = ch.Publish(
|
||||
"", // exchange
|
||||
q.Name, // routing key
|
||||
false, // mandatory
|
||||
false, // immediate
|
||||
amqp091.Publishing{
|
||||
DeliveryMode: amqp091.Persistent,
|
||||
ContentType: "text/plain",
|
||||
Body: []byte(body),
|
||||
})
|
||||
// If there is an error publishing the message, a log will be displayed in the terminal.
|
||||
failOnError(err, "Failed to publish a message")
|
||||
log.Printf(" [x] Congrats, sending message: %s", body)
|
||||
}
|
||||
Reference in New Issue
Block a user