Last active
March 1, 2017 05:20
-
-
Save aalvesjr/812f630bc6f86c7f5f7c7ff72580ea4c to your computer and use it in GitHub Desktop.
Example of NSQ consumer
This file contains hidden or 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 ( | |
"github.com/rafaeljesus/nsq-event-bus" | |
"log" | |
"sync" | |
) | |
type event struct{ Body string } | |
var wg sync.WaitGroup | |
func main() { | |
wg.Add(1) // just to test purposes, the program will await for one message | |
if err := bus.On(bus.ListenerConfig{ | |
Lookup: []string{"localhost:4161"}, | |
Topic: "events", | |
Channel: "consumer1", | |
HandlerFunc: handler, | |
}); err != nil { | |
// handle failure to listen a message | |
log.Println("Error while consuming message", err) | |
} | |
wg.Wait() | |
} | |
func handler(message *bus.Message) (reply interface{}, err error) { | |
e := event{} | |
if err = message.DecodePayload(&e); err != nil { | |
// handle failure to decode a message | |
log.Println("Error while consuming message", err) | |
message.Finish() | |
wg.Done() | |
return | |
} | |
log.Println("[Consumer 1] Consuming message", e) | |
message.Finish() | |
wg.Done() | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment