gitee.com/h79/goutils@v1.22.10/mq/consumer.go (about)

     1  package mq
     2  
     3  type Topic struct {
     4  	Topic      string
     5  	BrokerName string
     6  	QueueId    int
     7  }
     8  
     9  type Expression struct {
    10  	Type string
    11  	Exp  string
    12  }
    13  
    14  type Message struct {
    15  	MsgId    string
    16  	QueueId  int
    17  	Topic    string
    18  	Tags     string
    19  	Keys     string
    20  	Body     string
    21  	Property map[string]string
    22  }
    23  
    24  type Status int
    25  
    26  const (
    27  	ConOk   = Status(0)
    28  	ConFail = Status(1)
    29  )
    30  
    31  type Type int
    32  
    33  const (
    34  	ConTypePush = Type(0)
    35  	ConTypePull = Type(1)
    36  )
    37  
    38  type Consumer interface {
    39  	Start() error
    40  
    41  	Stop()
    42  
    43  	//Subscribe 建议外面使用 go goroutine
    44  	Subscribe(topic Topic, exp Expression, call func(msg *Message) Status) error
    45  }
    46  
    47  func NewConsumer(con Type, cfg Config) Consumer {
    48  	switch con {
    49  	case ConTypePush:
    50  		return NewPushConsumer(cfg)
    51  	case ConTypePull:
    52  		return NewPullConsumer(cfg)
    53  	}
    54  	return nil
    55  }