-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIncomingMsgProcessor.go
More file actions
126 lines (103 loc) · 3.08 KB
/
IncomingMsgProcessor.go
File metadata and controls
126 lines (103 loc) · 3.08 KB
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
121
122
123
124
125
126
package main
import (
"fmt"
"encoding/json"
)
import (
"github.com/streadway/amqp"
"bitbucket.org/smds/gofms/godelmar"
)
type IncomingMessageProcessor struct {
ConnProps amqp.URI
IncomingConn *amqp.Connection
IncomingChan *amqp.Channel
IncomingQ amqp.Queue
Sched FMSscheduler
OutgoingPool *goDelMar.RMQConnectionPool
}
func (m *IncomingMessageProcessor) Init(inQname string, props amqp.URI,
pool *goDelMar.RMQConnectionPool, s FMSscheduler) error {
var err error
m.ConnProps = props
m.Sched = s
m.OutgoingPool = pool
fmt.Printf("%s: Dialing RMQ and opening connection.\n", inQname)
m.IncomingConn, err = amqp.Dial(props.String())
if err != nil {
return err
}
fmt.Printf("%s: Opening incoming channel.\n", inQname)
m.IncomingChan, err = m.IncomingConn.Channel()
if err != nil {
return err
}
fmt.Printf("%s: Declaring incoming queue.\n", inQname)
m.IncomingQ, err = m.IncomingChan.QueueDeclare(
inQname, // qname
false, // durable
false, // delete when unused
false, // exclusive
false, // no-wait
nil, // arguments
)
if err != nil {
return err
}
return nil
}
func (m *IncomingMessageProcessor) ProcessMessages() {
fmt.Printf("%s: Registering consumer.\n", m.IncomingQ.Name)
msgs, err := m.IncomingChan.Consume(
m.IncomingQ.Name, // queue
"", // consumer
true, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
goDelMar.FailOnError(err, "Failed to register a consumer.")
//Process incoming payloads.
fmt.Printf("%s: Entering message processing loop...\n", m.IncomingQ.Name)
for d := range msgs {
go m.processMessage(d)
}
fmt.Println("Message processing loop ending... This shouldn't happen?")
}
func (m *IncomingMessageProcessor) StopProcessing() {
m.IncomingChan.Close()
m.IncomingConn.Close()
}
func (m *IncomingMessageProcessor) processMessage(d amqp.Delivery) {
fmt.Printf("%s: Received message\n", m.IncomingQ.Name)
var incoming goDelMar.GenericMsg
err := json.Unmarshal(d.Body, &incoming)
goDelMar.FailOnError(err, "Failed to unmarshal incoming message")
//fmt.Println(incoming.ModelPayload["26614f2c-5aff-4fc0-ba72-f16d9c22703f"])
//Check for Job ID and assign one if there isn't one already.
//id, err := uuid.FromString(incoming.JobID)
//if err != nil {
// lastidx := len(incoming.SolverList) - 1
// id, err = uuid.FromString(incoming.SolverList[lastidx])
// if err != nil {
// id = uuid.Must(uuid.NewV4())
// incoming.JobID = id.String()
// } else {
//
// }
//
//}
//fmt.Printf("%s: %s\n", m.IncomingQ.Name, string(d.Body[:]))
jobs := m.Sched.ScheduleGenericMsg(incoming)
for _, j := range jobs {
go m.sendJob(j)
fmt.Printf("%s: Sent message to %s\n", m.IncomingQ.Name, j.Destination)
//fmt.Printf("%s: %s\n", m.IncomingQ.Name, string(j.Body[:]))
}
}
func (m *IncomingMessageProcessor) sendJob(j ScheduledJob) {
conn := m.OutgoingPool.GetConnection()
defer m.OutgoingPool.ReleaseConnection(conn)
q := goDelMar.DeclareQueue(conn, j.Destination)
goDelMar.SendToQueue(conn, q, j.Body)
}