Skip to content

Instantly share code, notes, and snippets.

@stephenwithav
Created January 10, 2015 23:40
Show Gist options
  • Save stephenwithav/d713a94661c9661fce5a to your computer and use it in GitHub Desktop.
Save stephenwithav/d713a94661c9661fce5a to your computer and use it in GitHub Desktop.
sample nsq
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
}
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