This repository has been archived on 2024-11-28. You can view files and clone it, but cannot push or open issues or pull requests.
Files
schisandra-cloud-album/mq/comment_like_mq.go
2024-09-29 15:46:35 +08:00

45 lines
1.1 KiB
Go

package mq
import (
"fmt"
"github.com/nsqio/go-nsq"
"log"
"schisandra-cloud-album/core"
"schisandra-cloud-album/global"
)
const CommentLikeTopic = "comment_like"
type MessageHandler struct{}
func (h *MessageHandler) HandleMessage(m *nsq.Message) error {
if len(m.Body) == 0 {
// Returning nil will automatically send a FIN command to NSQ to mark the message as processed.
// In this case, a message with an empty body is simply ignored/discarded.
return nil
}
// do whatever actual message processing is desired
//err := processMessage(m.Body)
fmt.Println("comment_like_mq:", string(m.Body))
// Returning a non-nil error will automatically send a REQ command to NSQ to re-queue the message.
return nil
}
func CommentLikeProducer(messageBody []byte) {
err := global.NSQProducer.Publish(CommentLikeTopic, messageBody)
if err != nil {
global.LOG.Fatal(err)
}
}
func CommentLikeConsumer() {
consumer := core.InitConsumer(CommentLikeTopic)
consumer.AddHandler(&MessageHandler{})
err := consumer.ConnectToNSQD(global.CONFIG.NSQ.NsqAddr())
if err != nil {
log.Fatal(err)
}
}