package test import ( "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbitmq" "fmt" "log" "testing" "time" ) const TopicExchangeName = "test_topic_exchange" func TestTopicExchangeSend(t *testing.T) { var host, port, user, pwd string fmt.Scanf("%s %s %s %s", &host, &port, &user, &pwd) rabbitmq.Init(host, port, user, pwd) ch := rabbitmq.Connect(rabbitmq.Cfg.Uri) rabbitmq.NewExchange(rabbitmq.Cfg.Uri, TopicExchangeName, "topic") for { time.Sleep(1) ch.Publish("exchange", "hello world", "lazy.orange.elephant") } } func TestTopicExchangeReceive1(t *testing.T) { // 1.接收者,首先自己队列 // 2.创建交换机 // 3.将自己绑定到交换机上 // 4.接收交换机上发过来的消息 //第一个参数指定rabbitmq服务器的链接,第二个参数指定创建队列的名字 receiveMq := rabbitmq.New("amqp://user:password@ip:port/", "test_topic_exchange_receive_queue_1") //第一个参数:rabbitmq服务器的链接,第二个参数:交换机名字,第三个参数:交换机类型 rabbitmq.NewExchange("amqp://user:password@ip:port/", TopicExchangeName, "topic") receiveMq.Bind(TopicExchangeName, "*.orange.*") for { //接收消息时,指定 messages := receiveMq.Consume() go func() { for msg := range messages { log.Printf("recevie1 Received a message: %s", msg.Body) } }() } } func TestTopicExchangeReceive2(t *testing.T) { // 1.接收者,首先自己队列 // 2.创建交换机 // 3.将自己绑定到交换机上 // 4.接收交换机上发过来的消息 //第一个参数指定rabbitmq服务器的链接,第二个参数指定创建队列的名字 receiveMq := rabbitmq.New("amqp://user:password@ip:port/", "test_topic_exchange_receive_queue_2") //第一个参数:rabbitmq服务器的链接,第二个参数:交换机名字,第三个参数:交换机类型 rabbitmq.NewExchange("amqp://user:password@ip:port/", TopicExchangeName, "topic") receiveMq.Bind(TopicExchangeName, "*.*.rabbit") receiveMq.Bind(TopicExchangeName, "lazy.#") for { //接收消息时,指定 messages := receiveMq.Consume() go func() { for msg := range messages { log.Printf("recevie1 Received a message: %s", msg.Body) } }() } }