Ejemplo n.º 1
0
func main() {
	if len(os.Args) < 2 {
		log.Fatal("You must provide a message as first arg!")
	}

	msgBody := string(os.Args[1])

	connection, err := amqp.Dial(sylvilagus.AMQP_URI)
	if err != nil {
		log.Fatal("Failed to connect!: ", err)
	}

	defer connection.Close()

	channel, err := sylvilagus.CreateHelloTopology(connection)
	if err != nil {
		log.Fatal("Failed to build topology!: ", err)
	}

	msg := amqp.Publishing{
		DeliveryMode: amqp.Persistent,
		Timestamp:    time.Now(),
		ContentType:  "text/plain",
		Body:         []byte(msgBody),
	}

	err = channel.Publish("hello-exchange", "hola", false, false, msg)
	if err != nil {
		log.Fatal("Failed to publish message!: ", err)
	} else {
		log.Printf("Published '%v'\n", msgBody)
	}
}
Ejemplo n.º 2
0
func main() {
	connection, err := amqp.Dial(sylvilagus.AMQP_URI)
	if err != nil {
		log.Fatal("Failed to connect!: ", err)
	}

	defer connection.Close()

	channel, err := sylvilagus.CreateHelloTopology(connection)
	if err != nil {
		log.Fatal("Failed to build topology!: ", err)
	}

	hellos, err := channel.Consume("hello-queue", "hello-consumer", false, false, false, false, nil)
	if err != nil {
		log.Fatal("Failed to start consuming!:", err)
	}

	quit := make(chan bool)

	go func(quit chan bool) {
		log.Println("Consuming...")
		for hello := range hellos {
			log.Printf("hello -> %v\n", string(hello.Body))
			hello.Ack(false)
			if string(hello.Body) == "quit" {
				quit <- true
				return
			}
		}
	}(quit)

	<-quit
}