50 lines
994 B
Go
50 lines
994 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"log"
|
||
|
|
||
|
"github.com/streadway/amqp"
|
||
|
)
|
||
|
|
||
|
func MQMainReceive() {
|
||
|
conn, err := amqp.Dial("amqp://shoopea:UmDd5g4WRa2MzqOHsG2T@localhost:5672/chatwars")
|
||
|
failOnError(err, "MQMainReceive : Failed to connect to RabbitMQ")
|
||
|
defer conn.Close()
|
||
|
|
||
|
ch, err := conn.Channel()
|
||
|
failOnError(err, "MQMainReceive : Failed to open a channel")
|
||
|
defer ch.Close()
|
||
|
|
||
|
q, err := ch.QueueDeclare(
|
||
|
"msg", // name
|
||
|
false, // durable
|
||
|
false, // delete when unused
|
||
|
false, // exclusive
|
||
|
false, // no-wait
|
||
|
nil, // arguments
|
||
|
)
|
||
|
failOnError(err, "MQMainReceive : Failed to declare a queue")
|
||
|
|
||
|
msgs, err := ch.Consume(
|
||
|
q.Name, // queue
|
||
|
"", // consumer
|
||
|
true, // auto-ack
|
||
|
false, // exclusive
|
||
|
false, // no-local
|
||
|
false, // no-wait
|
||
|
nil, // args
|
||
|
)
|
||
|
failOnError(err, "MQMainReceive : Failed to register a consumer")
|
||
|
|
||
|
forever := make(chan bool)
|
||
|
|
||
|
go func() {
|
||
|
for d := range msgs {
|
||
|
log.Printf("MQMainReceive : Received a message: %s", d.Body)
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
<-forever
|
||
|
|
||
|
}
|