From 7340e72682ae56f5179636f339dfb2cd0470e3bb Mon Sep 17 00:00:00 2001 From: chanhyeong Date: Sun, 31 Oct 2021 11:02:23 +0900 Subject: [PATCH] =?UTF-8?q?[#22]=20integration=20event=20base=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/com/sns/localevent/DomainEvent.kt | 7 +++++++ .../config/IntegrationEventBaseConfig.kt | 11 +++++++++++ .../sns/localevent/service/EventPublisher.kt | 19 +++++++++++++++++++ .../user/component/test/domains/TestUser.kt | 8 ++++---- .../user/component/test/dtos/LaughingEvent.kt | 11 +++++++++-- .../test/listeners/EmotionListener.kt | 14 ++++---------- .../com/sns/user/core/supports/DomainEvent.kt | 3 --- 7 files changed, 54 insertions(+), 19 deletions(-) create mode 100644 submodules/local-event/src/main/kotlin/com/sns/localevent/DomainEvent.kt create mode 100644 submodules/local-event/src/main/kotlin/com/sns/localevent/config/IntegrationEventBaseConfig.kt create mode 100644 submodules/local-event/src/main/kotlin/com/sns/localevent/service/EventPublisher.kt delete mode 100644 user-api/src/main/kotlin/com/sns/user/core/supports/DomainEvent.kt diff --git a/submodules/local-event/src/main/kotlin/com/sns/localevent/DomainEvent.kt b/submodules/local-event/src/main/kotlin/com/sns/localevent/DomainEvent.kt new file mode 100644 index 0000000..3fe5ef0 --- /dev/null +++ b/submodules/local-event/src/main/kotlin/com/sns/localevent/DomainEvent.kt @@ -0,0 +1,7 @@ +package com.sns.localevent + +interface DomainEvent { + val eventId: String + + val channel: String +} diff --git a/submodules/local-event/src/main/kotlin/com/sns/localevent/config/IntegrationEventBaseConfig.kt b/submodules/local-event/src/main/kotlin/com/sns/localevent/config/IntegrationEventBaseConfig.kt new file mode 100644 index 0000000..1ed38ee --- /dev/null +++ b/submodules/local-event/src/main/kotlin/com/sns/localevent/config/IntegrationEventBaseConfig.kt @@ -0,0 +1,11 @@ +package com.sns.localevent.config + +import com.sns.localevent.service.EventPublisher +import org.springframework.context.ApplicationContext +import org.springframework.context.annotation.Bean + +class IntegrationEventBaseConfig { + + @Bean + fun eventPublisher(applicationContext: ApplicationContext) = EventPublisher(applicationContext) +} diff --git a/submodules/local-event/src/main/kotlin/com/sns/localevent/service/EventPublisher.kt b/submodules/local-event/src/main/kotlin/com/sns/localevent/service/EventPublisher.kt new file mode 100644 index 0000000..74f6601 --- /dev/null +++ b/submodules/local-event/src/main/kotlin/com/sns/localevent/service/EventPublisher.kt @@ -0,0 +1,19 @@ +package com.sns.localevent.service + +import com.sns.commons.utils.log +import com.sns.localevent.DomainEvent +import org.springframework.context.ApplicationContext +import org.springframework.messaging.MessageChannel +import org.springframework.messaging.support.GenericMessage + +class EventPublisher(private val applicationContext: ApplicationContext) { + private val log = log() + + fun publish(domainEvent: T) { + val channel = applicationContext.getBean(domainEvent.channel) as MessageChannel + + log.info("publish ${domainEvent.eventId}") + + channel.send(GenericMessage(domainEvent)) + } +} diff --git a/user-api/src/main/kotlin/com/sns/user/component/test/domains/TestUser.kt b/user-api/src/main/kotlin/com/sns/user/component/test/domains/TestUser.kt index 9b63d70..d765641 100644 --- a/user-api/src/main/kotlin/com/sns/user/component/test/domains/TestUser.kt +++ b/user-api/src/main/kotlin/com/sns/user/component/test/domains/TestUser.kt @@ -1,19 +1,19 @@ package com.sns.user.component.test.domains +import com.sns.localevent.DomainEvent import com.sns.user.component.test.dtos.LaughingEvent import org.springframework.data.annotation.Id -import org.springframework.data.domain.AbstractAggregateRoot import org.springframework.jdbc.core.BeanPropertyRowMapper import org.springframework.jdbc.core.RowMapper data class TestUser( val nickName: String = "anonymous" -) : AbstractAggregateRoot() { +) { @Id private var id: Int? = null - fun happy() { - registerEvent(LaughingEvent(nickName)) + fun happy(publish: (DomainEvent) -> Unit) { + publish(LaughingEvent(nickName)) } companion object { diff --git a/user-api/src/main/kotlin/com/sns/user/component/test/dtos/LaughingEvent.kt b/user-api/src/main/kotlin/com/sns/user/component/test/dtos/LaughingEvent.kt index 8d52879..bfa560e 100644 --- a/user-api/src/main/kotlin/com/sns/user/component/test/dtos/LaughingEvent.kt +++ b/user-api/src/main/kotlin/com/sns/user/component/test/dtos/LaughingEvent.kt @@ -1,5 +1,12 @@ package com.sns.user.component.test.dtos -import com.sns.user.core.supports.DomainEvent +import com.sns.localevent.DomainEvent +import com.sns.user.core.config.IntegrationConfig -class LaughingEvent(val who: String) : DomainEvent +class LaughingEvent(val who: String) : DomainEvent { + // FIMXE 예제용 임시 포맷 + override val eventId: String + get() = "$channel-$who-${System.currentTimeMillis()}" + + override val channel = IntegrationConfig.Channels.EMOTION +} diff --git a/user-api/src/main/kotlin/com/sns/user/component/test/listeners/EmotionListener.kt b/user-api/src/main/kotlin/com/sns/user/component/test/listeners/EmotionListener.kt index 5c690b9..65817e7 100644 --- a/user-api/src/main/kotlin/com/sns/user/component/test/listeners/EmotionListener.kt +++ b/user-api/src/main/kotlin/com/sns/user/component/test/listeners/EmotionListener.kt @@ -1,23 +1,17 @@ package com.sns.user.component.test.listeners +import com.sns.commons.utils.log +import com.sns.localevent.annotation.CustomEventListener import com.sns.user.component.test.dtos.LaughingEvent -import org.slf4j.LoggerFactory -import org.springframework.context.event.EventListener -import org.springframework.core.annotation.Order -import org.springframework.stereotype.Component -@Component +@CustomEventListener class EmotionListener { - val log = LoggerFactory.getLogger(javaClass) + private val log = log() - @EventListener(LaughingEvent::class) - @Order(1) fun onLaughing(event: LaughingEvent) { log.error("${event.who} is laughing") } - @EventListener - @Order(2) fun actionLaughing(event: LaughingEvent) { log.error("하 하 하") } diff --git a/user-api/src/main/kotlin/com/sns/user/core/supports/DomainEvent.kt b/user-api/src/main/kotlin/com/sns/user/core/supports/DomainEvent.kt deleted file mode 100644 index ac3e424..0000000 --- a/user-api/src/main/kotlin/com/sns/user/core/supports/DomainEvent.kt +++ /dev/null @@ -1,3 +0,0 @@ -package com.sns.user.core.supports - -interface DomainEvent