-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatchingSubscription.go
61 lines (51 loc) · 1.49 KB
/
dispatchingSubscription.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
package eventuate
import (
"github.com/eventuate-clients/eventuate-client-golang/future"
)
type DispatchingSubscription struct {
*Subscription
eventHandlers *EventResultHandlerMap
//subscription *Subscription
typeHints TypeHintMapper
//mgr *subscriptionManager
//ll loglib.LogLevelEnum
//lg loglib.Logger
}
func (sub *DispatchingSubscription) dispatchEvent(evt StompEvent) {
//sub := sub.subscription
defer func() {
if r := recover(); r != nil {
err := AppError(
"Recovered in event handler: %#v", r)
sub.handleEventHandlerResults(&evt, nil, err)
}
}()
var (
result future.Settler
evtHandler EventResultHandler
)
evtHandler = *sub.eventHandler
result = evtHandler(NewEventMetadataFromStomp(&evt, sub.typeHints))
if result.IsSettled() {
val, err := result.GetValue()
sub.handleEventHandlerResults(&evt, val, err)
} else {
go func() {
val, err := result.GetValue() // blocks
sub.handleEventHandlerResults(&evt, val, err)
}()
}
}
func (sub *DispatchingSubscription) handleEventHandlerResults(evt *StompEvent, val interface{}, err error) {
sub.lg.Printf("handleEventHandlerResults. subscription #%s, event: %v\nValue: %v\nError: %#v",
sub.Id, evt, val, err)
if err != nil {
sub.lg.Printf("Failing event: %v\n", evt)
sub.subscriptionErrors <- AppError(
"Failed handler for subscription #%s for event: %#v\nError: %#v",
sub.Id, evt, err)
return
}
sub.lg.Printf("Acknowledging event: %v\n", evt)
sub.AcknowledgeEvent(evt)
}