-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener.go
82 lines (61 loc) · 1.24 KB
/
listener.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"context"
"fmt"
"os"
"os/signal"
"time"
"github.com/julian776/rcgo"
)
func main() {
lname := "testListener"
configs := &rcgo.ListenerConfigs{
Url: "amqp://user:password@localhost",
DelayOnReject: time.Second * 5,
AckIfNoHandlers: true,
Timezone: time.UTC,
LogLevel: "info",
CmdsWorkers: 1,
EventsWorkers: 1,
QueriesWorkers: 1,
PrefetchCount: 10,
}
l := rcgo.NewListener(
configs,
lname,
)
l.AddCommandHandler(
"testListener.print",
func(ctx context.Context, c *rcgo.Cmd) error {
m := c.Data
fmt.Printf("msg received %+v\n", m)
return nil
},
)
l.AddEventHandler(
"orderPlaced",
func(ctx context.Context, e *rcgo.Event) error {
m := e.Data
fmt.Printf("order received %+v\n", m)
return nil
},
)
l.AddQueryHandler(
"testListener.employees",
func(ctx context.Context, q *rcgo.Query) (interface{}, error) {
req := q.Data
id := req["id"]
fmt.Printf("id received %s\n", id)
// Fetch data
res := map[string]interface{}{
"id": id,
"name": "julian",
}
return res, nil
},
)
ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
l.Listen(ctx)
<-ctx.Done()
l.Stop()
}