|
- package main
-
- import (
- "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbitmq"
- "fmt"
- "log"
- )
-
- func main() {
- var host, port, user, pwd string
- log.SetFlags(log.Lshortfile | log.LstdFlags)
- log.Println("input host,port,user,pwd")
- fmt.Scanf("%s %s %s %s", &host, &port, &user, &pwd)
- if host == "" && port == "" && user == "" && pwd == "" {
- host = "119.23.182.117"
- port = "5672"
- user = "admin"
- pwd = "123456"
- }
- //ch, err := GetChannel
- rabbitmq.Init(host, port, user, pwd)
- //第一个参数指定rabbitmq服务器的链接,第二个参数指定创建队列的名字
- receiveMq := rabbitmq.New(rabbitmq.Cfg.Uri, "test_single_queue")
- //for {
- receiveMq.Qos(1)
- //接收消息时,指定
- messages := receiveMq.Consume()
-
- //forever := make(chan bool) //开辟管道
- //启用协程处理消息
- //go func() {
- var i = 0
- for msg := range messages {
- log.Printf("Received a message: %s", msg.Body)
-
- msg.Ack(false)
- i++
- if i >= 5 {
- receiveMq.CloseChannel()
- return
- }
- //msg.Reject(true)
- }
- //}()
- log.Printf("[*] Waiting for message,To exit press CTRL+C")
- //<-forever
- }
|