[Spring Integration] With Kotlin DSL

DaeHoon·2022년 2월 16일
0

Spring Integration는 dsl 형태로 message flow을 구성할 수 있는 편리한 빌더들과 API를 제공한다.

DSL Basics

  • org.springframework.Integration.dsl 패키지에는 IntegrationFlowBuilder API와 여러 IntegrationComponentSpec 구현체가 포함되어 있으며, 이러한 구현은 빌더이기도 하며 구체적인 엔드포인트를 구성할 수 있는 유연한 API를 제공한다.

  • IntegrationFlowBuilder 인프라는 채널, 엔드포인트, 폴러 및 채널 인터셉터와 같은 메시지 기반 응용프로그램을 위한 공통 EIP(Enterprise Integration Patter)를 제공한다.

  • 엔드포인트는 가독성을 향상시키기 위해 dsl에서 동사로 표현된다. EIP 엔드포인트를 사용하는 dsl 메서드로 정의했다

transform → Transformer
filter → Filter
handle → ServiceActivator
split → Splitter
aggregate → Aggregator
route → Router
bridge → Bridge


참고 자료 엔드포인트 용어에 대한 개념 정의

  • integration processes는 이러한 엔드포인트를 하나 이상의 message flow로 구성함으로써 구성된다. 아래 코드는 EIP method를 사용하는 IntegrationFlows factory를 사용하여 IntegrationFlow bean을 정의한다.
@Bean
fun myFlow(): IntegrationFlow? {
  return IntegrationFlows.from("input")
    .filter { anObject: Any? -> "World".equals(anObject) }
    .transform { str: String -> "Hello $str" }
    .handle { x: Message<*>? -> println(x) }
    .get()
}

Inbound Channel Adapters

  • Message Flows는 Inbount Channel Adapter에서 시작.
  • 어댑터가 poller로 구성되어 있으며 MessageSource 객체에 주기적으로 메시지를 생성한다.
abstract class PollingChannelAdapter<T>(
    private val pollingChannelPageConfiguration: PollingChannelPageConfiguration
): IntegrationObjectSupport(), MessageSource<List<T>> {
    @Value("\${spring.application.name}")
    private lateinit var appName: String
    
    lateinit var time: PollingChannelTimeConfiguration
    
    override fun receive(): Message<List<T>> {
    ......
      return message
   }
  • MessageSource를 상속 받을 시 메시지를 받을 때 호출되는 receive() 함수를 오버라이딩 해야 한다.

Service Activators and handle Method

  • handle 메소드의 목적은 MessageHandler의 객체나 메소드를 호출하는 것.
  • 또 다른 목적은 람다 식을 활용해 메시지를 어떻게 활용할 것인지 정의할 수 있다.
  • handle 안의 scope에서 반환이 발생할 시 output channel로 데이터를 리턴값을 보낸다.
  • get 메소드는 input channel에 있는 receive 함수를 호출.
  • 예제 코드는 filter -> transformer -> serviceActivators의 순서로 구성. 리턴 값이 존재하지 않고 output channel을 정의하지 않아 출력만 하고 끝이 난다.
@Bean
fun myFlow(): IntegrationFlow? {
  return IntegrationFlows.from([InputChannelAdapterObject])
    .filter { anObject: Any? -> "World".equals(anObject) }
    .transform { str: String -> "Hello $str" }
    .handle { x: Message<*>? -> println(x) }
    .get()
}
profile
평범한 백엔드 개발자

0개의 댓글