https://docs.spring.io/spring-modulith/reference/events.html
@Service
class OrderManagement(val inventory: InventoryManagement) {
@Transactional
fun complete(order: Order) {
inventory.updateStockFor(order)
}
}
complete()
메서드는 다른 애플리케이션 모듈에 정의된 스프링 빈과 상호 작용하고 있다.
OrderManagement
인스턴스를 생성하기 위해 의존하는 빈이 필요하기에 테스트하기 까다로워진다.@Service
class OrderManagement(val events: ApplicationEventPublisher, val dependency: OrderInternal) {
@Transactional
fun complete(order: Order) {
events.publishEvent(OrderCompleted(order.id))
}
}
@Component
class InventoryManagement {
@Async
@TransactionalEventListener
fun on(event: OrderCompleted) { /* … */ }
}
@Component
class InventoryManagement {
@Async
@Transactional(propagation = Propagation.REQUIRES_NEW)
@TransactionalEventListener
fun on(event: OrderCompleted) { /* … */ }
}
@ApplicationModuleListener
를 제공한다.@Component
class InventoryManagement {
@ApplicationModuleListener
fun on(event: OrderCompleted) { /* … */ }
}
spring-modulith-starter-jpa
spring-modulith-starter-jdbc
spring-modulith-starter-mongodb
spring-modulith-starter-neo4j
spring-modulith-events-api
CompletedEventPublications
IncompleteEventPublications
predicate
에 일치하거나 발행 날짜를 기준으로 지정된 Duration
보다 오래된 것을 다시 제출할 수 있다.EventPublicationRepository
SPIspring.modulith.events.jdbc.schema-initialization.enabled=true
spring-modulith-events-core
에 포함된 EventSerializer
추상화를 통해 이벤트 인스턴스를 데이터 저장소에 적합한 형식으로 변환하는 방법에 대한 다양한 전략을 사용할 수 있다.Clock.systemUTC()
가 반환한 날짜를 이벤트 발행 시간으로 사용한다.Clock
빈을 등록하면 된다.spring-modulith-events-{메시지 브로커 이름}
)@Externalized
어노테이션을 사용하여 외부화할 이벤트 타입 선택spring-modulith-events-kafka
spring-modulith-eventsamap
spring-modulith-events-jms
spring-modulith-events-aws-sqs
spring-modulith-events-aws-sns
@Externalized
어노테이션을 통해 외부화 이벤트 선택@Externalized
어노테이션에 특별히 정의하지 않는한 기본적으로 appllication-local 타입 이름을 사용한다.
1. 즉 com.acme.app
기본 패키지가 있는 애플리케이션에서 com.acme.app.sample.SampleEvent
는 sample.SampleEvent
에 게시된다.
$target::$key
패턴을 사용하면 된다.
CustomerCreated
이벤트는 메서드를 통해 고객의 lastName을 노출하고 있다.@Externalized
어노테이션 내부 표현식에서 사용한다.@Externalized("customer-created::#{#this.getLastname()}")
class CustomerCreated {
fun getLastname(): String {
// …
}
}
@Externalized("…::#{@beanName.someMethod(#this)}")
spring-modulith-events-api
에는 위의 모든 단계를 사용자 정의할 수 있는 EventExternalizationConfiguration
을 포함한다.@Configuration
class ExternalizationConfiguration {
@Bean
fun eventExternalizationConfiguration(): EventExternalizationConfiguration {
EventExternalizationConfiguration.externalizing()
.select(EventExternalizationConfiguration.annotatedAsExternalized())
.mapping(SomeEvent::class, it -> …)
.routeKey(WithKeyProperty::class, WithKeyProperty::getKey)
.build()
}
}
@ApplicationModuleTest
를 사용하면 테스트 메서드에 발행된 이벤트 인스턴스를 주입할 수 있다.@ApplicationModuleTest
class OrderIntegrationTests {
@Test
fun someTestMethod(events: PublishedEvents) {
// …
var matchingMapped = events.ofType(OrderCompleted::class)
.matching(OrderCompleted::getOrderId, reference.getId())
assertThat(matchingMapped).hasSize(1)
}
}