1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
package main
import (
"fmt"
"time"
"github.com/nsqio/go-nsq"
)
type GdNsqConfig struct {
Host string `json:"host"`
Port string `json:"port"`
Topic string `json:"topic"`
Channel string `json:"channel"`
NotifyURL string `json:"notify_url"`
}
var (
nsqConfig *GdNsqConfig = &GdNsqConfig{
Host: "192.168.2.189",
Port: "4150",
Topic: "TOPIC_DATA_FILEPATH_440115cVHz",
Channel: "test",
}
)
func checkError(err error) {
if err != nil {
panic(err.Error())
}
}
func main() {
config := nsq.NewConfig()
consumer, err := nsq.NewConsumer(nsqConfig.Topic, nsqConfig.Channel, config)
checkError(err)
consumer.AddHandler(&myMessageHandler{})
err = consumer.ConnectToNSQD(fmt.Sprintf("%s:%s", nsqConfig.Host, nsqConfig.Port))
checkError(err)
ticker := time.NewTicker(100 * time.Second)
select {
case <-ticker.C:
fmt.Println("ticker stop")
}
consumer.Stop()
}
type myMessageHandler struct{}
// HandleMessage implements the Handler interface.
func (h *myMessageHandler) HandleMessage(m *nsq.Message) error {
fmt.Println(string(m.Body))
return nil
}
|