作者 tangxvhui

消息处理

  1 +{
  2 + // 使用 IntelliSense 了解相关属性。
  3 + // 悬停以查看现有属性的描述。
  4 + // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
  5 + "version": "0.2.0",
  6 + "configurations": [
  7 +
  8 + {
  9 + "name": "Launch file",
  10 + "type": "go",
  11 + "request": "launch",
  12 + "mode": "debug",
  13 + "buildFlags": "--tags=local",
  14 + "program": "./main.go"
  15 + },
  16 + ]
  17 +}
@@ -6,17 +6,17 @@ import ( @@ -6,17 +6,17 @@ import (
6 ) 6 )
7 7
8 type ReceivedMessage struct { 8 type ReceivedMessage struct {
9 - MessageId int64  
10 - MessageType string  
11 - MessageBody string  
12 - OccurredOn time.Time  
13 - CreateAt time.Time 9 + MessageId int64 `json:"MessageId"`
  10 + MessageType string `json:"MessageType"`
  11 + MessageBody string `json:"MessageBody"`
  12 + OccurredOn time.Time `json:"OccurredOn"`
  13 + CreateAt time.Time `json:"-"`
14 } 14 }
15 15
16 type MessageBody struct { 16 type MessageBody struct {
17 Module string `json:"module"` 17 Module string `json:"module"`
18 Action string `json:"action"` 18 Action string `json:"action"`
19 - Datas json.RawMessage `json:"data"` // 具体的对象JSON数据 19 + Datas json.RawMessage `json:"datas"` // 具体的对象JSON数据
20 } 20 }
21 21
22 type ReceivedMessageRepository interface { 22 type ReceivedMessageRepository interface {
@@ -15,22 +15,23 @@ import ( @@ -15,22 +15,23 @@ import (
15 15
16 func SyncDataBusinessAdmin(msgData *sarama.ConsumerMessage) error { 16 func SyncDataBusinessAdmin(msgData *sarama.ConsumerMessage) error {
17 var receivedMsg domain.ReceivedMessage 17 var receivedMsg domain.ReceivedMessage
  18 +
  19 + log.Logger.Debug(string(msgData.Value))
18 err := json.Unmarshal(msgData.Value, &receivedMsg) 20 err := json.Unmarshal(msgData.Value, &receivedMsg)
19 if err != nil { 21 if err != nil {
  22 + log.Logger.Error("解析ReceivedMessage 失败" + err.Error())
20 return err 23 return err
21 } 24 }
22 transactionContext, err := factory.CreateTransactionContext(nil) 25 transactionContext, err := factory.CreateTransactionContext(nil)
23 if err != nil { 26 if err != nil {
24 return err 27 return err
25 } 28 }
26 - transactionContext.StartTransaction()  
27 - var transactionIsSucceed bool 29 + _ = transactionContext.StartTransaction()
  30 +
28 defer func() { 31 defer func() {
29 - if transactionIsSucceed {  
30 - transactionContext.CommitTransaction()  
31 - } else {  
32 - transactionContext.RollbackTransaction()  
33 - } 32 +
  33 + _ = transactionContext.RollbackTransaction()
  34 +
34 }() 35 }()
35 36
36 msgRepo := factory.CreateReceivedMessageRepository(map[string]interface{}{ 37 msgRepo := factory.CreateReceivedMessageRepository(map[string]interface{}{
@@ -87,5 +88,6 @@ func SyncDataBusinessAdmin(msgData *sarama.ConsumerMessage) error { @@ -87,5 +88,6 @@ func SyncDataBusinessAdmin(msgData *sarama.ConsumerMessage) error {
87 log.Logger.Error(" 保存新消息发生错误 " + err.Error()) 88 log.Logger.Error(" 保存新消息发生错误 " + err.Error())
88 log.Logger.Info("异常消息message_id=" + strconv.FormatInt(receivedMsg.MessageId, 10)) 89 log.Logger.Info("异常消息message_id=" + strconv.FormatInt(receivedMsg.MessageId, 10))
89 } 90 }
  91 + _ = transactionContext.CommitTransaction()
90 return nil 92 return nil
91 } 93 }