rabbitmq 操作库
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

48 строки
1.1 KiB

  1. package main
  2. import (
  3. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbitmq"
  4. "fmt"
  5. "log"
  6. )
  7. func main() {
  8. var host, port, user, pwd string
  9. log.SetFlags(log.Lshortfile | log.LstdFlags)
  10. log.Println("input host,port,user,pwd")
  11. fmt.Scanf("%s %s %s %s", &host, &port, &user, &pwd)
  12. if host == "" && port == "" && user == "" && pwd == "" {
  13. host = "119.23.182.117"
  14. port = "5672"
  15. user = "admin"
  16. pwd = "123456"
  17. }
  18. //ch, err := GetChannel
  19. rabbitmq.Init(host, port, user, pwd)
  20. //第一个参数指定rabbitmq服务器的链接,第二个参数指定创建队列的名字
  21. receiveMq := rabbitmq.New(rabbitmq.Cfg.Uri, "test_single_queue")
  22. //for {
  23. receiveMq.Qos(1)
  24. //接收消息时,指定
  25. messages := receiveMq.Consume()
  26. //forever := make(chan bool) //开辟管道
  27. //启用协程处理消息
  28. //go func() {
  29. var i = 0
  30. for msg := range messages {
  31. log.Printf("Received a message: %s", msg.Body)
  32. msg.Ack(false)
  33. i++
  34. if i >= 5 {
  35. receiveMq.CloseChannel()
  36. return
  37. }
  38. //msg.Reject(true)
  39. }
  40. //}()
  41. log.Printf("[*] Waiting for message,To exit press CTRL+C")
  42. //<-forever
  43. }