蛋蛋星球RabbitMq消费项目
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

75 рядки
2.0 KiB

  1. package consume
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/db"
  5. "applet/app/utils"
  6. "applet/app/utils/logx"
  7. "applet/consume/md"
  8. "code.fnuoos.com/EggPlanet/egg_system_rules.git"
  9. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule"
  10. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  11. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  12. "encoding/json"
  13. "errors"
  14. "fmt"
  15. "github.com/streadway/amqp"
  16. )
  17. func AddPublicPlatoonUserRelationCommissionConsume(queue md.MqQueue) {
  18. fmt.Println(">>>>>>>>>>>>AddPublicPlatoonUserRelationCommissionConsume>>>>>>>>>>>>")
  19. ch, err := rabbit.Cfg.Pool.GetChannel()
  20. if err != nil {
  21. logx.Error(err)
  22. return
  23. }
  24. defer ch.Release()
  25. //1、将自己绑定到交换机上
  26. ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey)
  27. //2、取出数据进行消费
  28. ch.Qos(1)
  29. delivery := ch.Consume(queue.Name, false)
  30. egg_system_rules.Init(cfg.RedisAddr)
  31. var res amqp.Delivery
  32. var ok bool
  33. for {
  34. res, ok = <-delivery
  35. if ok == true {
  36. err = handleAddPublicPlatoonUserRelationCommissionConsume(res.Body)
  37. if err != nil {
  38. fmt.Println("err ::: ", err)
  39. utils.FilePutContents("AddPublicPlatoonUserRelationCommissionConsume_ERR", "[err]:"+err.Error())
  40. _ = res.Reject(false)
  41. //TODO::重新推回队列末尾,避免造成队列堵塞
  42. var msg *md2.AddPublicPlatoonUserRelationCommissionReq
  43. json.Unmarshal(res.Body, &msg)
  44. ch.Publish(queue.ExchangeName, msg, queue.RoutKey)
  45. } else {
  46. _ = res.Ack(true)
  47. }
  48. } else {
  49. panic(errors.New("error getting message"))
  50. }
  51. }
  52. fmt.Println("get msg done")
  53. }
  54. func handleAddPublicPlatoonUserRelationCommissionConsume(msgData []byte) error {
  55. //1、解析mq中queue的数据结构体
  56. var msg *md2.AddPublicPlatoonUserRelationCommissionReq
  57. err := json.Unmarshal(msgData, &msg)
  58. if err != nil {
  59. return err
  60. }
  61. var req []*md2.AddPublicPlatoonUserRelationCommissionReq
  62. req = append(req, msg)
  63. _, err = rule.AddPublicPlatoonUserRelationCommission(db.Db, req)
  64. fmt.Println("err::::", err)
  65. if err != nil {
  66. return err
  67. }
  68. return nil
  69. }