Example #1
0
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()
}
Example #2
0
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
}
Example #3
0
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

}
Example #4
0
// 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
}
Example #5
0
func Receive(cs map[string]chan []*Report.Command_Param) {
	q.AddHandler(nsq.HandlerFunc(func(message *nsq.Message) error {
		defer func() {
			if x := recover(); x != nil {
				log.Println("sorry, chan has closed")
			}
		}()

		log.Printf("Got one message: %v", message.Body)

		res := BuildResult(message.Body)
		if res.commandType == int32(Report.Command_CMT_REPWARNUP) {

			var jsonRes []byte //返回内容
			var paras []*Report.Command_Param
			paras = []*Report.Command_Param(res.paras)

			mapValue := make(map[string]string)
			mapValue["warntime"] = strconv.Itoa(int(paras[0].Npara))
			mapValue["devtype"] = strconv.Itoa(int(paras[1].Npara))
			mapValue["devid"] = strconv.Itoa(int(paras[2].Npara))
			mapValue["warnstyle"] = strconv.Itoa(int(paras[3].Npara))

			jsonRes, _ = json.Marshal(mapValue)
			SendMsg(res.mac, string(jsonRes))
		} else if res.commandType == int32(Report.Command_CMT_REPADDDELDEVICE) {

			var jsonRes []byte //返回内容
			var paras []*Report.Command_Param
			paras = []*Report.Command_Param(res.paras)

			mapValue := make(map[string]string)
			mapValue["action"] = strconv.Itoa(int(paras[0].Npara))
			mapValue["devtype"] = strconv.Itoa(int(paras[1].Npara))
			mapValue["devid"] = strconv.Itoa(int(paras[2].Npara))
			mapValue["manufacturers"] = strconv.Itoa(int(paras[3].Npara))
			mapValue["devstate"] = strconv.Itoa(int(paras[4].Npara))
			jsonRes, _ = json.Marshal(mapValue)
			SendMsg(res.mac, string(jsonRes))

		} else {
			chanKey := res.mac + strconv.Itoa(int(res.serialNum))
			log.Println("nsqrecv" + chanKey)
			ci, ok := cs[chanKey]
			log.Println(cs)
			if !ok {
				log.Println("no chan!")
			} else {
				ci <- res.paras
				close(ci)
				delete(cs, chanKey)
				log.Println("delete existed chan!")
			}
		}
		return nil
	}))

	nsqlookup_url, _ := config.GetString("message", "nsqlookup_url")
	errConsumer := q.ConnectToNSQD(nsqlookup_url)
	if errConsumer != nil {
		log.Panic("Consumer could not connect nsq")
	}
}