-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefaultNotificationEventConsumer.kt
59 lines (52 loc) · 2.27 KB
/
DefaultNotificationEventConsumer.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
package com.aamdigital.aambackendservice.reporting.notification.core
import com.aamdigital.aambackendservice.error.AamException
import com.aamdigital.aambackendservice.queue.core.QueueMessageParser
import com.aamdigital.aambackendservice.reporting.domain.event.NotificationEvent
import com.aamdigital.aambackendservice.reporting.notification.di.ReportingNotificationQueueConfiguration.Companion.NOTIFICATION_QUEUE
import com.rabbitmq.client.Channel
import org.slf4j.LoggerFactory
import org.springframework.amqp.AmqpRejectAndDontRequeueException
import org.springframework.amqp.core.Message
import org.springframework.amqp.rabbit.annotation.RabbitListener
class DefaultNotificationEventConsumer(
private val messageParser: QueueMessageParser,
private val useCase: TriggerWebhookUseCase,
) : NotificationEventConsumer {
private val logger = LoggerFactory.getLogger(javaClass)
@RabbitListener(
queues = [NOTIFICATION_QUEUE],
)
override fun consume(rawMessage: String, message: Message, channel: Channel) {
val type = try {
messageParser.getTypeKClass(rawMessage.toByteArray())
} catch (ex: AamException) {
throw AmqpRejectAndDontRequeueException("[${ex.code}] ${ex.localizedMessage}", ex)
}
when (type.qualifiedName) {
NotificationEvent::class.qualifiedName -> {
val payload = messageParser.getPayload(
body = rawMessage.toByteArray(),
kClass = NotificationEvent::class
)
try {
useCase.trigger(payload)
} catch (ex: Exception) {
throw AmqpRejectAndDontRequeueException(
"[USECASE_ERROR] ${ex.localizedMessage}",
ex
)
}
return
}
else -> {
logger.warn(
"[DefaultNotificationEventConsumer] Could not find any use case for this EventType: {}",
type.qualifiedName,
)
throw AmqpRejectAndDontRequeueException(
"[NO_USECASE_CONFIGURED] Could not found matching use case for: ${type.qualifiedName}",
)
}
}
}
}