package test import ( "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbitmq" "log" "strconv" "strings" "testing" "time" ) const WorkQueueName = "test_work_queue" /** TODO:: 公平分发模式下的发送端和接收端(一个发动端,多个接受端) */ func TestWorkSend(t *testing.T) { //第一个参数指定rabbitmq服务器的链接,第二个参数指定创建队列的名字 sendMq := rabbitmq.New("链接", WorkQueueName) i := 0 for { time.Sleep(time.Second * 5) greetings := []string{"HelloWorld!", strconv.Itoa(i)} sendMq.Send("hello", strings.Join(greetings, " ")) i = i + 1 } } func TestWorkReceive1(t *testing.T) { //第一个参数指定rabbitmq服务器的链接,第二个参数指定创建队列的名字 receiveMq := rabbitmq.New("amqp://user:password@ip:port/", WorkQueueName) for { //接收消息时,指定 messages := receiveMq.Consume() go func() { for msg := range messages { log.Printf("Received a message: %s", msg.Body) } }() } } func TestWorkReceive2(t *testing.T) { //第一个参数指定rabbitmq服务器的链接,第二个参数指定创建队列的名字 receiveMq := rabbitmq.New("amqp://user:password@ip:port/", WorkQueueName) for { //接收消息时,指定 messages := receiveMq.Consume() go func() { for msg := range messages { log.Printf("Received a message: %s", msg.Body) } }() } } /** TODO:: 公平派遣模式下的发送端和接收端(公平派遣模式下发送端与公平分发相同,接收端只需要加一端配置代码) */ func TestWorkReceive3(t *testing.T) { //第一个参数指定rabbitmq服务器的链接,第二个参数指定创建队列的名字 receiveMq := rabbitmq.New("amqp://user:password@ip:port/", WorkQueueName) //todo::配置公平派遣 //(将预取计数设置为1。这告诉RabbitMQ一次不要给工人一个以上的消息。换句话说,在处理并确认上一条消息之前,不要将新消息发送给工作人员。而是将其分派给不忙的下一个工作程序。) receiveMq.Qos(1) for { //接收消息时,指定 messages := receiveMq.Consume() go func() { for msg := range messages { log.Printf("Received a message: %s", msg.Body) } }() } }