Skip to content

Instantly share code, notes, and snippets.

@iamgoangle
Created June 29, 2019 14:24
Show Gist options
  • Select an option

  • Save iamgoangle/761e2d32d3a6ec962d0c58d02ebdc004 to your computer and use it in GitHub Desktop.

Select an option

Save iamgoangle/761e2d32d3a6ec962d0c58d02ebdc004 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"time"
"github.com/iamgoangle/rabbit-go/internal/rabbitmq"
)
type mockData struct {
EventType string `json:"eventType"`
Data string `json:"data"`
}
// ROUTING_KEY=asia.thailand.bangkok go run producer.go
func main() {
conn, err := rabbitmq.NewConnection(rabbitmq.ConfigConnection{
Type: "standalone",
Url: "amqp://admin:1234@localhost:5672/",
})
if err != nil {
log.Fatalf("[main]: unable to connect RabbitMQ %+v", err)
}
rbMqConfig := rabbitmq.ConfigProducer{
Exchange: rabbitmq.ConfigExchange{
Type: rabbitmq.ExchangeTopic,
Name: "asia.exchange.topic",
Durable: true,
RoutingKey: os.Getenv("ROUTING_KEY"),
},
}
producer, err := rabbitmq.NewProducer(conn, rbMqConfig)
if err != nil {
log.Fatalf("[main]: unable to initial producer RabbitMQ %+v", err)
}
for i := 0; i < 1; i++ {
body, _ := json.Marshal(&mockData{
EventType: "asiaEvent",
Data: fmt.Sprintf("test topic %+v", i),
})
err = producer.Publish(body)
if err != nil {
log.Printf("unable to send message %+v", err)
}
time.Sleep(1 * time.Second)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment