-
Notifications
You must be signed in to change notification settings - Fork 0
/
Service2Service3Subscriber.kt
158 lines (141 loc) · 6.17 KB
/
Service2Service3Subscriber.kt
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package ru.quipy.saga.aggregation.service1.subscribers
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.dao.DuplicateKeyException
import org.springframework.data.annotation.Id
import org.springframework.data.annotation.Version
import org.springframework.data.mongodb.core.mapping.Document
import org.springframework.data.mongodb.repository.MongoRepository
import org.springframework.stereotype.Component
import org.springframework.stereotype.Repository
import ru.quipy.core.EventSourcingService
import ru.quipy.domain.Aggregate
import ru.quipy.domain.Event
import ru.quipy.saga.SagaContext
import ru.quipy.saga.SagaManager
import ru.quipy.saga.aggregation.service2.api.Service2Aggregate
import ru.quipy.saga.aggregation.service2.api.Service2ProcessedEvent
import ru.quipy.saga.aggregation.service3.api.Service3Aggregate
import ru.quipy.saga.aggregation.service3.api.Service3ProcessedEvent
import ru.quipy.saga.plus
import ru.quipy.streams.AggregateSubscriptionsManager
import java.util.*
import javax.annotation.PostConstruct
@Component
class Service2Service3Subscriber(
private val subscriptionsManager: AggregateSubscriptionsManager,
private val aggregationRepository: AggregationRepository,
private val service1EsService: EventSourcingService<UUID, ru.quipy.saga.aggregation.service1.api.Service1Aggregate, ru.quipy.saga.aggregation.service1.logic.Service1>,
private val sagaManager: SagaManager
) {
private val logger: Logger = LoggerFactory.getLogger(Service2Service3Subscriber::class.java)
@PostConstruct
fun init() {
subscriptionsManager.createSubscriber(Service2Aggregate::class, "service1::service2-subscriber") {
`when`(Service2ProcessedEvent::class) { event ->
val id = getSagaId(event)
var aggregationOptional: Optional<Aggregation>
try {
aggregationOptional = aggregationRepository.findById(id)
if (!aggregationOptional.isPresent) {
val service2 = Service2Aggregation(
event.service2Id,
event.sagaContext
)
aggregationRepository.save(
Aggregation(
id,
service2,
null
)
)
}
} catch (e: DuplicateKeyException) {
logger.warn("Optimistic lock exception")
}
aggregationOptional = aggregationRepository.findById(id)
if (aggregationOptional.isPresent && aggregationOptional.get().service3 != null) {
val aggregation = aggregationOptional.get()
aggregation.service2 = Service2Aggregation(
event.service2Id,
event.sagaContext
)
aggregationRepository.save(aggregation)
val service3 = aggregation.service3
val service2 = aggregation.service2
val sagaContext = sagaManager
.withContextGiven(service2!!.sagaCtx + service3!!.sagaCtx)
.performSagaStep("SAGA_EXAMPLE", "finish saga")
.sagaContext
service1EsService.update(event.service2Id, sagaContext) {
it.finishSaga(event.service2Id, service3.id)
}
}
}
}
subscriptionsManager.createSubscriber(Service3Aggregate::class, "service1::service3-subscriber") {
`when`(Service3ProcessedEvent::class) { event ->
val id = getSagaId(event)
var aggregationOptional: Optional<Aggregation>
try {
aggregationOptional = aggregationRepository.findById(id)
if (!aggregationOptional.isPresent) {
val service3 = Service3Aggregation(
event.service3Id,
event.sagaContext
)
aggregationRepository.save(
Aggregation(
id,
null,
service3
)
)
}
} catch (e: DuplicateKeyException) {
logger.warn("Optimistic lock exception")
}
aggregationOptional = aggregationRepository.findById(id)
if (aggregationOptional.isPresent && aggregationOptional.get().service2 != null) {
val aggregation = aggregationOptional.get()
aggregation.service3 = Service3Aggregation(
event.service3Id,
event.sagaContext
)
aggregationRepository.save(aggregation)
val service3 = aggregation.service3
val service2 = aggregation.service2
val sagaContext = sagaManager
.withContextGiven(service3!!.sagaCtx + service2!!.sagaCtx)
.performSagaStep("SAGA_EXAMPLE", "finish saga")
.sagaContext
service1EsService.update(service3.id, sagaContext) {
it.finishSaga(service2.id, service3.id)
}
}
}
}
}
private fun <A : Aggregate, E : Event<A>> getSagaId(event: E): UUID {
return event.sagaContext.ctx["SAGA_EXAMPLE"]!!.sagaInstanceId
}
}
@Document("aggregation-example")
data class Aggregation(
@Id
val sagaInstanceId: UUID,
var service2: Service2Aggregation?,
var service3: Service3Aggregation?,
@Version
val version: Int = 0
)
data class Service2Aggregation(
val id: UUID,
val sagaCtx: SagaContext
)
data class Service3Aggregation(
val id: UUID,
val sagaCtx: SagaContext
)
@Repository
interface AggregationRepository : MongoRepository<Aggregation, UUID>