func Run(nsqLookupdHTTPAddrs []string, st store.Store) { cfg := nsq.NewConfig() cfg.UserAgent = fmt.Sprintf("phosphor go-nsq/%s", nsq.VERSION) cfg.MaxInFlight = maxInFlight consumer, err := nsq.NewConsumer(topic, channel, cfg) if err != nil { log.Critical(err) os.Exit(1) } consumer.AddHandler(&IngestionHandler{ store: st, }) err = consumer.ConnectToNSQLookupds(nsqLookupdHTTPAddrs) if err != nil { log.Critical(err) os.Exit(1) } // Block until exit <-consumer.StopChan }
func (arch *Archiver) init() { arch.pending = make(chan []byte) arch.stop = make(chan bool) cfg := nsq.NewConfig() consumer, err := nsq.NewConsumer(TOPIC, CHANNEL, cfg) if err != nil { log.Critical(err) os.Exit(-1) } // message process consumer.AddHandler(nsq.HandlerFunc(func(msg *nsq.Message) error { arch.pending <- msg.Body return nil })) // read environtment variable addresses := []string{DEFAULT_NSQLOOKUPD} if env := os.Getenv(ENV_NSQLOOKUPD); env != "" { addresses = strings.Split(env, ";") } // connect to nsqlookupd log.Trace("connect to nsqlookupds ip:", addresses) if err := consumer.ConnectToNSQLookupds(addresses); err != nil { log.Critical(err) return } log.Info("nsqlookupd connected") go arch.archive_task() }
func (p *NsqSrcProvider) Init(ctx *sj.Json) error { var err error p.nsqMsgChan = make(chan nsq.Message) // not buffered p.timeOutSec = ctx.Get("timeout_sec").MustInt() nsqLookupdAddrs := ctx.Get("nsq_lookupd_addrs").MustStringArray() if len(nsqLookupdAddrs) <= 0 { return errors.New("no nsq_lookupd_addr in provided") } topic := ctx.Get("topic").MustString() channel := ctx.Get("channel").MustString() p.config = nsq.NewConfig() p.consumer, err = nsq.NewConsumer(topic, channel, p.config) if p.consumer == nil || err != nil { return err } p.consumer.AddHandler(nsq.HandlerFunc(func(message *nsq.Message) error { p.nsqMsgChan <- *message return nil })) err = p.consumer.ConnectToNSQLookupds(nsqLookupdAddrs) // ip:port if err != nil { return errors.New("Could not connect to nsqLookupd") } return nil }
func consummer() { cfg := nsq.NewConfig() c, _ := nsq.NewConsumer("test", "c1", cfg) c.AddHandler(nsq.HandlerFunc(func(msg *nsq.Message) error { // 这个比定义一个实现handler interface的struct要快捷的多 fmt.Println(msg.Body) return nil })) c.ConnectToNSQLookupd(host + ":4161") <-c.StopChan }
// Subscribe prepares the peer to consume messages. func (n *Peer) Subscribe() error { consumer, err := nsq.NewConsumer(topic, broker.GenerateName(), nsq.NewConfig()) if err != nil { return err } consumer.AddHandler(nsq.HandlerFunc(func(message *nsq.Message) error { n.messages <- message.Body return nil })) if err := consumer.ConnectToNSQD(n.host); err != nil { return err } n.consumer = consumer return nil }
func init() { //初始化配置 var configError error config, configError = goconfig.ReadConfigFile("conf.ini") if configError != nil { log.Panic("can not read the config file") } //初始化主机名 var errHost error hostname, errHost = os.Hostname() if errHost != nil { log.Fatal("Hostname can not obtain!") } AppCommUnitInit() //端口 Port, _ = config.GetString("server", "port") //初始化指令接收者 configConsumer := nsq.NewConfig() consumerTopic, _ := config.GetString("message", "consumerTopic") log.Println("aaaaaaa") log.Println(consumerTopic) q, _ = nsq.NewConsumer(consumerTopic, "ch", configConsumer) //初始化指令发送者 configProducer := nsq.NewConfig() nsq_url, _ := config.GetString("message", "nsq_url") w, _ = nsq.NewProducer(nsq_url, configProducer) delay, _ = config.GetInt64("message", "delay_time") //指令回复处理handler Receive(cs) }