-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevent_server.go
78 lines (73 loc) · 2.24 KB
/
event_server.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
package synapse
import (
"github.com/bitly/go-simplejson"
"strings"
"github.com/streadway/amqp"
"fmt"
)
/**
绑定事件监听队列
*/
func (s *Server) eventQueue() *amqp.Channel {
channel := s.CreateChannel(s.EventProcessNum, "EventServer")
q, err := channel.QueueDeclare(
fmt.Sprintf("%s_%s_event", s.SysName, s.AppName), // name
true, // durable
true, // delete when usused
false, // exclusive
false, // no-wait
nil, // arguments
)
if err != nil {
Log(fmt.Sprintf("Failed to declare event queue: %s", err), LogError)
}
for k := range s.EventCallback {
err = channel.QueueBind(
q.Name, // queue name
"event."+k, // routing key
s.SysName, // exchange
false,
nil)
if err != nil {
Log(fmt.Sprintf("Failed to bind event queue: %s", k), LogError)
}
}
return channel
}
/**
创建事件监听
callback回调为监听到事件后的处理函数
*/
func (s *Server) eventServer(channel *amqp.Channel) {
msgs, err := channel.Consume(
fmt.Sprintf("%s_%s_event", s.SysName, s.AppName), // queue
fmt.Sprintf("%s.%s.event.%s", s.SysName, s.AppName, s.AppId), // consumer
false, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
if err != nil {
Log(fmt.Sprintf("Failed to register event consumer: %s", err), LogError)
}
for d := range msgs {
go s.eventHandler(d)
}
}
/**
事件处理器
*/
func (s *Server) eventHandler(d amqp.Delivery) {
query, _ := simplejson.NewJson(d.Body)
if s.Debug {
logData, _ := query.MarshalJSON()
Log(fmt.Sprintf("Event Receive: %s@%s %s", d.Type, d.ReplyTo, logData), LogDebug)
}
callback, ok := s.EventCallback[strings.Replace(d.RoutingKey, "event.", "", 1)]
if ok && callback(query, d) {
d.Ack(false)
} else {
d.Reject(true)
}
}