-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiver.go
74 lines (57 loc) · 1.39 KB
/
receiver.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
package main
import (
"SimpleGoEventProcessingExample/internal"
"SimpleGoEventProcessingExample/types"
"context"
"encoding/json"
"log"
"time"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
server := &types.Server{
Rdb: internal.NewRedisClient("localhost:6379", "", 0),
Ctx: ctx,
Conn: internal.NewRabbitConnection(),
QueueName: "person_created",
}
defer server.Conn.Close()
defer server.Rdb.Close()
internal.PingOrPanic(server)
ch, err := server.Conn.Channel()
internal.HandleError(err, "Error creating RabbitMQ channel")
defer ch.Close()
queue, err := ch.QueueDeclare(
server.QueueName,
false,
false,
false,
false,
nil)
internal.HandleError(err, "Failed to declare a queue")
msgs, err := ch.ConsumeWithContext(
ctx,
queue.Name,
"",
true,
false,
false,
false,
nil)
internal.HandleError(err, "Failed to register a consumer")
var forever chan struct{}
go func() {
for d := range msgs {
log.Printf("Received a message: %s", d.Body)
event := internal.ProcessEvent(d)
person := internal.GetPerson(server, event.ID)
body, err := json.Marshal(person)
internal.HandleError(err, "Failed to marshal JSON")
log.Printf("Person: %s\n", body)
internal.RemovePerson(server, event.ID)
}
}()
log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
<-forever
}