2015-12-24 19:39:37 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-03-14 11:01:10 +00:00
|
|
|
"log"
|
2015-12-24 19:39:37 +00:00
|
|
|
|
|
|
|
"github.com/micro/go-micro/broker"
|
|
|
|
"github.com/micro/go-micro/cmd"
|
2015-12-24 19:52:27 +00:00
|
|
|
// To enable rabbitmq plugin uncomment
|
|
|
|
//_ "github.com/micro/go-plugins/broker/rabbitmq"
|
2015-12-24 19:39:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
topic = "go.micro.topic.foo"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Example of a shared subscription which receives a subset of messages
|
|
|
|
func sharedSub() {
|
|
|
|
_, err := broker.Subscribe(topic, func(p broker.Publication) error {
|
|
|
|
fmt.Println("[sub] received message:", string(p.Message().Body), "header", p.Message().Header)
|
|
|
|
return nil
|
2016-05-10 10:55:18 +01:00
|
|
|
}, broker.Queue("consumer"))
|
2015-12-24 19:39:37 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Example of a subscription which receives all the messages
|
|
|
|
func sub() {
|
|
|
|
_, err := broker.Subscribe(topic, func(p broker.Publication) error {
|
|
|
|
fmt.Println("[sub] received message:", string(p.Message().Body), "header", p.Message().Header)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
cmd.Init()
|
|
|
|
|
|
|
|
if err := broker.Init(); err != nil {
|
|
|
|
log.Fatalf("Broker Init error: %v", err)
|
|
|
|
}
|
|
|
|
if err := broker.Connect(); err != nil {
|
|
|
|
log.Fatalf("Broker Connect error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sub()
|
|
|
|
select {}
|
|
|
|
}
|