2016-12-28 09:20:29 -02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-05-24 18:23:07 -03:00
|
|
|
"fmt"
|
2017-03-09 17:32:05 -03:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
2017-05-24 18:23:07 -03:00
|
|
|
"time"
|
2017-03-09 17:32:05 -03:00
|
|
|
|
2016-12-28 09:20:29 -02:00
|
|
|
"github.com/eventials/goevents/amqp"
|
2017-10-05 14:11:11 -03:00
|
|
|
"github.com/eventials/goevents/messaging"
|
2016-12-28 09:20:29 -02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
conn, err := amqp.NewConnection("amqp://guest:guest@broker:5672/")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-06-22 16:33:59 -03:00
|
|
|
consumerA, err := amqp.NewConsumerConfig(conn, false, "events-exchange", "events-queue-a", amqp.ConsumerConfig{
|
2017-10-11 09:50:36 -03:00
|
|
|
ConsumeRetryInterval: 2 * time.Second,
|
|
|
|
PrefetchCount: 1,
|
2017-06-22 16:33:59 -03:00
|
|
|
})
|
2016-12-28 09:20:29 -02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:19:54 -03:00
|
|
|
consumerA.Subscribe("object.eventA", func(e messaging.Event) error {
|
|
|
|
fmt.Println("object.eventA:", string(e.Body))
|
2017-05-24 18:23:07 -03:00
|
|
|
return nil
|
2017-10-05 16:22:28 -03:00
|
|
|
}, nil)
|
2016-12-28 09:20:29 -02:00
|
|
|
|
2017-10-05 15:19:54 -03:00
|
|
|
consumerA.Subscribe("object.eventB", func(e messaging.Event) error {
|
|
|
|
fmt.Println("object.eventB:", string(e.Body))
|
2017-05-24 18:23:07 -03:00
|
|
|
return nil
|
2017-10-05 16:22:28 -03:00
|
|
|
}, nil)
|
2016-12-28 09:20:29 -02:00
|
|
|
|
2017-10-05 16:22:28 -03:00
|
|
|
consumerA.Subscribe("object.eventToRetryDelay", func(e messaging.Event) error {
|
|
|
|
fmt.Println("object.eventToRetryDelay:", string(e.Body))
|
|
|
|
return fmt.Errorf("Try again.")
|
|
|
|
}, &messaging.SubscribeOptions{
|
2017-10-05 14:11:11 -03:00
|
|
|
RetryDelay: 10 * time.Second,
|
|
|
|
DelayedRetry: true,
|
|
|
|
MaxRetries: 30,
|
|
|
|
})
|
2017-05-24 18:23:07 -03:00
|
|
|
|
2017-10-05 16:22:28 -03:00
|
|
|
consumerA.Subscribe("object.eventToRetry", func(e messaging.Event) error {
|
|
|
|
fmt.Println("object.eventToRetry:", string(e.Body))
|
|
|
|
return fmt.Errorf("Try again.")
|
|
|
|
}, &messaging.SubscribeOptions{
|
2017-10-05 14:11:11 -03:00
|
|
|
RetryDelay: 1 * time.Second,
|
|
|
|
DelayedRetry: false,
|
|
|
|
MaxRetries: 10,
|
|
|
|
})
|
2017-05-24 18:23:07 -03:00
|
|
|
|
2016-12-28 09:20:29 -02:00
|
|
|
consumerB, err := conn.Consumer(false, "events-exchange", "events-queue-b")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-10-05 15:19:54 -03:00
|
|
|
consumerB.Subscribe("object.eventC", func(e messaging.Event) error {
|
|
|
|
fmt.Println("object.eventC:", string(e.Body))
|
2017-05-24 18:23:07 -03:00
|
|
|
return nil
|
2017-10-05 16:22:28 -03:00
|
|
|
}, nil)
|
2016-12-28 09:20:29 -02:00
|
|
|
|
2017-10-05 15:19:54 -03:00
|
|
|
consumerB.Subscribe("object.eventD", func(e messaging.Event) error {
|
|
|
|
fmt.Println("object.eventD:", string(e.Body))
|
2017-05-24 18:23:07 -03:00
|
|
|
return nil
|
2017-10-05 16:22:28 -03:00
|
|
|
}, nil)
|
2016-12-28 09:20:29 -02:00
|
|
|
|
2017-03-09 17:32:05 -03:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
wg.Add(1)
|
|
|
|
consumerA.Consume()
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
wg.Add(1)
|
|
|
|
consumerB.Consume()
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
sigc := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigc, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
|
|
|
|
|
|
|
|
<-sigc
|
|
|
|
consumerA.Close()
|
|
|
|
consumerB.Close()
|
2016-12-28 09:20:29 -02:00
|
|
|
|
2017-03-09 17:32:05 -03:00
|
|
|
wg.Wait()
|
2016-12-28 09:20:29 -02:00
|
|
|
}
|