2 Commits

Author SHA1 Message Date
  guzeng b64b4697ca 修改SendMsg发送 1 year ago
  guzeng 6db5138ede 增加SendMsg方法 1 year ago
1 changed files with 25 additions and 1 deletions
Unified View
  1. +25
    -1
      conn.go

+ 25
- 1
conn.go View File

@ -3,10 +3,11 @@ package rocketmq
import ( import (
"context" "context"
"errors" "errors"
"log"
"github.com/apache/rocketmq-client-go/v2" "github.com/apache/rocketmq-client-go/v2"
"github.com/apache/rocketmq-client-go/v2/primitive" "github.com/apache/rocketmq-client-go/v2/primitive"
"github.com/apache/rocketmq-client-go/v2/producer" "github.com/apache/rocketmq-client-go/v2/producer"
"log"
) )
var MqPool rocketmq.Producer var MqPool rocketmq.Producer
@ -28,6 +29,7 @@ func MqConnect(group string, resolver []string) {
} }
MqPool = p MqPool = p
} }
func SendSync(ctx context.Context, mq *primitive.Message) (*primitive.SendResult, error) { func SendSync(ctx context.Context, mq *primitive.Message) (*primitive.SendResult, error) {
var res primitive.SendResult var res primitive.SendResult
if MqPool == nil { if MqPool == nil {
@ -35,3 +37,25 @@ func SendSync(ctx context.Context, mq *primitive.Message) (*primitive.SendResult
} }
return MqPool.SendSync(ctx, mq) return MqPool.SendSync(ctx, mq)
} }
func Conn(group string, resolver []string) {
MqConnect(group, resolver)
}
/**
* 发送mq消息
* topic
* tag
* message json格式化
* 2023/02/17
*/
func SendMsg(ctx context.Context, topic, tag string, message []byte) (*primitive.SendResult, error) {
if MqPool == nil {
return nil, errors.New("Mq未链接")
}
msg := primitive.NewMessage(topic, message)
msg.WithTag(tag)
return MqPool.SendSync(ctx, msg)
}

Loading…
Cancel
Save