2012-11-15 02:16:00 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/streadway/amqp"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
connection, err := amqp.Dial("amqp://guest:guest@localhost:5672")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to connect!: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer connection.Close()
|
|
|
|
|
|
|
|
channel, err := connection.Channel()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to get channel!: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = channel.ExchangeDeclare("hello-exchange", "direct", true, false, false, false, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to declare exchange!: ", err)
|
|
|
|
}
|
|
|
|
|
2012-11-15 03:27:46 +00:00
|
|
|
_, err = channel.QueueDeclare("hello-queue", true, false, false, false, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to declare queue!: ", err)
|
|
|
|
}
|
|
|
|
|
2012-11-15 02:16:00 +00:00
|
|
|
err = channel.QueueBind("hello-queue", "hola", "hello-exchange", false, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Failed to bind to queue!: ", err)
|
|
|
|
}
|
2012-11-15 03:27:46 +00:00
|
|
|
|
|
|
|
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() {
|
|
|
|
for hello := range hellos {
|
|
|
|
log.Println("hello ->", hello)
|
|
|
|
hello.Ack(false)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-quit
|
2012-11-15 02:16:00 +00:00
|
|
|
}
|