使用協議介面卡
到目前為止展示的所有示例都說明了 DSL 如何透過使用 Spring Integration 程式設計模型來支援訊息架構。然而,我們還沒有進行任何真正的整合。這需要透過 HTTP、JMS、AMQP、TCP、JDBC、FTP、SMTP 等訪問遠端資源,或者訪問本地檔案系統。Spring Integration 支援所有這些以及更多。理想情況下,DSL 應該對所有這些提供一流的支援,但是實現所有這些並跟上 Spring Integration 中新新增的介面卡是一個艱鉅的任務。因此,預期 DSL 會不斷追趕 Spring Integration。
因此,我們提供了高階 API 來無縫定義特定於協議的訊息傳遞。我們使用工廠和構建器模式以及 lambda 來做到這一點。您可以將工廠類視為“名稱空間工廠”,因為它們在具體協議特定的 Spring Integration 模組中扮演與 XML 名稱空間相同的角色。目前,Spring Integration Java DSL 支援 Amqp
、Feed
、Jms
、Files
、(S)Ftp
、Http
、JPA
、MongoDb
、TCP/UDP
、Mail
、WebFlux
和 Scripts
名稱空間工廠。以下示例展示瞭如何使用其中三個(Amqp
、Jms
和 Mail
)
@Bean
public IntegrationFlow amqpFlow() {
return IntegrationFlow.from(Amqp.inboundGateway(this.rabbitConnectionFactory, queue()))
.transform("hello "::concat)
.transform(String.class, String::toUpperCase)
.get();
}
@Bean
public IntegrationFlow jmsOutboundGatewayFlow() {
return IntegrationFlow.from("jmsOutboundGatewayChannel")
.handle(Jms.outboundGateway(this.jmsConnectionFactory)
.replyContainer(c ->
c.concurrentConsumers(3)
.sessionTransacted(true))
.requestDestination("jmsPipelineTest"))
.get();
}
@Bean
public IntegrationFlow sendMailFlow() {
return IntegrationFlow.from("sendMailChannel")
.handle(Mail.outboundAdapter("localhost")
.port(smtpPort)
.credentials("user", "pw")
.protocol("smtp")
.javaMailProperties(p -> p.put("mail.debug", "true")),
e -> e.id("sendMailEndpoint"))
.get();
}
前面的示例展示瞭如何將“名稱空間工廠”用作內聯介面卡宣告。但是,您可以從 @Bean
定義中使用它們,以使 IntegrationFlow
方法鏈更具可讀性。
在我們投入精力開發其他工廠之前,我們正在徵集社群對這些名稱空間工廠的反饋。我們也歡迎您就我們接下來應該支援哪些介面卡和閘道器的優先順序提供任何意見。 |
您可以在本參考手冊中協議特定的章節中找到更多 Java DSL 示例。
所有其他協議通道介面卡可以配置為通用 bean 並連線到 IntegrationFlow
,如下例所示
@Bean
public QueueChannelSpec wrongMessagesChannel() {
return MessageChannels
.queue()
.wireTap("wrongMessagesWireTapChannel");
}
@Bean
public IntegrationFlow xpathFlow(MessageChannel wrongMessagesChannel) {
return IntegrationFlow.from("inputChannel")
.filter(new StringValueTestXPathMessageSelector("namespace-uri(/*)", "my:namespace"),
e -> e.discardChannel(wrongMessagesChannel))
.log(LoggingHandler.Level.ERROR, "test.category", m -> m.getHeaders().getId())
.route(xpathRouter(wrongMessagesChannel))
.get();
}
@Bean
public AbstractMappingMessageRouter xpathRouter(MessageChannel wrongMessagesChannel) {
XPathRouter router = new XPathRouter("local-name(/*)");
router.setEvaluateAsString(true);
router.setResolutionRequired(false);
router.setDefaultOutputChannel(wrongMessagesChannel);
router.setChannelMapping("Tags", "splittingChannel");
router.setChannelMapping("Tag", "receivedChannel");
return router;
}