天天看點

Spring Integration-Message Endpoints概念介紹

作者:軟體搬運工老張

Message Endpoints概念介紹

Spring Integration是基于管道-過濾器架構的,Message Endpoint是過濾器中的一部分。在簡介中提到過,endpoint主要是用來關聯架構與應用業務代碼的,就像MVC模型中controller隻需要與url綁定即可擷取到請求資料進行業務處理一樣,endpoint隻需要與message channel進行映射即可。

這裡将與同學們共同學習Spring Integration提供的一些Endpoint類型。

Message Transformer

消息轉換器用于轉換消息内容及消息結構體,可以對消息内容進行修改。如:封包格式轉化(JSON-XML互轉),封包格式化,資料脫敏,封包頭修改等。

Message Filter

消息過濾器用于處理消息是否需要傳遞,可以用來進行權限及認證控制、限流控制等。

Message Router

消息路由器用于決定消息下一步發送到哪個通道,一般根據封包内容或消息中繼資料來确定。

Spring Integration-Message Endpoints概念介紹

Splitter

分流器可以将一個消息分成多個消息,并且可以将這些消息分發給不同的輸出通道。

Aggregator

聚合器與分流器相反,用于将多個消息聚合為一個消息,但是聚合器要比分流器複雜得多,因為為了聚合不同的消息需要維護待聚合消息的狀态,以此決定一組消息是否完成了聚合或者是否已經逾時。Spring Integration提供了CorrelationStrategy和ReleaseStrategy兩種政策、逾時配置以及一個丢棄通道,用于決定當聚合逾時候是否發送部分消息。

Service Activator

Service Activator用于調用service對象的方法,在調用過程中activator提取請求消息的payload作為參數去調用service的方法,當service方法有傳回值時将會被封裝成Message對象并發送到輸出通道。是以在配置Service Activator時需要配置消息輸入通道及消息輸出通道(如果有傳回值),如果沒有配置輸出通道,也可以通過消息頭中的"Return Address"來指定輸出通道。

Spring Integration-Message Endpoints概念介紹

Channel Adapter

Channel Adapter用來提供不同協定消息的适配。

Spring Integration-Message Endpoints概念介紹

Endpoint Bean Names

Endpoint有多種配置方式:

Service Activator:

<int:service-activator id = "someService" ... />
           
@Component
public class SomeComponent {
  @ServiceActivator(inputChannel = ...)
  public String someMethod(...) {
  ...
  }
}
@Component
public class SomeComponent {
  @EndpointId("someService")
  @ServiceActivator(inputChannel = ...)
  public String someMethod(...) {
  ...
  }
}
@Configuration
public class SomeConfiguration {
  @Bean
  @ServiceActivator(inputChannel = ...)
  public MessageHandler someHandler() {
  ...
  }
}
@Configuration
public class SomeConfiguration {
  @Bean("someService.handler") ①
  @EndpointId("someService") ②
  @ServiceActivator(inputChannel = ...)
  public MessageHandler someHandler() {
  ...
  }
}
           

Channel Adapter:

<int:inbound-channel-adapter id = "someAdapter" ... />
           
@EndpointId("someAdapter")
@InboundChannelAdapter(channel = "channel", poller = @Poller(fixedDelay = "5000" ))
public String pojoSource() {
  ... 
}
@Bean("someAdapter.source")
@EndpointId("someAdapter")
@InboundChannelAdapter(channel = "channel", poller = @Poller(fixedDelay = "5000" ))
public MessageSource<?> source() {
  return () -> {
  ...
  }; 
}           

繼續閱讀