60 lines
1.3 KiB
Go
Executable File
60 lines
1.3 KiB
Go
Executable File
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
|
|
"git.wishpal.cn/wishpal_ironfan/xframe/base/threading"
|
|
"git.wishpal.cn/wishpal_ironfan/xframe/component/queue/rabbitmq"
|
|
)
|
|
|
|
func main() {
|
|
sender, err := rabbitmq.NewSender(rabbitmq.RabbitSenderConf{
|
|
URL: "amqp://guest:guest@localhost:5672/",
|
|
Exchange: rabbitmq.ExchangeConf{
|
|
Name: "test_exchange",
|
|
Type: "topic",
|
|
Durable: true,
|
|
AutoDelete: false,
|
|
Internal: false,
|
|
NoWait: false,
|
|
},
|
|
ContentType: "text/plain",
|
|
Concurrency: 3,
|
|
})
|
|
if err != nil {
|
|
fmt.Printf("new rabbitmq sender err:%v\n", err)
|
|
return
|
|
}
|
|
|
|
rg := threading.NewRoutineGroup()
|
|
for i := 0; i < 5; i++ {
|
|
_i := i
|
|
rg.RunVoid(func() {
|
|
j := 0
|
|
for {
|
|
j++
|
|
time.Sleep(1 * time.Second)
|
|
data := map[string]interface{}{
|
|
"msg": fmt.Sprintf("json test %d_%d", _i, j),
|
|
}
|
|
msg, _ := json.Marshal(data)
|
|
routeKey := fmt.Sprintf("test.%d", _i*j)
|
|
err := sender.Send(context.Background(), rabbitmq.Message{
|
|
Body: msg,
|
|
RouteKey: routeKey,
|
|
})
|
|
if err != nil {
|
|
fmt.Printf("rabbitmq send msg %s-%s, err:%v\n", routeKey, string(msg), err)
|
|
continue
|
|
}
|
|
fmt.Printf("rabbitmq send msg %s-%s, success\n", routeKey, string(msg))
|
|
}
|
|
})
|
|
}
|
|
|
|
rg.Wait()
|
|
}
|