Created
January 6, 2022 08:02
-
-
Save sudipidus/6b1e235c02c42957d6f3ad94e67ea4cf to your computer and use it in GitHub Desktop.
go script to send events to rabbit mq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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