|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package test
-
- import (
- "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbitmq"
- "log"
- "strconv"
- "strings"
- "testing"
- "time"
- )
-
- const DirectExchangeName = "test_direct_exchange"
- const DirectExchangeRoutingKey1 = "direct_exchange_routing_key_1"
- const DirectExchangeRoutingKey2 = "direct_exchange_routing_key_2"
-
- func TestDirectExchangeSend(t *testing.T) {
- ch := rabbitmq.Connect("amqp://user:password@ip:port/")
- rabbitmq.NewExchange("amqp://user:password@ip:port/", DirectExchangeName, "direct")
- i := 0
- for {
- time.Sleep(1)
- greetings := []string{"HelloWorld!", strconv.Itoa(i)}
- if i%2 == 1 {
- //如果是奇数
- ch.Publish("exchange", strings.Join(greetings, " "), DirectExchangeRoutingKey1)
- } else {
- ch.Publish("exchange", strings.Join(greetings, " "), DirectExchangeRoutingKey2)
- }
- i = i + 1
- }
- }
-
- func TestDirectExchangeReceive1(t *testing.T) {
- // 1.接收者,首先自己队列
- // 2.创建交换机
- // 3.将自己绑定到交换机上
- // 4.接收交换机上发过来的消息
- //第一个参数指定rabbitmq服务器的链接,第二个参数指定创建队列的名字
-
- receiveMq := rabbitmq.New("amqp://user:password@ip:port/", "test_direct_exchange_receive_queue_1")
-
- //第一个参数:rabbitmq服务器的链接,第二个参数:交换机名字,第三个参数:交换机类型
- rabbitmq.NewExchange("amqp://user:password@ip:port/", "exchange", "direct")
-
- receiveMq.Bind(DirectExchangeName, DirectExchangeRoutingKey1)
-
- //4
- for {
- //接收消息时,指定
- messages := receiveMq.Consume()
- go func() {
- for msg := range messages {
- log.Printf("recevie1 Received a message: %s", msg.Body)
- }
- }()
- }
- }
-
- func TestDirectExchangeReceive2(t *testing.T) {
- // 1.接收者,首先自己队列
- // 2.创建交换机
- // 3.将自己绑定到交换机上
- // 4.接收交换机上发过来的消息
- //第一个参数指定rabbitmq服务器的链接,第二个参数指定创建队列的名字
-
- receiveMq := rabbitmq.New("amqp://user:password@ip:port/", "test_direct_exchange_receive_queue_2")
-
- //第一个参数:rabbitmq服务器的链接,第二个参数:交换机名字,第三个参数:交换机类型
- rabbitmq.NewExchange("amqp://user:password@ip:port/", "exchange", "direct")
-
- receiveMq.Bind(DirectExchangeName, DirectExchangeRoutingKey2)
-
- //4
- for {
- //接收消息时,指定
- messages := receiveMq.Consume()
- go func() {
- for msg := range messages {
- log.Printf("recevie1 Received a message: %s", msg.Body)
- }
- }()
- }
- }
|