使用協議介面卡

到目前為止顯示的所有示例都說明了 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 支援 AmqpFeedJmsFiles(S)FtpHttpJPAMongoDbTCP/UDPMailWebFluxScripts 名稱空間工廠。以下示例展示瞭如何使用其中三個(AmqpJmsMail

@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;
}
© . This site is unofficial and not affiliated with VMware.