rabbitmq 操作库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fanout_exchange_test.go 3.1 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package test
  2. import (
  3. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbitmq"
  4. "fmt"
  5. "log"
  6. "strconv"
  7. "strings"
  8. "testing"
  9. "time"
  10. )
  11. const FanoutExchangeName = "test_fanout_exchange"
  12. func TestFanoutExchangeSend(t *testing.T) {
  13. var host, port, user, pwd string
  14. log.SetFlags(log.Lshortfile | log.LstdFlags)
  15. log.Println("input host,port,user,pwd")
  16. fmt.Scanf("%s %s %s %s", &host, &port, &user, &pwd)
  17. if host == "" && port == "" && user == "" && pwd == "" {
  18. host = "119.23.182.117"
  19. port = "5672"
  20. user = "admin"
  21. pwd = "123456"
  22. }
  23. rabbitmq.Init(host, port, user, pwd)
  24. ch := rabbitmq.Connect(rabbitmq.Cfg.Uri)
  25. rabbitmq.NewExchange(rabbitmq.Cfg.Uri, FanoutExchangeName, "fanout")
  26. i := 0
  27. for {
  28. time.Sleep(1)
  29. greetings := []string{"Helloworld!", strconv.Itoa(i)}
  30. ch.Publish(FanoutExchangeName, strings.Join(greetings, " "), "")
  31. i = i + 1
  32. }
  33. }
  34. func TestFanoutExchangeReceive1(t *testing.T) {
  35. // 1.接收者,首先创建自己队列
  36. // 2.创建交换机
  37. // 3.将自己绑定到交换机上
  38. // 4.接收交换机上发过来的消息
  39. var host, port, user, pwd string
  40. log.SetFlags(log.Lshortfile | log.LstdFlags)
  41. log.Println("input host,port,user,pwd")
  42. fmt.Scanf("%s %s %s %s", &host, &port, &user, &pwd)
  43. if host == "" && port == "" && user == "" && pwd == "" {
  44. host = "119.23.182.117"
  45. port = "5672"
  46. user = "admin"
  47. pwd = "123456"
  48. }
  49. rabbitmq.Init(host, port, user, pwd)
  50. //第一个参数指定rabbitmq服务器的链接,第二个参数指定创建队列的名字
  51. receiveMq := rabbitmq.New(rabbitmq.Cfg.Uri, "test_fanout_exchange_receive_queue_1")
  52. //第一个参数:rabbitmq服务器的链接,第二个参数:交换机名字,第三个参数:交换机类型
  53. rabbitmq.NewExchange(rabbitmq.Cfg.Uri, FanoutExchangeName, "fanout")
  54. // 队列绑定到exchange
  55. receiveMq.Bind(FanoutExchangeName, "")
  56. //4
  57. forever := make(chan bool) //开辟管道
  58. var i = 0
  59. //for {
  60. //接收消息时,指定
  61. messages := receiveMq.Consume()
  62. go func() {
  63. for msg := range messages {
  64. log.Printf("recevie1 Received a message: %s", msg.Body)
  65. i++
  66. if i >= 5 {
  67. msg.Reject(true)
  68. receiveMq.CloseChannel()
  69. return
  70. } else {
  71. msg.Ack(false)
  72. }
  73. }
  74. }()
  75. log.Printf("[*] Waiting for message,To exit press CTRL+C")
  76. <-forever
  77. //}
  78. }
  79. func TestFanoutExchangeReceive2(t *testing.T) {
  80. // 1.接收者,首先创建自己队列
  81. // 2.创建交换机
  82. // 3.将自己绑定到交换机上
  83. // 4.接收交换机上发过来的消息
  84. //第一个参数指定rabbitmq服务器的链接,第二个参数指定创建队列的名字
  85. receiveMq := rabbitmq.New("amqp://user:password@ip:port/", "test_fanout_exchange_receive_queue_2")
  86. //第一个参数:rabbitmq服务器的链接,第二个参数:交换机名字,第三个参数:交换机类型
  87. rabbitmq.NewExchange("amqp://user:password@ip:port/", FanoutExchangeName, "fanout")
  88. // 队列绑定到exchange
  89. receiveMq.Bind(FanoutExchangeName, "")
  90. //4
  91. for {
  92. //接收消息时,指定
  93. msgs := receiveMq.Consume()
  94. go func() {
  95. for d := range msgs {
  96. log.Printf("recevie1 Received a message: %s", d.Body)
  97. }
  98. }()
  99. }
  100. }