-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
51 lines (40 loc) · 1.04 KB
/
message.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
package litetq
import (
"encoding/json"
"fmt"
"strings"
"time"
"github.com/google/uuid"
)
const MaxRetrySeconds = 300
type MessageType string
type Message struct {
MsgId string `json:"msgId"` // 消息ID
Retry int `json:"retry"` // 重试次数
Type MessageType `json:"type"` // 消息类型
Created time.Time `json:"created"` // 事件时间
Data string `json:"data"` // 消息内容,JSON
}
func (m *Message) Table() string {
return "tqmsg"
}
func (m *Message) String() string {
return fmt.Sprintf("msgId:%s, retry:%d, type:%s, created:%s, data:%s",
m.MsgId, m.Retry, m.Type, m.Created, m.Data)
}
func (m *Message) Unmarshal(v interface{}) error {
return json.Unmarshal([]byte(m.Data), &v)
}
// NewMessage 创建Message, data会JSON
func NewMessage(t MessageType, data interface{}) *Message {
b, _ := json.Marshal(data)
return &Message{
MsgId: NewId(),
Type: t,
Created: time.Now(),
Data: string(b),
}
}
func NewId() string {
return strings.Replace(uuid.NewString(), "-", "", -1)
}