-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefaultUserNotificationPublisher.kt
58 lines (50 loc) · 2.03 KB
/
DefaultUserNotificationPublisher.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
package com.aamdigital.aambackendservice.notification.core
import com.aamdigital.aambackendservice.error.AamErrorCode
import com.aamdigital.aambackendservice.error.InternalServerException
import com.aamdigital.aambackendservice.notification.core.event.CreateUserNotificationEvent
import com.aamdigital.aambackendservice.queue.core.QueueMessage
import com.fasterxml.jackson.databind.ObjectMapper
import org.slf4j.LoggerFactory
import org.springframework.amqp.AmqpException
import org.springframework.amqp.rabbit.core.RabbitTemplate
import java.time.Instant
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
import java.util.*
class DefaultUserNotificationPublisher(
private val objectMapper: ObjectMapper,
private val rabbitTemplate: RabbitTemplate,
) : UserNotificationPublisher {
enum class DefaultNotificationPublisherErrorCode : AamErrorCode {
EVENT_PUBLISH_ERROR
}
private val logger = LoggerFactory.getLogger(javaClass)
override fun publish(channel: String, event: CreateUserNotificationEvent): QueueMessage {
val message = QueueMessage(
id = UUID.randomUUID(),
eventType = CreateUserNotificationEvent::class.java.canonicalName,
event = event,
createdAt = Instant.now()
.atOffset(ZoneOffset.UTC)
.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
)
try {
rabbitTemplate.convertAndSend(
channel,
objectMapper.writeValueAsString(message)
)
} catch (ex: AmqpException) {
throw InternalServerException(
message = "Could not publish SendNotificationEvent: $event",
code = DefaultNotificationPublisherErrorCode.EVENT_PUBLISH_ERROR,
cause = ex
)
}
logger.trace(
"[DefaultNotificationPublisher]: publish message to channel '{}' Payload: {}",
channel,
objectMapper.writeValueAsString(message)
)
return message
}
}