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.

74 lines
2.2 KiB

  1. package test
  2. import (
  3. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbitmq"
  4. "fmt"
  5. "log"
  6. "testing"
  7. "time"
  8. )
  9. const TopicExchangeName = "test_topic_exchange"
  10. func TestTopicExchangeSend(t *testing.T) {
  11. var host, port, user, pwd string
  12. fmt.Scanf("%s %s %s %s", &host, &port, &user, &pwd)
  13. rabbitmq.Init(host, port, user, pwd)
  14. ch := rabbitmq.Connect(rabbitmq.Cfg.Uri)
  15. rabbitmq.NewExchange(rabbitmq.Cfg.Uri, TopicExchangeName, "topic")
  16. for {
  17. time.Sleep(1)
  18. ch.Publish("exchange", "hello world", "lazy.orange.elephant")
  19. }
  20. }
  21. func TestTopicExchangeReceive1(t *testing.T) {
  22. // 1.接收者,首先自己队列
  23. // 2.创建交换机
  24. // 3.将自己绑定到交换机上
  25. // 4.接收交换机上发过来的消息
  26. //第一个参数指定rabbitmq服务器的链接,第二个参数指定创建队列的名字
  27. receiveMq := rabbitmq.New("amqp://user:password@ip:port/", "test_topic_exchange_receive_queue_1")
  28. //第一个参数:rabbitmq服务器的链接,第二个参数:交换机名字,第三个参数:交换机类型
  29. rabbitmq.NewExchange("amqp://user:password@ip:port/", TopicExchangeName, "topic")
  30. receiveMq.Bind(TopicExchangeName, "*.orange.*")
  31. for {
  32. //接收消息时,指定
  33. messages := receiveMq.Consume()
  34. go func() {
  35. for msg := range messages {
  36. log.Printf("recevie1 Received a message: %s", msg.Body)
  37. }
  38. }()
  39. }
  40. }
  41. func TestTopicExchangeReceive2(t *testing.T) {
  42. // 1.接收者,首先自己队列
  43. // 2.创建交换机
  44. // 3.将自己绑定到交换机上
  45. // 4.接收交换机上发过来的消息
  46. //第一个参数指定rabbitmq服务器的链接,第二个参数指定创建队列的名字
  47. receiveMq := rabbitmq.New("amqp://user:password@ip:port/", "test_topic_exchange_receive_queue_2")
  48. //第一个参数:rabbitmq服务器的链接,第二个参数:交换机名字,第三个参数:交换机类型
  49. rabbitmq.NewExchange("amqp://user:password@ip:port/", TopicExchangeName, "topic")
  50. receiveMq.Bind(TopicExchangeName, "*.*.rabbit")
  51. receiveMq.Bind(TopicExchangeName, "lazy.#")
  52. for {
  53. //接收消息时,指定
  54. messages := receiveMq.Consume()
  55. go func() {
  56. for msg := range messages {
  57. log.Printf("recevie1 Received a message: %s", msg.Body)
  58. }
  59. }()
  60. }
  61. }