This commit is contained in:
shoopea 2019-06-28 16:18:48 +08:00
parent e70e4677cd
commit 6b320ff0ee
2 changed files with 42 additions and 40 deletions

14
mq.go
View File

@ -7,5 +7,19 @@ import (
func (c *MQClient) Open() error { func (c *MQClient) Open() error {
conn, err := amqp.Dial("amqp://" + c.User + ":" + c.Password + "@" + c.Host + "/" + c.Path) conn, err := amqp.Dial("amqp://" + c.User + ":" + c.Password + "@" + c.Host + "/" + c.Path)
c.Connection = conn c.Connection = conn
if err != nil {
c.Connection.Close()
return err
}
ch, err := c.Connection.Channel()
c.Channel = ch
if err != nil {
c.Channel.Close()
c.Connection.Close()
return err
}
return err return err
} }

View File

@ -27,61 +27,49 @@ func MQGetMsgWorker(id int, msgs chan<- ChatWarsMessage) {
err := c.Open() err := c.Open()
logOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to connect to RabbitMQ")
if err != nil { if err != nil {
c.Connection.Close() q, err := c.Channel.QueueDeclare(
time.Sleep(15 * time.Second) "msg", // name
} else { false, // durable
ch, err := c.Connection.Channel() false, // delete when unused
c.Channel = ch false, // exclusive
logOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to open a channel") false, // no-wait
nil, // arguments
)
logOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to declare a queue")
if err != nil { if err != nil {
c.Channel.Close() c.Channel.Close()
c.Connection.Close()
time.Sleep(15 * time.Second) time.Sleep(15 * time.Second)
} else { } else {
q, err := c.Channel.QueueDeclare( m, err := ch.Consume(
"msg", // name q.Name, // queue
false, // durable "", // consumer
false, // delete when unused true, // auto-ack
false, // exclusive false, // exclusive
false, // no-wait false, // no-local
nil, // arguments false, // no-wait
nil, // args
) )
logOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to declare a queue") logOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to register a consumer")
if err != nil { if err != nil {
c.Channel.Close() c.Channel.Close()
c.Connection.Close() c.Connection.Close()
time.Sleep(15 * time.Second) time.Sleep(15 * time.Second)
} else { } else {
m, err := ch.Consume( for d := range m {
q.Name, // queue // log.Printf("MQGetMsgWorker["+strconv.Itoa(id)+"] : Received a message: %s", string(d.Body))
"", // consumer err = json.Unmarshal(d.Body, &x)
true, // auto-ack logOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Can't unmarshal.\n"+string(d.Body))
false, // exclusive if err == nil {
false, // no-local msgs <- x
false, // no-wait
nil, // args
)
logOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Failed to register a consumer")
if err != nil {
c.Channel.Close()
c.Connection.Close()
time.Sleep(15 * time.Second)
} else {
for d := range m {
// log.Printf("MQGetMsgWorker["+strconv.Itoa(id)+"] : Received a message: %s", string(d.Body))
err = json.Unmarshal(d.Body, &x)
logOnError(err, "MQGetMsgWorker["+strconv.Itoa(id)+"] : Can't unmarshal.\n"+string(d.Body))
if err == nil {
msgs <- x
}
} }
c.Channel.Close()
c.Connection.Close()
} }
c.Channel.Close()
c.Connection.Close()
} }
} }
} }
} }