Created
January 10, 2015 23:40
-
-
Save stephenwithav/d713a94661c9661fce5a to your computer and use it in GitHub Desktop.
sample nsq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"os/signal" | |
"time" | |
"github.com/bitly/go-nsq" | |
) | |
func main() { | |
cfg := nsq.NewConfig() | |
consumer, err := nsq.NewConsumer(os.Args[1], os.Args[2], cfg) | |
if err != nil { | |
log.Fatalf("Failed to connect to nsqd - %s", err) | |
} | |
consumer.AddHandler(nsq.HandlerFunc(func(m *nsq.Message) error { | |
// m.DisableAutoResponse() | |
fmt.Println(string(m.Body)) | |
return nil | |
})) | |
consumer.ConnectToNSQD("127.0.0.1:4150") | |
consumer.ConnectToNSQLookupd("127.0.0.1:4161") | |
termChan := make(chan os.Signal, 1) | |
signal.Notify(termChan) | |
<-termChan | |
consumer.Stop() | |
<-consumer.StopChan | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"os/signal" | |
"time" | |
"github.com/bitly/go-nsq" | |
) | |
func main() { | |
cfg := nsq.NewConfig() | |
producer, err := nsq.NewProducer("127.000.0.1:4150", cfg) | |
if err != nil { | |
log.Fatalf("Failed to connect to nsqd - %s", err) | |
} | |
tickerCh := time.Tick(10 * time.Second) | |
go func() { | |
for { | |
t := <-tickerCh | |
encoded, _ := t.MarshalText() | |
producer.Publish(os.Args[1], encoded) | |
fmt.Println(t) | |
} | |
}() | |
termChan := make(chan os.Signal, 1) | |
signal.Notify(termChan) | |
<-termChan | |
producer.Stop() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment