main.go
2.1 KB
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
package main
import (
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/message/publisher/kafka_message/sarama"
"github.com/linmadan/egglib-go/message/publisher/local_Message"
"github.com/linmadan/egglib-go/utils/json"
_ "gitlab.fjmaimaimai.com/mmm-go-pp/egglib-go-demos/orm/go-pg"
"gitlab.fjmaimaimai.com/mmm-go-pp/egglib-go-demos/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go-pp/egglib-go-demos/pkg/application/message/command"
"gitlab.fjmaimaimai.com/mmm-go-pp/egglib-go-demos/pkg/constant"
"gitlab.fjmaimaimai.com/mmm-go-pp/egglib-go-demos/pkg/log"
"math"
"math/rand"
"time"
)
func main() {
engineOptions := make(map[string]interface{})
engineOptions["kafkaHosts"] = constant.KAFKA_HOSTS
storeOptions := make(map[string]interface{})
transaction, _ := factory.CreateTransactionContext(nil)
transaction.StartTransaction()
defer transaction.RollbackTransaction()
storeOptions["transactionContext"] = transaction
local_message.LaunchLocalMessageDispatcher(time.Second*30, "sarama", engineOptions, "pg", storeOptions, log.Logger)
publisher, err := sarama.NewKafkaSaramaMessagePublisher(constant.KAFKA_HOSTS, log.Logger)
if err != nil {
log.Logger.Error(err.Error())
transaction.RollbackTransaction()
return
}
rand.Seed(time.Now().Unix())
messages := []*application.Message{
&application.Message{
MessageId: int64(rand.Intn(math.MaxInt32)),
MessageType: constant.PUSH_MESSAGE_TOPIC,
MessageBody: json.MarshalToString(command.PushMessageCommand{
MessageType: 1,
Title: "卖家发货通知",
Content: "您的商品编号:xxx 已发货",
SenderUid: 2,
SenderName: "买家二号店",
Receivers: []command.Receiver{
command.Receiver{ReceiverName: "刘德华", ReceiverUid: 100},
},
CompanyId: 999,
}),
OccurredOn: time.Now(),
},
}
err = publisher.PublishMessages(messages, nil)
if err != nil {
log.Logger.Error(err.Error())
transaction.RollbackTransaction()
return
}
if err = transaction.CommitTransaction(); err != nil {
return
}
time.Sleep(time.Second * 10)
}