|
- package test
-
- import (
- "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbitmq"
- "fmt"
- "log"
- "strconv"
- "strings"
- "testing"
- "time"
- )
-
- const FanoutExchangeName = "test_fanout_exchange"
-
- func TestFanoutExchangeSend(t *testing.T) {
- var host, port, user, pwd string
- log.SetFlags(log.Lshortfile | log.LstdFlags)
- log.Println("input host,port,user,pwd")
- fmt.Scanf("%s %s %s %s", &host, &port, &user, &pwd)
- if host == "" && port == "" && user == "" && pwd == "" {
- host = "119.23.182.117"
- port = "5672"
- user = "admin"
- pwd = "123456"
- }
- rabbitmq.Init(host, port, user, pwd)
- ch := rabbitmq.Connect(rabbitmq.Cfg.Uri)
- rabbitmq.NewExchange(rabbitmq.Cfg.Uri, FanoutExchangeName, "fanout")
-
- i := 0
- for {
- time.Sleep(1)
- greetings := []string{"Helloworld!", strconv.Itoa(i)}
- ch.Publish(FanoutExchangeName, strings.Join(greetings, " "), "")
- i = i + 1
- }
- }
-
- func TestFanoutExchangeReceive1(t *testing.T) {
- // 1.接收者,首先创建自己队列
- // 2.创建交换机
- // 3.将自己绑定到交换机上
- // 4.接收交换机上发过来的消息
- var host, port, user, pwd string
- log.SetFlags(log.Lshortfile | log.LstdFlags)
- log.Println("input host,port,user,pwd")
- fmt.Scanf("%s %s %s %s", &host, &port, &user, &pwd)
- if host == "" && port == "" && user == "" && pwd == "" {
- host = "119.23.182.117"
- port = "5672"
- user = "admin"
- pwd = "123456"
- }
- rabbitmq.Init(host, port, user, pwd)
- //第一个参数指定rabbitmq服务器的链接,第二个参数指定创建队列的名字
- receiveMq := rabbitmq.New(rabbitmq.Cfg.Uri, "test_fanout_exchange_receive_queue_1")
- //第一个参数:rabbitmq服务器的链接,第二个参数:交换机名字,第三个参数:交换机类型
- rabbitmq.NewExchange(rabbitmq.Cfg.Uri, FanoutExchangeName, "fanout")
- // 队列绑定到exchange
- receiveMq.Bind(FanoutExchangeName, "")
- //4
- forever := make(chan bool) //开辟管道
- var i = 0
- //for {
- //接收消息时,指定
- messages := receiveMq.Consume()
- go func() {
- for msg := range messages {
- log.Printf("recevie1 Received a message: %s", msg.Body)
- i++
- if i >= 5 {
- msg.Reject(true)
- receiveMq.CloseChannel()
- return
- } else {
- msg.Ack(false)
- }
- }
- }()
- log.Printf("[*] Waiting for message,To exit press CTRL+C")
- <-forever
- //}
- }
-
- func TestFanoutExchangeReceive2(t *testing.T) {
- // 1.接收者,首先创建自己队列
- // 2.创建交换机
- // 3.将自己绑定到交换机上
- // 4.接收交换机上发过来的消息
-
- //第一个参数指定rabbitmq服务器的链接,第二个参数指定创建队列的名字
- receiveMq := rabbitmq.New("amqp://user:password@ip:port/", "test_fanout_exchange_receive_queue_2")
- //第一个参数:rabbitmq服务器的链接,第二个参数:交换机名字,第三个参数:交换机类型
- rabbitmq.NewExchange("amqp://user:password@ip:port/", FanoutExchangeName, "fanout")
- // 队列绑定到exchange
- receiveMq.Bind(FanoutExchangeName, "")
- //4
- for {
- //接收消息时,指定
- msgs := receiveMq.Consume()
- go func() {
- for d := range msgs {
- log.Printf("recevie1 Received a message: %s", d.Body)
- }
- }()
- }
- }
|