transfer_middleware/cmd/rpc/pkg/mq/rocketmq.go

36 lines
598 B
Go
Raw Normal View History

2024-06-12 13:46:14 +08:00
package mq
import (
"context"
2024-06-18 16:34:14 +08:00
"trasfer_middleware/until/mq"
2024-06-12 13:46:14 +08:00
)
type RocketMq struct {
}
2024-06-18 16:34:14 +08:00
type AliyunRocketMq struct {
AccessKey string
SecretKey string
SecurityToken string
ServerAddress []string
}
func (n *AliyunRocketMq) Produce(c context.Context, topic string, body []byte) error {
p, err := mq.NewProducer(n.ServerAddress[0], mq.WithProducerCredentials(n.AccessKey, n.SecretKey, n.SecurityToken))
2024-06-12 13:46:14 +08:00
if err != nil {
2024-06-18 16:34:14 +08:00
return err
2024-06-12 13:46:14 +08:00
}
2024-06-18 16:34:14 +08:00
err = p.Start()
if err != nil {
return err
2024-06-12 13:46:14 +08:00
}
2024-06-18 16:34:14 +08:00
err = p.SendSync(c, topic, body)
2024-06-12 13:46:14 +08:00
if err != nil {
2024-06-18 16:34:14 +08:00
return err
2024-06-12 13:46:14 +08:00
}
2024-06-18 16:34:14 +08:00
p.Shutdown()
2024-06-12 13:46:14 +08:00
2024-06-18 16:34:14 +08:00
return nil
2024-06-12 13:46:14 +08:00
}