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.

work_queue_test.go 2.2 KiB

2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package test
  2. import (
  3. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbitmq"
  4. "log"
  5. "strconv"
  6. "strings"
  7. "testing"
  8. "time"
  9. )
  10. const WorkQueueName = "test_work_queue"
  11. /**
  12. TODO:: 公平分发模式下的发送端和接收端(一个发动端,多个接受端)
  13. */
  14. func TestWorkSend(t *testing.T) {
  15. //第一个参数指定rabbitmq服务器的链接,第二个参数指定创建队列的名字
  16. sendMq := rabbitmq.New("链接", WorkQueueName)
  17. i := 0
  18. for {
  19. time.Sleep(time.Second * 5)
  20. greetings := []string{"HelloWorld!", strconv.Itoa(i)}
  21. sendMq.Send("hello", strings.Join(greetings, " "))
  22. i = i + 1
  23. }
  24. }
  25. func TestWorkReceive1(t *testing.T) {
  26. //第一个参数指定rabbitmq服务器的链接,第二个参数指定创建队列的名字
  27. receiveMq := rabbitmq.New("amqp://user:password@ip:port/", WorkQueueName)
  28. for {
  29. //接收消息时,指定
  30. messages := receiveMq.Consume()
  31. go func() {
  32. for msg := range messages {
  33. log.Printf("Received a message: %s", msg.Body)
  34. }
  35. }()
  36. }
  37. }
  38. func TestWorkReceive2(t *testing.T) {
  39. //第一个参数指定rabbitmq服务器的链接,第二个参数指定创建队列的名字
  40. receiveMq := rabbitmq.New("amqp://user:password@ip:port/", WorkQueueName)
  41. for {
  42. //接收消息时,指定
  43. messages := receiveMq.Consume()
  44. go func() {
  45. for msg := range messages {
  46. log.Printf("Received a message: %s", msg.Body)
  47. }
  48. }()
  49. }
  50. }
  51. /**
  52. TODO:: 公平派遣模式下的发送端和接收端(公平派遣模式下发送端与公平分发相同,接收端只需要加一端配置代码)
  53. */
  54. func TestWorkReceive3(t *testing.T) {
  55. //第一个参数指定rabbitmq服务器的链接,第二个参数指定创建队列的名字
  56. receiveMq := rabbitmq.New("amqp://user:password@ip:port/", WorkQueueName)
  57. //todo::配置公平派遣
  58. //(将预取计数设置为1。这告诉RabbitMQ一次不要给工人一个以上的消息。换句话说,在处理并确认上一条消息之前,不要将新消息发送给工作人员。而是将其分派给不忙的下一个工作程序。)
  59. receiveMq.Qos(1)
  60. for {
  61. //接收消息时,指定
  62. messages := receiveMq.Consume()
  63. go func() {
  64. for msg := range messages {
  65. log.Printf("Received a message: %s", msg.Body)
  66. }
  67. }()
  68. }
  69. }