Permalink
Fetching contributors…
Cannot retrieve contributors at this time
52 lines (39 sloc) 1.07 KB
// Sources for https://watermill.io/docs/getting-started/
package main
import (
"log"
"time"
"github.com/satori/go.uuid"
"github.com/ThreeDotsLabs/watermill/message"
"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill/message/infrastructure/gochannel"
)
func main() {
pubSub := gochannel.NewGoChannel(
0, // buffer (channel) size
watermill.NewStdLogger(false, false),
time.Second, // send timeout
)
messages, err := pubSub.Subscribe("example.topic")
if err != nil {
panic(err)
}
go process(messages)
publishMessages(pubSub)
}
func publishMessages(publisher message.Publisher) {
for {
msg := message.NewMessage(uuid.NewV4().String(), []byte("Hello, world!"))
if err := publisher.Publish("example.topic", msg); err != nil {
panic(err)
}
}
}
func process(messages chan *message.Message) {
for msg := range messages {
log.Printf("received message: %s, payload: %s", msg.UUID, string(msg.Payload))
// we need to Acknowledge that we received and processed the message,
// otherwise we will not receive next message
msg.Ack()
}
}