generated from ministryofjustice/hmpps-template-kotlin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
fix the listener issues when parsing message body and getting message attributes (from header)
- Loading branch information
1 parent
e7373a2
commit 1ab1fee
Showing
7 changed files
with
63 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 3 additions & 5 deletions
8
...lin/uk/gov/justice/digital/hmpps/jobsboardintegrationapi/shared/domain/MessageServices.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,13 @@ | ||
package uk.gov.justice.digital.hmpps.jobsboardintegrationapi.shared.domain | ||
|
||
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.shared.infrastructure.MessageAttributes | ||
import java.time.Instant | ||
|
||
interface IntegrationMessageService { | ||
fun handleMessage(integrationEvent: IntegrationEvent, messageAttributes: MessageAttributes) | ||
} | ||
|
||
data class IntegrationEvent( | ||
val eventId: String, | ||
val eventType: String, | ||
val timestamp: Instant, | ||
val content: String, | ||
val eventId: String? = null, | ||
val eventType: String? = null, | ||
val message: String? = null, | ||
) |
56 changes: 37 additions & 19 deletions
56
...v/justice/digital/hmpps/jobsboardintegrationapi/shared/infrastructure/MessageListeners.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,57 @@ | ||
package uk.gov.justice.digital.hmpps.jobsboardintegrationapi.shared.infrastructure | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import io.awspring.cloud.sqs.annotation.SqsListener | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.messaging.handler.annotation.Header | ||
import org.springframework.messaging.handler.annotation.MessageMapping | ||
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.shared.domain.IntegrationEvent | ||
import uk.gov.justice.digital.hmpps.jobsboardintegrationapi.shared.domain.IntegrationMessageService | ||
|
||
const val ATTRIBUTE_EVENT_TYPE = "eventType" | ||
const val ATTRIBUTE_EVENT_ID = "eventId" | ||
const val ATTRIBUTE_ID = "id" | ||
|
||
class IntegrationMessageListener( | ||
private val integrationMessageService: IntegrationMessageService, | ||
private val objectMapper: ObjectMapper, | ||
) { | ||
companion object { | ||
private val log = LoggerFactory.getLogger(this::class.java) | ||
} | ||
|
||
@MessageMapping | ||
@SqsListener("integrationqueue", factory = "hmppsQueueContainerFactoryProxy") | ||
fun processMessage(message: Message) { | ||
val event = objectMapper.readValue(message.message, IntegrationEvent::class.java) | ||
integrationMessageService.handleMessage(event, message.messageAttributes) | ||
fun processMessage( | ||
message: String, | ||
@Header(ATTRIBUTE_EVENT_TYPE) eventType: String?, | ||
@Header(ATTRIBUTE_EVENT_ID) eventId: String?, | ||
@Header(ATTRIBUTE_ID) messageId: String?, | ||
) { | ||
log.info("processMessage()|Processing message:messageId=$messageId, eventType=$eventType, eventId=$eventId") | ||
val messageAttributes = MessageAttributes().also { attributes -> | ||
mapOf( | ||
ATTRIBUTE_EVENT_TYPE to eventType, | ||
ATTRIBUTE_EVENT_ID to eventId, | ||
ATTRIBUTE_ID to messageId, | ||
).filterValues { it != null }.let { attributes.putAll(it) } | ||
} | ||
val event = IntegrationEvent( | ||
eventType = eventType, | ||
message = message, | ||
) | ||
integrationMessageService.handleMessage(event, messageAttributes) | ||
} | ||
} | ||
|
||
data class MessageAttribute(val value: String, val type: String) { | ||
constructor(value: String) : this(value, "String") | ||
} | ||
typealias EventType = MessageAttribute | ||
class MessageAttributes() : HashMap<String, Any?>() { | ||
constructor(eventType: String) : this() { | ||
put(ATTRIBUTE_EVENT_TYPE, eventType) | ||
} | ||
|
||
class MessageAttributes() : HashMap<String, MessageAttribute>() { | ||
constructor(attribute: EventType) : this() { | ||
put(ATTRIBUTE_EVENT_TYPE, attribute) | ||
constructor(source: Map<String, Any?>) : this() { | ||
putAll(source) | ||
} | ||
|
||
val eventType: String? get() = this[ATTRIBUTE_EVENT_TYPE]?.value | ||
val eventType: String? get() = get(ATTRIBUTE_EVENT_TYPE) as? String? | ||
val eventId: String? get() = get(ATTRIBUTE_EVENT_ID) as? String? | ||
val messageId: String? get() = get(ATTRIBUTE_ID) as? String? | ||
} | ||
|
||
data class Message( | ||
val message: String, | ||
val messageId: String, | ||
val messageAttributes: MessageAttributes, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters