蛋蛋星球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.
 
 
 
 
 
 

61 lines
1.4 KiB

  1. package consume
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/db"
  5. "applet/app/utils"
  6. "code.fnuoos.com/EggPlanet/egg_system_rules.git"
  7. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule"
  8. "applet/app/utils/logx"
  9. "applet/consume/md"
  10. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  11. "encoding/json"
  12. "errors"
  13. "fmt"
  14. "github.com/streadway/amqp"
  15. "time"
  16. )
  17. func EggSlowAutoUpLvConsume(queue md.MqQueue) {
  18. fmt.Println(">>>>>>>>>>>>EggSlowAutoUpLvConsume>>>>>>>>>>>>")
  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 = handleEggSlowAutoUpLvConsume(res.Body)
  37. err = res.Ack(true)
  38. fmt.Println("err ::: ", err)
  39. } else {
  40. panic(errors.New("error getting message"))
  41. }
  42. }
  43. fmt.Println("get msg done")
  44. }
  45. func handleEggSlowAutoUpLvConsume(msgData []byte) error {
  46. time.Sleep(time.Duration(100) * time.Millisecond) //休眠100毫秒
  47. // 1.解析mq中queue的数据结构体
  48. var msg *md.CommUserId
  49. err := json.Unmarshal(msgData, &msg)
  50. if err != nil {
  51. return err
  52. }
  53. rule.UserUpgradeInsert(db.Db, utils.StrToInt(msg.Uid))
  54. return nil
  55. }