45 lines
897 B
Go
45 lines
897 B
Go
package message
|
|
|
|
import (
|
|
"log"
|
|
|
|
"github.com/nsqio/go-nsq"
|
|
)
|
|
|
|
type myMessageHandler struct{}
|
|
|
|
func processMessage(b []byte) error {
|
|
log.Println(string(b))
|
|
return nil
|
|
}
|
|
|
|
// HandleMessage implements the Handler interface.
|
|
func (h *myMessageHandler) HandleMessage(m *nsq.Message) error {
|
|
if len(m.Body) == 0 {
|
|
return nil
|
|
}
|
|
err := processMessage(m.Body)
|
|
return err
|
|
}
|
|
|
|
func Message() {
|
|
cf := nsq.NewConfig()
|
|
|
|
consumer, err := nsq.NewConsumer("error", "error", cf)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Set the Handler for messages received by this Consumer. Can be called multiple times.
|
|
// See also AddConcurrentHandlers.
|
|
consumer.AddHandler(&myMessageHandler{})
|
|
|
|
// Use nsqlookupd to discover nsqd instances.
|
|
// See also ConnectToNSQD, ConnectToNSQDs, ConnectToNSQLookupds.
|
|
err = consumer.ConnectToNSQLookupd("ekb-app01.myway.local:4161")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
}
|