Skip to content

Instantly share code, notes, and snippets.

@sudipidus
Created January 6, 2022 08:02
Show Gist options
  • Save sudipidus/6b1e235c02c42957d6f3ad94e67ea4cf to your computer and use it in GitHub Desktop.
Save sudipidus/6b1e235c02c42957d6f3ad94e67ea4cf to your computer and use it in GitHub Desktop.
go script to send events to rabbit mq
package main
import (
"fmt"
"log"
amqp "github.com/rabbitmq/amqp091-go"
)
func failOnError(err error, msg string) {
if err != nil {
log.Panicf("%s: %s", msg, err)
}
}
func main() {
conn, err := amqp.Dial("amqp://*********")
failOnError(err, "Failed to connect to RabbitMQ")
defer conn.Close()
ch, err := conn.Channel()
failOnError(err, "Failed to open a channel")
defer ch.Close()
failOnError(err, "Failed to declare a queue")
body := `{
'key':'value'
}`
for i := 0; i < 100; i++ {
publishToAMQP(ch, body)
fmt.Printf("publishing to amqp %d", (i + 1))
}
failOnError(err, "Failed to publish a message")
}
func publishToAMQP(ch *amqp.Channel, body string) {
ch.Publish(
"notifications_topic_staging", // exchange
"promotion.#", // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
ContentType: "text/plain",
Body: []byte(body),
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment