This repository has been archived by the owner on Apr 2, 2021. It is now read-only.
forked from dstockhammer/franklin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatcher.go
120 lines (102 loc) · 3.09 KB
/
dispatcher.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package franklin
import (
"encoding/json"
"log"
"reflect"
"github.com/streadway/amqp"
)
// Dispatcher is going to be a go port of the C# ServiceActivator.
// Currently it's just an arbitrary implementation that connects to AMQ
// and consumes messages.
type Dispatcher interface {
Receive()
Close()
}
type amqpDispatcher struct {
subscriberRegistry *SubscriberRegistry
messageMapperRegistry *MessageMapperRegistry
connection *amqp.Connection
channel *amqp.Channel
exchange string
}
// Receive begins listening for messages on channels,
// and dispatching them to request handlers.
func (d *amqpDispatcher) Receive() {
for key, consumer := range d.subscriberRegistry.Consumers() {
queueName := d.subscriberRegistry.QueueForKey(key)
q, err := d.channel.QueueDeclare(
queueName, // name
true, // durable
false, // delete when usused
false, // exclusive
false, // no-wait
nil, // arguments
)
failOnError(err, "Failed to declare queue")
d.channel.QueueBind(
queueName, // name of the queue
key, // bindingKey
d.exchange, // sourceExchange
false, // noWait
nil, // arguments
)
failOnError(err, "Failed to bind queue to exchange")
deliveries, err := d.channel.Consume(
q.Name, // queue
"", // consumer
true, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
failOnError(err, "Failed to register a consumer")
go d.consume(key, consumer, deliveries)
}
forever := make(chan bool)
<-forever
}
func (d *amqpDispatcher) Close() {
d.channel.Close()
d.connection.Close()
}
func (d *amqpDispatcher) consume(key string, handler MessageHandler, deliveries <-chan amqp.Delivery) {
log.Printf("Consuming %s...", key)
for delivery := range deliveries {
log.Printf("%s: Received %d bytes: [%v] %q",
key, len(delivery.Body), delivery.DeliveryTag, delivery.Body)
messageType := handler.MessageType()
var message Message
var err error
messageMapper := d.messageMapperRegistry.MapperForType(messageType)
if messageMapper != nil {
message, err = messageMapper.MapToMessage(delivery.Body)
} else {
// todo: investigate why this doesn't work
message = reflect.Zero(messageType)
err = json.Unmarshal([]byte(delivery.Body), &message)
}
if err != nil {
log.Printf("Error: %s", err.Error())
} else {
err = handler.Handle(message)
if err != nil {
log.Printf("Error: %s", err.Error())
}
}
}
}
// InitialiseDispatcher initialises an amqpDispatcher and
// establishes an AMQ connection.
func InitialiseDispatcher(url string, exchange string, subscriberRegistry *SubscriberRegistry, messageMapperRegistry *MessageMapperRegistry) Dispatcher {
conn, err := amqp.Dial(url)
failOnError(err, "Failed to connect to RabbitMQ")
ch, err := conn.Channel()
failOnError(err, "Failed to open a channel")
return &amqpDispatcher{
subscriberRegistry: subscriberRegistry,
messageMapperRegistry: messageMapperRegistry,
connection: conn,
channel: ch,
exchange: exchange}
}