Reference
Spring Integration Basic Terms
後篇: https://www.isaacnote.com/2018/10/spring-integration-channels.html
後篇: https://www.isaacnote.com/2018/10/spring-integration-channels.html
Terms
org.springframework.integration.Message<T>
用來封裝訊息, 成員包含 MessageHeader 與 Payload
org.springframework.integration.MessageHeaders
MessageHeaders 是 immutable 的, 包含一些預設的 attribute: id, timestamp, correlation id, and priority
Payloads
用來裝 message body, 可以自訂 transformer 來傳遞封包
Message Channel
Message Channel 用來傳遞封包, 也用來 decouple producer 與 consumer.
Message Channel 有兩種模式:
- point to point, 一個封包只會被一個 consumer 收到
- public/subscribe: 一個封包會被多個 consumer 收到
Message Endpoints
endpoint 泛指 Spring Integration 裡面的各種 component.
- Message Adapter: 資料可從外部系統透過 adapter 送進 Spring Integration
- Transformer: 轉換訊息
- Filter: 決定 Message 是否傳給 Message Channel
- Router: 透過 Message 的內容判斷要送給哪一個 Message Channel
- Splitter: 把一個 Message 切成好幾個轉送給不同適合的 Message Channel
- Aggregator: 把多個訊息合併成一個
- Service activator: Message Channel 與 Service instance 之間的介面
Example
spring-context.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:int="http://www.springframework.org/schema/integration" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-5.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="examples" /> <int:channel id="input"/> <int:channel id="output"> <int:queue capacity="10"/> </int:channel> <int:service-activator input-channel="input" output-channel="output" ref="messageHandler"/> </beans>
examples/MessageHandler.java
@Component public class MessageHandler { @ServiceActivator public String handleMessage(String message) { System.out.println("Received message: " + message); return "MESSAGE:" + message; } }
examples/Application.java
public class Application { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context.xml"); context.start(); MessageChannel input = context.getBean("input", MessageChannel.class ); PollableChannel output = context.getBean("output", PollableChannel.class ); Scanner scanner = new Scanner(System.in); while (scanner.hasNextLine()) { input.send(MessageBuilder.withPayload(scanner.nextLine()).build()); } } }