nsg-0.3.0 API变更

NSQ是由知名短链接服务商bitly用Go语言开发的实时消息处理系统,具有高性能、高可靠、无视单点故障等优点,是一个非常不错的新兴的消息队列解决方案。

nsg易于配置和部署,所有参考都通过命令行指定,编译好的二进制文件,没有其它依赖项。而且支持多种消息格式。

关于nsq的介绍看这里:http://www.oschina.net/translate/day-22-a-journey-into-nsq

官网:http://nsq.io/overview/quick_start.html

相比之前,API更好用了。

直接上代码。

package main

import (
    "github.com/bitly/go-nsq"
    "fmt"
    "time"
)

type Handle struct{
    msgchan chan *nsq.Message
    stop bool
}

func (h *Handle) HandleMsg(m *nsq.Message) error {
    if !stop{
        h.msgchan <- m    
    }    
    return nil
}

func (h *Handle) Process(){
    h.stop = false
    for{
        select{
            case m := <-h.msgchan:
                fmt.Println(string(m.Body));
            case <-time.After(time.Second):
                if h.stop{
                    close(h.msgchan)
                    return
                }        
        }
    }
}

func (h *Handle) Stop(){
    h.stop = true
}

func main(){
    config := nsq.NewConfig()
    consumer, err := nsq.NewConsumer("test", "my", config)
    if err != nil{
        panic(err)
    }
    h := new(Handle)
    consumer.AddHandler(nsq.HandlerFunc(h.HandleMsg))
    h.msgchan = make(chan *nsq.Message, 1024)
    err = consumer.ConnectToNSQD("127.0.0.1:4150")
    if err != nil{
        panic(err)
    }
    h.Process()
}

 

nsg-0.3.0 API变更

上一篇:C#的delegate与C的函数指针


下一篇:window 下命令行编译连接运行