-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtopic.go
157 lines (138 loc) · 4.25 KB
/
topic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package cmq_go
import (
"strconv"
"fmt"
)
type Topic struct {
topicName string
client *CMQClient
}
func NewTopic(topicName string, client *CMQClient) (queue *Topic) {
return &Topic{
topicName: topicName,
client: client,
}
}
func (this *Topic) SetTopicAttributes(maxMsgSize int) (err error, code int) {
code = DEFAULT_ERROR_CODE
if maxMsgSize < 1024 || maxMsgSize > 1048576 {
err = fmt.Errorf("Invalid parameter maxMsgSize < 1KB or maxMsgSize > 1024KB")
//log.Printf("%v", err.Error())
return
}
param := make(map[string]string)
param["topicName"] = this.topicName
if maxMsgSize > 0 {
param["maxMsgSize"] = strconv.Itoa(maxMsgSize)
}
_, err, code = doCall(this.client, param, "SetTopicAttributes")
if err != nil {
//log.Printf("client.call SetTopicAttributes failed: %v\n", err.Error())
return
}
return
}
func (this *Topic) GetTopicAttributes() (meta TopicMeta, err error, code int) {
code = DEFAULT_ERROR_CODE
param := make(map[string]string)
param["topicName"] = this.topicName
resMap, err, code := doCall(this.client, param, "GetTopicAttributes")
if err != nil {
//log.Printf("client.call GetTopicAttributes failed: %v\n", err.Error())
return
}
pmeta := NewTopicMeta()
pmeta.MsgCount = int(resMap["msgCount"].(float64))
pmeta.MaxMsgSize = int(resMap["maxMsgSize"].(float64))
pmeta.MsgRetentionSeconds = int(resMap["msgRetentionSeconds"].(float64))
pmeta.CreateTime = int(resMap["createTime"].(float64))
pmeta.LastModifyTime = int(resMap["lastModifyTime"].(float64))
meta = *pmeta
return
}
func (this *Topic) PublishMessage(message string) (msgId string, err error, code int) {
msgId, err, code = _publishMessage(this.client, this.topicName, message, nil, "")
return
}
func _publishMessage(client *CMQClient, topicName, msg string, tagList []string, routingKey string) (
msgId string, err error, code int) {
code = DEFAULT_ERROR_CODE
param := make(map[string]string)
param["topicName"] = topicName
param["msgBody"] = msg
if routingKey != "" {
param["routingKey"] = routingKey
}
if tagList != nil {
for i, tag := range tagList {
param["msgTag."+strconv.Itoa(i+1)] = tag
}
}
resMap, err, code := doCall(client, param, "PublishMessage")
if err != nil {
//log.Printf("client.call GetTopicAttributes failed: %v\n", err.Error())
return
}
msgId = resMap["msgId"].(string)
return
}
func (this *Topic) BatchPublishMessage(msgList []string) (msgIds []string, err error, code int) {
msgIds, err, code = _batchPublishMessage(this.client, this.topicName, msgList, nil, "")
return
}
func _batchPublishMessage(client *CMQClient, topicName string, msgList, tagList []string, routingKey string) (
msgIds []string, err error, code int) {
code = DEFAULT_ERROR_CODE
param := make(map[string]string)
param["topicName"] = topicName
if routingKey != "" {
param["routingKey"] = routingKey
}
if msgList != nil {
for i, msg := range msgList {
param["msgBody."+strconv.Itoa(i+1)] = msg
}
}
if tagList != nil {
for i, tag := range tagList {
param["msgTag."+strconv.Itoa(i+1)] = tag
}
}
resMap, err, code := doCall(client, param, "BatchPublishMessage")
if err != nil {
//log.Printf("client.call BatchPublishMessage failed: %v\n", err.Error())
return
}
resMsgList := resMap["msgList"].([]interface{})
for _, v := range resMsgList {
msg := v.(map[string]interface{})
msgIds = append(msgIds, msg["msgId"].(string))
}
return
}
func (this *Topic) ListSubscription(offset, limit int, searchWord string) (totalCount int, subscriptionList []string, err error, code int) {
code = DEFAULT_ERROR_CODE
param := make(map[string]string)
param["topicName"] = this.topicName
if searchWord != "" {
param["searchWord "] = searchWord
}
if offset >= 0 {
param["offset "] = strconv.Itoa(offset)
}
if limit > 0 {
param["limit "] = strconv.Itoa(limit)
}
resMap, err, code := doCall(this.client, param, "ListSubscriptionByTopic")
if err != nil {
//log.Printf("client.call ListSubscriptionByTopic failed: %v\n", err.Error())
return
}
totalCount = int(resMap["totalCount"].(float64))
resSubscriptionList := resMap["subscriptionList"].([]interface{})
for _, v := range resSubscriptionList {
subscribe := v.(map[string]interface{})
subscriptionList = append(subscriptionList, subscribe["subscriptionName"].(string))
}
return
}