蛋蛋星球RabbitMq消费项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

98 lines
2.6 KiB

  1. package consume
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/db"
  5. utils2 "applet/app/utils"
  6. "applet/app/utils/cache"
  7. "applet/app/utils/logx"
  8. "applet/consume/md"
  9. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  10. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  11. "code.fnuoos.com/EggPlanet/egg_system_rules.git"
  12. "code.fnuoos.com/EggPlanet/egg_system_rules.git/jPush"
  13. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  14. "encoding/json"
  15. "errors"
  16. "fmt"
  17. "github.com/streadway/amqp"
  18. "strings"
  19. "time"
  20. )
  21. func JpushRecordConsume(queue md.MqQueue) {
  22. fmt.Println(">>>>>>>>>>>>JpushRecordConsume>>>>>>>>>>>>")
  23. ch, err := rabbit.Cfg.Pool.GetChannel()
  24. if err != nil {
  25. logx.Error(err)
  26. return
  27. }
  28. defer ch.Release()
  29. //1、将自己绑定到交换机上
  30. ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey)
  31. //2、取出数据进行消费
  32. ch.Qos(1)
  33. delivery := ch.Consume(queue.Name, false)
  34. egg_system_rules.Init(cfg.RedisAddr)
  35. var res amqp.Delivery
  36. var ok bool
  37. for {
  38. res, ok = <-delivery
  39. if ok == true {
  40. err = handleJpushRecordConsume(res.Body)
  41. if err != nil {
  42. fmt.Println("JpushRecordConsume_ERR:::::", err.Error())
  43. utils2.FilePutContents("JpushRecordConsume_ERR", utils2.SerializeStr(map[string]interface{}{
  44. "body": res.Body,
  45. "err": err.Error(),
  46. }))
  47. _ = res.Reject(false)
  48. //TODO::重新推回队列末尾,避免造成队列堵塞
  49. var msg *md.JpushRecordFundData
  50. json.Unmarshal(res.Body, &msg)
  51. ch.Publish(queue.ExchangeName, msg, queue.RoutKey)
  52. } else {
  53. //_ = res.Reject(false)
  54. err = res.Ack(true)
  55. }
  56. fmt.Println("err ::: ", err)
  57. } else {
  58. panic(errors.New("error getting message"))
  59. }
  60. }
  61. fmt.Println("get msg done")
  62. }
  63. func handleJpushRecordConsume(msgData []byte) error {
  64. time.Sleep(time.Duration(100) * time.Millisecond) //休眠100毫秒
  65. // 1.解析mq中queue的数据结构体
  66. var msg *md.JpushRecordFundData
  67. err := json.Unmarshal(msgData, &msg)
  68. if err != nil {
  69. return err
  70. }
  71. engine := db.Db
  72. redisConn := cache.GetPool().Get()
  73. sysCfgDb := implement.NewSysCfgDb(engine, redisConn)
  74. jpushKey := sysCfgDb.SysCfgGetWithDb("jpush_key")
  75. jpushSecret := sysCfgDb.SysCfgGetWithDb("jpush_secret")
  76. if msg.Target == "0" { //广播全部
  77. _, err := jPush.PushAllUser(jpushKey, jpushSecret, msg.Title, msg.Content, msg.Platform, nil)
  78. if err != nil {
  79. return err
  80. }
  81. } else {
  82. _, err = jPush.PushMoreUser(jpushKey, jpushSecret, msg.Title, msg.Content, msg.Platform, strings.Split(msg.UserId, ","), nil)
  83. if err != nil {
  84. return err
  85. }
  86. }
  87. if msg.Id != "" {
  88. engine.Where("id=?", msg.Id).Cols("state").Update(&model.JpushRecord{State: 1})
  89. }
  90. return nil
  91. }