Skip to content
This repository has been archived by the owner on Nov 21, 2023. It is now read-only.

Commit

Permalink
[#22] integration event base 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
chanhyeong committed Oct 31, 2021
1 parent 49fb330 commit 7340e72
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.sns.localevent

interface DomainEvent {
val eventId: String

val channel: String
}
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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 <T : DomainEvent> publish(domainEvent: T) {
val channel = applicationContext.getBean(domainEvent.channel) as MessageChannel

log.info("publish ${domainEvent.eventId}")

channel.send(GenericMessage(domainEvent))
}
}
Original file line number Diff line number Diff line change
@@ -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<TestUser>() {
) {
@Id
private var id: Int? = null

fun happy() {
registerEvent(LaughingEvent(nickName))
fun happy(publish: (DomainEvent) -> Unit) {
publish(LaughingEvent(nickName))
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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("하 하 하")
}
Expand Down

This file was deleted.

0 comments on commit 7340e72

Please sign in to comment.